本文整理汇总了Python中blargs.Parser.int方法的典型用法代码示例。如果您正苦于以下问题:Python Parser.int方法的具体用法?Python Parser.int怎么用?Python Parser.int使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类blargs.Parser
的用法示例。
在下文中一共展示了Parser.int方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_errors
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
def test_errors(self):
p = Parser()
p.int('a')
try:
p._process_command_line([])
except TypeError:
self.fail()
示例2: create
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
def create():
p = Parser()
a = p.int('a')
b = p.int('b')
p.int('c').requires(a.or_(b))
p.int('d').requires(a.and_(b))
return p
示例3: test_requires_and_default
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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'])
示例4: test_default
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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)
示例5: test_requires
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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'])
示例6: test_help
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
def test_help(self):
p = Parser()
p.int('a').described_as('a fun variable')
p.float('b').described_as('yet another a fun variable')
p.float('c').described_as('')
p.float('d').required()
p['a'].requires('b')
p['b'].conflicts('a', 'c')
p['b'].requires('b')
p._sys_exit_error = FakeSystemExit
p.out = StringIO()
self.assertRaises(FakeSystemExit, p._process_command_line, ['--help'])
self.assertEqual(p.out.getvalue(), 'Usage: %s [--a <int>] [--c <float>] [--b <float>] [--help/-h] [--d <float>]\nOptions: (! denotes required argument)\n --a <int> a fun variable Requires --b\n --c <float> \n --b <float> yet another a fun variable Conflicts with --a, --c Requires --b\n --help/-h Print help message. \n !--d <float> \n' % sys.argv[0])
示例7: test_oo
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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'])
示例8: test_conflicts
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
def test_conflicts(self):
p = Parser()
p.int('x').conflicts(p['x'])
self.assertRaises(ConflictError, p._process_command_line, ['--x', '3'])
def create():
p = Parser()
p.int('x').conflicts(p.int('y'))
return p
self.assertRaises(ConflictError, create()._process_command_line, specify('x', 'y'))
create()._process_command_line(specify('y'))
try:
create()._process_command_line(specify('x'))
except:
self.assertTrue(False)
示例9: test_env
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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)
示例10: test_one_required
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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'])
示例11: test_cast
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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)
示例12: test_config
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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')
示例13: test_missing
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
def test_missing(self):
p = Parser()
p.int('x')
self.assertRaises(MissingValueError, p._process_command_line, ['--x'])
示例14: test_flag2
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
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'])
示例15: test_shorthand
# 需要导入模块: from blargs import Parser [as 别名]
# 或者: from blargs.Parser import int [as 别名]
def test_shorthand(self):
p = Parser()
aa = p.int('aa').shorthand('a')
self.assertRaises(ValueError, lambda x: p.int('ab').shorthand(x), 'a')
self.assertRaises(ValueError, p.int('bb').shorthand, 'a')
self.assertRaises(ValueError, aa.shorthand, 'a')