Exemplo n.º 1
0
    def test_multiword(self):
        def create():
            p = Parser()
            p.multiword('aa')
            p.str('ab')
            return p

        self.assertRaises(MissingValueError, create()._process_command_line, ['--aa'])
        vals = create()._process_command_line(['--aa', 'a', '--ab', 'b'])
        self.assertEqual(vals['aa'], 'a')
        self.assertEqual(vals['ab'], 'b')

        vals = create()._process_command_line(['--aa', 'a c d', '--ab', 'b'])
        self.assertEqual(vals['aa'], 'a c d')

        vals = create()._process_command_line(['--aa', 'a', 'c', 'd', '--ab', 'b'])
        self.assertEqual(vals['aa'], 'a c d')

        p = Parser().set_single_prefix('+').set_double_prefix('M')
        p.multiword('aa')
        p.str('ab').shorthand('a')
        vals = p._process_command_line(['Maa', 'a', 'c', 'd', 'Mab', 'b'])
        self.assertEqual(vals['aa'], 'a c d')

        p = Parser().set_single_prefix('+').set_double_prefix('M')
        p.multiword('aa')
        p.str('ab').shorthand('a')
        vals = p._process_command_line(['Maa', 'a', 'c', 'd', '+a', 'b'])
        self.assertEqual(vals['aa'], 'a c d')

        self.assertRaises(ValueError, Parser().set_single_prefix('++').set_double_prefix, '+')
Exemplo n.º 2
0
    def test_cast(self):
        p = Parser()
        p.str('x').cast(int)
        vals = p._process_command_line(['--x', '1'])
        self.assertEqual(vals['x'], 1)

        p = Parser()
        p.str('x').cast(int)
        self.assertRaises(ArgumentError, p._process_command_line, ['--x', 'a'])

        p = Parser()
        p.int('x')
        vals = p._process_command_line(['--x', '1'])

        p = Parser()
        p.multiword('x').cast(lambda x: [float(y) for y in x.split()])
        vals = p._process_command_line(['--x', '1.2 9.8 4.6'])
        self.assertEqual(vals['x'], [1.2, 9.8000000000000007,
            4.5999999999999996])

        p = Parser()
        p.int('x').default('yes')
        self.assertRaises(FormatError, p._process_command_line)
Exemplo n.º 3
0
 def create():
     p = Parser()
     p.multiword('aa')
     p.str('ab')
     return p
Exemplo n.º 4
0
    def test_config(self):
        def create():
            p = Parser()
            p.config('a')
            return p

        self.assertRaises(IOError, create()._process_command_line, ['--a',
             'doesnotexist.cfg'])

        fname = os.path.join(self._dir, 'config.cfg')

        def write_config(**kw):
            delim = '='
            if 'delim' in kw:
                delim = kw['delim']
                del kw['delim']
            with open(fname, 'w') as w:
                w.write('[myconfig]\n# comment1\n; comment2\n' + '\n'.join(
                    ('%s %s %s' % (k, delim, v) for k, v in kw.items())))

        write_config(b=3, c='hello')

        vals = create()._process_command_line(['--a', fname])
        self.assertEqual(list(vals.keys()), ['help'])

        def create():
            p = Parser({})
            p.config('a')
            p.int('b')
            p.str('c')
            p.int('d')
            return p

        for delim in '=:':
            write_config(b=3, c='hello', d=5, delim=delim)
            try:
                vals = create()._process_command_line(['--a', fname])
            except MultipleSpecifiedArgumentError as e:
                print(e)
            self.assertEqual(vals['b'], 3)
            self.assertEqual(vals['c'], 'hello')
            self.assertEqual(vals['d'], 5)

        write_config(b=3, c='hello', d='what')
        self.assertRaises(FormatError, create()._process_command_line, ['--a', fname])

        # XXX may want subsequent line to eventually be true
#        vals = create()._process_command_line(['--a', fname, '--d', '4'])
        self.assertRaises(MultipleSpecifiedArgumentError,
                create()._process_command_line, (['--a', fname, '--d', '4']))

#         self.assertEqual(vals['b'], 3)
#         self.assertEqual(vals['c'], 'hello')
#         self.assertEqual(vals['d'], 4)

        self.assertRaises(MultipleSpecifiedArgumentError,
                create()._process_command_line, (['--a', fname, '--d', '4', '--c', 'sup',
                '--b', '1']))

#         self.assertEqual(vals['b'], 1)
#         self.assertEqual(vals['c'], 'sup')
#         self.assertEqual(vals['d'], 4)

        def create():
            p = Parser({})
            p.config('a').default(fname)
            p.int('b').requires(p.int('d'))
            p.str('c')
            return p

        write_config(b=3, c='sup')
        self.assertRaises(DependencyError, create()._process_command_line) # should pass because default
        self.assertRaises(DependencyError, create()._process_command_line, ['--a', fname])

        with open(fname, 'w') as w:
            w.write('''[myconfig]
b=9
[part]
b=3
''')

        p = Parser()
        p.config('a')
        p.int('b')
        self.assertRaises(MultipleSpecifiedArgumentError,
                p._process_command_line, ['--a', fname])

        p = Parser()
        p.config('a')
        p.int('b').multiple()
        vals = p._process_command_line(['--a', fname])
        self.assertEqual(sorted(vals['b']), [3, 9])

        write_config(b='hello world')
        p = Parser()
        p.config('a')
        p.multiword('b')
        vals = p._process_command_line(['--a', fname])
        self.assertEqual(vals['b'], 'hello world')