Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def returns_only_contexts_mentioned(self):
task1 = Context("mytask")
task2 = Context("othertask")
result = Parser((task1, task2)).parse_argv(["othertask"])
assert len(result) == 1
assert result[0].name == "othertask"
def handles_multiple_boolean_flags_per_context(self):
c = Context('mytask', args=(
Argument('foo', kind=bool), Argument('bar', kind=bool)
))
r = Parser([c]).parse_argv(['mytask', '--foo', '--bar'])
a = r[0].args
eq_(a.foo.value, True)
eq_(a.bar.value, True)
def raises_error_for_context_name_and_alias_clashes(self):
# I.e. inverse of the above, which is a different code path.
Parser((Context('foo'), Context('bar', aliases=('foo',))))
def positional_args_can_still_be_given_as_flags(self):
# AKA "positional args can come anywhere in the context"
pos1 = Argument('pos1', positional=True)
pos2 = Argument('pos2', positional=True)
nonpos = Argument('nonpos', positional=False, default='lol')
mytask = Context('mytask', args=[pos1, pos2, nonpos])
eq_(mytask.positional_args, [pos1, pos2])
r = Parser([mytask]).parse_argv([
'mytask',
'--nonpos', 'wut',
'--pos2', 'pos2val',
'pos1val',
])[0]
eq_(r.args['pos1'].value, 'pos1val')
eq_(r.args['pos2'].value, 'pos2val')
eq_(r.args['nonpos'].value, 'wut')
def parses_sys_argv_style_list_of_strings(self):
"parses sys.argv-style list of strings"
# Doesn't-blow-up tests FTL
mytask = Context(name="mytask")
mytask.add_arg("arg")
p = Parser(contexts=[mytask])
p.parse_argv(["mytask", "--arg", "value"])
def raises_error_for_context_name_clashes(self):
with raises(ValueError):
Parser(contexts=(Context("foo"), Context("foo")))
def _compare(self, argname, invoke, value):
c = Context('mytask', args=(Argument(argname, kind=str),))
r = Parser((c,)).parse_argv(['mytask', invoke])
eq_(r[0].args[argname].value, value)
def unparsed_does_not_share_state(self):
r = Parser(ignore_unknown=True).parse_argv(['self'])
eq_(r.unparsed, ['self'])
r2 = Parser(ignore_unknown=True).parse_argv(['contained'])
eq_(r.unparsed, ['self']) # NOT ['self', 'contained']
eq_(r2.unparsed, ['contained']) # NOT ['self', 'contained']
Argument(
names=('version', 'V'),
kind=bool,
default=False,
help="Show version and exit."
),
Argument(
names=('warn-only', 'w'),
kind=bool,
default=False,
help="Warn, instead of failing, when shell commands fail.",
),
))
# 'core' will result an .unparsed attribute with what was left over.
debug("Parsing initial context (core args)")
parser = Parser(initial=initial_context, ignore_unknown=True)
core = parse_gracefully(parser, argv[1:])
debug("After core-args pass, leftover argv: {0!r}".format(core.unparsed))
args = core[0].args
# Enable debugging from here on out, if debug flag was given.
if args.debug.value:
enable_logging()
# Print version & exit if necessary
if args.version.value:
if version:
print(version)
else:
print("Invoke {0}".format(__version__))
raise Exit