Exemplo n.º 1
0
 def test_errors(self):
     p = Parser()
     p.int('a')
     try:
         p._process_command_line([])
     except TypeError:
         self.fail()
Exemplo n.º 2
0
    def test_conditions(self):
        p = Parser()
        a = p.float('a')
        b = p.float('b')
        c = p.float('c').requires(a.or_(b))
        self.assertRaises(DependencyError, p._process_command_line, ['--c', '9.2'])
        p._process_command_line(['--a', '11', '--c', '9.2'])
        p._process_command_line(['--b', '11', '--c', '9.2'])

        def create():
            p = Parser()
            a = p.float('a')
            b = p.float('b')
            c = p.float('c').if_(a.or_(b))
            return p

        create()._process_command_line(['--c', '9.2'])

        self.assertRaises(MissingRequiredArgumentError,
                create()._process_command_line, ['--a', '11'])

        create()._process_command_line(['--a', '11', '--c', '9.2'])
        self.assertRaises(MissingRequiredArgumentError,
                create()._process_command_line, ['--b', '11'])

        create()._process_command_line(['--b', '11', '--c', '9.2'])
Exemplo n.º 3
0
 def test_requires_and_default(self):
     p = Parser()
     p.int('a').default(3)
     b = p.int('b').requires(p['a'])
     b.requires(p['a'])
     self.assertEqual(len(b._getreqs()), 1)
     p._process_command_line(['--b', '5'])
Exemplo n.º 4
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.º 5
0
    def test_unspecified_default(self):
        p = Parser({})
        p.str('x').unspecified_default()
        self.assertRaises(ValueError, Option.unspecified_default, p.str('y'))

        vals = {}
        p = Parser(vals)
        p.str('x').unspecified_default().required()
        p._process_command_line(['ok'])
        self.assertEqual(vals['x'], 'ok')

        p = Parser({})
        p.str('x').unspecified_default().conflicts(p.str('y'))
        self.assertRaises(ConflictError, p._process_command_line,
            ['--y', 'a', 'unspecified_default'])

        p = Parser({})
        p.str('x').unspecified_default().conflicts(p.str('y'))
        self.assertRaises(ConflictError, p._process_command_line,
            ['unspecified_default', '--y', 'a'])

        # multiple
        p = Parser()
        p.str('x').unspecified_default()
        self.assertRaises(ValueError, p.str('y').unspecified_default)
Exemplo n.º 6
0
 def test_unspecified(self):
     p = Parser()
     arg = 'b'
     self.assertRaises(UnspecifiedArgumentError, p._process_command_line, ['--' + arg])
     try:
         p._process_command_line(['--' + arg])
     except UnspecifiedArgumentError as e:
         self.assertEqual(str(e), UnspecifiedArgumentError.message.format(arg))
Exemplo n.º 7
0
    def test_default(self):
        p = Parser()
        p.int('x').default(5)
        vals = p._process_command_line([])
        self.assertEqual(vals['x'], 5)

        p = Parser()
        p.int('x').default(5)
        vals = p._process_command_line(['--x', '6'])
        self.assertEqual(vals['x'], 6)
Exemplo n.º 8
0
    def test_flag(self):
        p = Parser()
        p.flag('x')
        vals = p._process_command_line()
        self.assertTrue('x' in vals)
        self.assertFalse(vals['x'])

        p = Parser()
        p.flag('x')
        vals = p._process_command_line(['--x'])
        self.assertTrue(vals['x'])
Exemplo n.º 9
0
    def test_oo(self):
        p = Parser()
        p.int('x')
        y = p.int('y')
        z = p.int('z').requires('y')
        x = p['x']
        x.requires(y, z)
        self.assertRaises(DependencyError, p._process_command_line, ['--x', '3'])
        self.assertRaises(DependencyError, p._process_command_line, ['--z', '3'])
        p._process_command_line(['--y', '3'])

        p = Parser()
        p.flag('x')
        p.flag('y').conflicts('x')
        self.assertRaises(ConflictError, p._process_command_line, ['--y', '--x'])
Exemplo n.º 10
0
    def test_add(self):
        p = Parser()
        p.str('x')
        vals = p._process_command_line(['--x', 'hi'])
        self.assertEqual(vals['x'], 'hi')

        p = Parser(locals())
        p.str('x')
        p._process_command_line(['--x', 'hi'])
        self.assertTrue('x' in locals())

        p = Parser()
        p.str('x')
        vals = p._process_command_line(['--x=5'])
        self.assertEqual(vals['x'], '5')
Exemplo n.º 11
0
    def test_url(self):
        p = Parser()
        p.url('url').required()
        self.assertRaises(MissingRequiredArgumentError, p._process_command_line)
        vals = p._process_command_line(['--url', 'http://www.com'])

        p = Parser()
        p.url('url')
        self.assertRaises(FormatError, p._process_command_line, ['--url', '/www.com'])
Exemplo n.º 12
0
    def test_basic(self):
        p = Parser()
        p.str('x')
        p.flag('y')
        vals = p._process_command_line([])
        self.assertTrue(vals['x'] is None)
#        self.assertEqual(vals['x'], x)
#        self.assertEqual(vals['y'], y)
        self.assertFalse(vals['y'])
Exemplo n.º 13
0
    def test_env(self):
        import os

        os.environ['PORT'] = 'yes'
        p = Parser()
        p.int('port').environment()

        self.assertRaises(FormatError, p._process_command_line)

        os.environ['PORT'] = '9999'

        for port in ('Port', 'pORt', 'port', 'PORT'):
            p = Parser()
            p.int(port).environment()
            vals = p._process_command_line()
            self.assertEqual(vals[port], 9999)

            vals = p._process_command_line(['--%s' % port, '2222'])
            self.assertEqual(vals[port], 2222)
Exemplo n.º 14
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.º 15
0
    def test_one_required(self):
        def create():
            p = Parser()
            p.require_one(
                p.str('x'),
                p.str('y'),
                p.str('z')
            )

            return p

        create()._process_command_line(['--x', '1'])
        create()._process_command_line(['--y', '1'])
        create()._process_command_line(['--z', '1'])

        self.assertRaises(ConflictError, create()._process_command_line, ['--x', '1', '--y', '1'])
        self.assertRaises(ArgumentError, create()._process_command_line, [])

        p = Parser()
        p.int('a')
        p.int('b')
        p.require_one('a', 'b')
        p._process_command_line(['--b', '3'])
Exemplo n.º 16
0
    def test_required(self):
        p = Parser()
        p.str('x').required()
        self.assertRaises(MissingRequiredArgumentError, p._process_command_line, [])

        p = Parser()
        p.str('x').required()
        self.assertRaises(MissingRequiredArgumentError,
                p._process_command_line, [])

        p = Parser()
        y = p.str('y')
        z = p.str('z')
        x = p.str('x').unless(y.or_(z))
        self.assertTrue(x != None)
        self.assertRaises(ArgumentError,
                p._process_command_line, [])
        p._process_command_line(['--y', 'a'])
        p._process_command_line(['--x', 'a'])
        p._process_command_line(['--x', 'a', '--y', 'b'])
        p._process_command_line(['--z', 'a'])
Exemplo n.º 17
0
    def test_flag2(self):
        p = Parser()
        p.flag('x').requires(p.int('y'))

        p._process_command_line()
        self.assertRaises(DependencyError, p._process_command_line, ['--x'])
Exemplo n.º 18
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')