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, '+')
def create(): p = Parser({}) p.config('a') p.int('b') p.str('c') p.int('d') return p
def test_with(self): import sys sys.argv[1:] = ['--x', 'yes'] d = {'test_x': None} p = Parser(d) with p: p.str('x').shorthand('test-x')
def create(): p = Parser() p.str('x').requires( p.str('y'), p.str('z'), p.str('w') ) return p
def create(): p = Parser() p.require_one( p.str('x'), p.str('y'), p.str('z') ) return p
def create(): p = Parser() p.at_least_one( p.str('x'), p.str('y'), p.str('z') ) return p
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'])
def test_requires(self): p = Parser() r = p.str('x') self.assertRaises(ValueError, r.requires, 'y') try: r.requires(p.str('y')) except ValueError: self.fail() p = Parser() y = p.int('y') p.int('x').requires(y) self.assertRaises(DependencyError, p._process_command_line, ['--x', '5'])
def test_multiple(self): p = Parser() p.str('x') self.assertRaises(MultipleSpecifiedArgumentError, p._process_command_line, ['--x', '1', '--x', '2']) def create(): p = Parser() x = p.int('x').multiple() self.assertTrue(x != None) return p vals = create()._process_command_line(['--x', '1', '--x', '2']) self.assertEqual(sorted(vals['x']), [1, 2]) vals = create()._process_command_line(['--x', '1']) self.assertEqual(vals['x'], [1]) vals = create()._process_command_line([]) self.assertEqual(vals['x'], [None]) self.assertRaises(FormatError, create()._process_command_line, ['--x', '1', '--x', 'hello'])
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)
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')
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'])
def create(): p = Parser({}) p.config('a').default(fname) p.int('b').requires(p.int('d')) p.str('c') return p
def test_bad_format(self): p = Parser() p.str('f') p.str('m')
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)
def create(): p = Parser() p.multiword('aa') p.str('ab') return p
def create(): p = Parser() p.str('a').requires(p['a'] != 'c') return p