本文整理汇总了Python中blargs.Parser类的典型用法代码示例。如果您正苦于以下问题:Python Parser类的具体用法?Python Parser怎么用?Python Parser使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_conditions
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'])
示例2: test_errors
def test_errors(self):
p = Parser()
p.int('a')
try:
p._process_command_line([])
except TypeError:
self.fail()
示例3: test_unspecified
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))
示例4: test_with
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')
示例5: create
def create():
p = Parser()
p.all_if_any(
p.int('x'),
p.int('y'),
p.int('z')
)
return p
示例6: test_localize
def test_localize(self):
p = Parser.with_locals()
p.str('multi-word').requires(p.str('another-multi-word'))
vals = p._process_command_line(['--multi-word', 'a', '--another-multi-word', 'b'])
self.assertTrue('multi_word' in vals)
self.assertFalse('multi-word' in vals)
self.assertTrue('multi_word' in locals())
示例7: test_conflicts
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)
示例8: test_default
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)
示例9: test_flag
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'])
示例10: test_url
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'])
示例11: test_multiple
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'])
示例12: test_requires_and_default
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'])
示例13: test_basic
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'])
示例14: test_requires
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'])
示例15: test_env
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)