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 other_tokens_afterwards_raise_parse_errors(self): # NOTE: this is because of the special-casing where we supply # the task name as the value when the flag is literally named # "help". task1 = Context("mytask") init = Context(args=[Argument("help", optional=True)]) parser = Parser(initial=init, contexts=[task1]) with raises(ParseError, match=r".*foobar.*"): parser.parse_argv(["mytask", "--help", "foobar"])
def value_requiring_core_flags_also_work_correctly(self): "value-requiring core flags also work correctly" initial = Context(args=[Argument("hide")]) task1 = Context("mytask") parser = Parser(initial=initial, contexts=[task1]) result = parser.parse_argv(["mytask", "--hide", "both"]) assert result[0].args.hide.value == "both"
def by_itself_base_case(self): task1 = Context("mytask") init = Context(args=[Argument("help", optional=True)]) parser = Parser(initial=init, contexts=[task1]) result = parser.parse_argv(["mytask", "--help"]) assert len(result) == 2 assert result[0].args.help.value == "mytask" assert "help" not in result[1].args
def core_flags_work_normally_when_no_conflict(self): # Initial parse context with an --echo, plus a no-args task initial = Context(args=[self._echo()]) task1 = Context("mytask") parser = Parser(initial=initial, contexts=[task1]) # Call with --echo in the per-task context, expect the core # context got updated (vs an error) result = parser.parse_argv(["mytask", "--echo"]) assert result[0].args.echo.value is True
def when_conflict_per_task_args_win_out(self): # Initial parse context with an --echo, plus task w/ same initial = Context(args=[self._echo()]) task1 = Context("mytask", args=[self._echo()]) parser = Parser(initial=initial, contexts=[task1]) # Call with --echo in the per-task context, expect the task # context got updated, and not core. result = parser.parse_argv(["mytask", "--echo"]) assert result[0].args.echo.value is False assert result[1].args.echo.value is True
class parsing_errors: def setup(self): self.p = Parser([Context(name="foo", args=[Argument("bar")])]) def missing_flag_values_raise_ParseError(self): with raises(ParseError): self.p.parse_argv(["foo", "--bar"]) def attaches_context_to_ParseErrors(self): try: self.p.parse_argv(["foo", "--bar"]) except ParseError as e: assert e.context is not None def attached_context_is_None_outside_contexts(self): try: Parser().parse_argv(["wat"]) except ParseError as e: assert e.context is None
def core_bool_but_per_task_string(self): # Initial parse context with bool --hide, and a task with a # regular (string) --hide initial = Context( args=[Argument("hide", kind=bool, default=False)] ) task1 = Context("mytask", args=[Argument("hide")]) parser = Parser(initial=initial, contexts=[task1]) # Expect that, because the task's version wins, we're able to # call it with a value. (If there were weird bugs where the # core flag informed the parsing, this would fail.) result = parser.parse_argv(["mytask", "--hide", "both"]) assert result[0].args.hide.value is False assert result[1].args.hide.value == "both"
def clones_noninitial_contexts(self): a = Argument("foo") assert a.value is None c = Context(name="mytask", args=(a,)) p = Parser(contexts=(c,)) assert p.contexts["mytask"] is c r = p.parse_argv(["mytask", "--foo", "val"]) assert p.contexts["mytask"] is c c2 = r[0] assert c2 is not c a2 = c2.args["foo"] assert a2 is not a assert a.value is None assert a2.value == "val"
def clones_initial_context(self): a = Argument("foo", kind=bool) assert a.value is None c = Context(args=(a,)) p = Parser(initial=c) assert p.initial is c r = p.parse_argv(["--foo"]) assert p.initial is c c2 = r[0] assert c2 is not c a2 = c2.args["foo"] assert a2 is not a assert a.value is None assert a2.value is True
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"]