def _parse(self, *args): c = Context("mytask", args=(Argument("mylist", kind=list), )) argv = ["mytask"] + list(args) return Parser([c]).parse_argv(argv)[0].args.mylist.value
def returns_only_contexts_mentioned(self): task1 = Context('mytask') task2 = Context('othertask') result = Parser((task1, task2)).parse_argv(['othertask']) eq_(len(result), 1) eq_(result[0].name, 'othertask')
def returned_contexts_are_in_order_given(self): t1, t2 = Context('t1'), Context('t2') r = Parser((t1, t2)).parse_argv(['t2', 't1']) eq_([x.name for x in r], ['t2', 't1'])
def setup(self): self.context = Context('mytask', args=(Argument('foo', kind=str), Argument('bar'))) argv = ['mytask', '--foo', 'foo-val', '--', 'my', 'remainder'] self.result = Parser((self.context,)).parse_argv(argv)
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 raises_ValueError_for_unnamed_Contexts_in_contexts(self): Parser(initial=Context(), contexts=[Context()])
def task_name(self): # mytask --foo myothertask c1 = Context('mytask', args=(Argument('foo', optional=True),)) c2 = Context('othertask') p = Parser([c1, c2]) self._test_for_ambiguity("--foo othertask", p)
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 ignore_unknown_does_not_mutate_rest_of_argv(self): p = Parser([Context("ugh")], ignore_unknown=True) r = p.parse_argv(["ugh", "what", "-nowai"]) # NOT: ['what', '-n', '-w', '-a', '-i'] assert r.unparsed == ["what", "-nowai"]
def setup(self): self.context = Context("mytask", args=(Argument("foo", kind=str), Argument("bar"))) argv = ["mytask", "--foo", "foo-val", "--", "my", "remainder"] self.result = Parser((self.context, )).parse_argv(argv)
def can_take_initial_context(self): c = Context() p = Parser(initial=c) assert p.initial == c
def task_args_work_correctly(self): task1 = Context("mytask", args=(Argument("meh"), )) result = Parser((task1, )).parse_argv( ["mytask", "--meh", "mehval1", "mytask", "--meh", "mehval2"]) assert result[0].args.meh.value == "mehval1" assert result[1].args.meh.value == "mehval2"
def is_happy_to_handle_same_task_multiple_times(self): task1 = Context("mytask") result = Parser((task1, )).parse_argv(["mytask", "mytask"]) assert len(result) == 2 for x in result: assert x.name == "mytask"
def raises_error_for_context_name_and_alias_clashes(self): # I.e. inverse of the above, which is a different code path. with raises(ValueError): Parser((Context("foo"), Context("bar", aliases=("foo", ))))
def can_take_just_contexts_as_non_keyword_arg(self): c = Context('foo') p = Parser([c]) eq_(p.contexts['foo'], c)
def returned_contexts_are_in_order_given(self): t1, t2 = Context("t1"), Context("t2") r = Parser((t1, t2)).parse_argv(["t2", "t1"]) assert [x.name for x in r] == ["t2", "t1"]
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 inverse_bools_get_set_correctly(self): arg = Argument('myarg', kind=bool, default=True) c = Context('mytask', args=(arg,)) r = Parser((c,)).parse_argv(['mytask', '--no-myarg']) eq_(r[0].args['myarg'].value, False)
def raises_error_for_context_name_clashes(self): Parser(contexts=(Context('foo'), Context('foo')))
def can_take_initial_and_other_contexts(self): c1 = Context('foo') c2 = Context('bar') p = Parser(initial=Context(), contexts=[c1, c2]) eq_(p.contexts['foo'], c1) eq_(p.contexts['bar'], c2)
def is_happy_to_handle_same_task_multiple_times(self): task1 = Context('mytask') result = Parser((task1,)).parse_argv(['mytask', 'mytask']) eq_(len(result), 2) [eq_(x.name, 'mytask') for x in result]
def returns_remainder(self): "returns -- style remainder string chunk" r = Parser((Context('foo'),)).parse_argv( ['foo', '--', 'bar', 'biz'] ) eq_(r.remainder, "bar biz")
def raises_error_for_context_alias_and_name_clashes(self): Parser((Context('foo', aliases=('bar',)), Context('bar')))
def setup(self): self.p = Parser([Context(name='foo', args=[Argument('bar')])])
def can_take_initial_context(self): c = Context() p = Parser(initial=c) eq_(p.initial, c)
def can_take_just_other_contexts(self): c = Context('foo') p = Parser(contexts=[c]) eq_(p.contexts['foo'], c)
def ignore_unknown_does_not_mutate_rest_of_argv(self): p = Parser([Context('ugh')], ignore_unknown=True) r = p.parse_argv(['ugh', 'what', '-nowai']) # NOT: ['what', '-n', '-w', '-a', '-i'] eq_(r.unparsed, ['what', '-nowai'])
def _basic(self): arg = Argument('pos', positional=True) mytask = Context(name='mytask', args=[arg]) return Parser(contexts=[mytask])
def returned_context_member_arguments_contain_given_values(self): c = Context('mytask', args=(Argument('boolean', kind=bool), )) result = Parser((c, )).parse_argv(['mytask', '--boolean']) eq_(result[0].args['boolean'].value, True)
def raises_error_for_context_alias_and_name_clashes(self): with raises(ValueError): Parser((Context("foo", aliases=("bar", )), Context("bar")))