Example #1
0
 def unfilled_posargs(self):
     p = self._parser(
         (
             Argument("foo", optional=True),
             Argument("bar", positional=True),
         )
     )
     self._test_for_ambiguity("--foo uhoh", p)
Example #2
0
 def setup(self):
     self.c = Context(
         args=(
             Argument("foo"),
             Argument(names=("bar", "biz")),
             Argument("baz", attr_name="wat"),
         )
     )
Example #3
0
 def valid_argument_is_NOT_ambiguous(self):
     # The one exception that proves the rule?
     self._parser((Argument("foo", optional=True), Argument("bar")))
     for form in ("--bar barval", "--bar=barval"):
         result = self._parse("--foo {}".format(form))
         assert len(result) == 1
         args = result[0].args
         assert args["foo"].value is True
         assert args["bar"].value == "barval"
Example #4
0
 def returned_arguments_not_given_contain_default_values(self):
     # I.e. a Context with args A and B, invoked with no mention of B,
     # should result in B existing in the result, with its default value
     # intact, and not e.g. None, or the arg not existing.
     a = Argument("name", kind=str)
     b = Argument("age", default=7)
     c = Context("mytask", args=(a, b))
     Parser((c,)).parse_argv(["mytask", "--name", "blah"])
     assert c.args["age"].value == 7
Example #5
0
 def arguments_which_take_values_get_defaults_overridden_correctly(
     self
 ):  # noqa
     args = (Argument("arg", kind=str), Argument("arg2", kind=int))
     c = Context("mytask", args=args)
     argv = ["mytask", "--arg", "myval", "--arg2", "25"]
     result = Parser((c,)).parse_argv(argv)
     assert result[0].args["arg"].value == "myval"
     assert result[0].args["arg2"].value == 25
Example #6
0
 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
     assert a.foo.value is True
     assert a.bar.value is True
Example #7
0
 def represents_positional_args_missing_values(self):
     arg1 = Argument("arg1", positional=True)
     arg2 = Argument("arg2", positional=False)
     arg3 = Argument("arg3", positional=True)
     c = Context(name="foo", args=(arg1, arg2, arg3))
     assert c.missing_positional_args == [arg1, arg3]
     c.positional_args[0].value = "wat"
     assert c.missing_positional_args == [arg3]
     c.positional_args[1].value = "hrm"
     assert c.missing_positional_args == []
Example #8
0
 def omitted_positional_args_raises_ParseError(self):
     try:
         arg = Argument("pos", positional=True)
         arg2 = Argument("morepos", positional=True)
         mytask = Context(name="mytask", args=[arg, arg2])
         Parser(contexts=[mytask]).parse_argv(["mytask"])
     except ParseError as e:
         expected = "'mytask' did not receive required positional arguments: 'pos', 'morepos'"  # noqa
         assert str(e) == expected
     else:
         assert False, "Did not raise ParseError!"
Example #9
0
 def no_ambiguity_if_option_val_already_given(self):
     p = self._parser(
         (
             Argument("foo", optional=True),
             Argument("bar", kind=bool),
         )
     )
     # This should NOT raise a ParseError.
     result = self._parse("--foo hello --bar", p)
     assert result[0].args["foo"].value == "hello"
     assert result[0].args["bar"].value is True
Example #10
0
 def valid_flaglike_argument_is_NOT_ambiguous(self):
     # The OTHER exception that proves the rule?
     self._parser(
         (
             Argument("foo", optional=True),
             Argument("bar", kind=bool),
         )
     )
     result = self._parse("--foo --bar")
     assert len(result) == 1
     args = result[0].args
     assert args["foo"].value is True
     assert args["bar"].value is True
Example #11
0
        def setup(self):
            # Normal, non-task/collection related Context
            self.vanilla = Context(
                args=(Argument("foo"), Argument("bar", help="bar the baz"))
            )
            # Task/Collection generated Context
            # (will expose flags n such)
            @task(help={"otherarg": "other help"}, optional=["optval"])
            def mytask(c, myarg, otherarg, optval, intval=5):
                pass

            col = Collection(mytask)
            self.tasked = col.to_contexts()[0]
Example #12
0
 def positional_args_eat_otherwise_valid_context_names(self):
     mytask = Context(
         "mytask",
         args=[
             Argument("pos", positional=True),
             Argument("nonpos", default="default"),
         ],
     )
     Context("lolwut")
     result = Parser([mytask]).parse_argv(["mytask", "lolwut"])
     r = result[0]
     assert r.args["pos"].value == "lolwut"
     assert r.args["nonpos"].value == "default"
     assert len(result) == 1  # Not 2
Example #13
0
 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"
Example #14
0
 def iterables_work_correctly_outside_a_vacuum(self):
     # Undetected bug where I was primarily focused on the -vvv use
     # case...'normal' incrementables never left 'waiting for value'
     # state in the parser! so _subsequent_ task names & such never got
     # parsed right, always got appended to the list.
     c = Context("mytask", args=[Argument("mylist", kind=list)])
     c2 = Context("othertask")
     argv = [
         "mytask",
         "--mylist",
         "val",
         "--mylist",
         "val2",
         "othertask",
     ]
     result = Parser([c, c2]).parse_argv(argv)
     # When bug present, result only has one context (for 'mytask') and
     # its 'mylist' consists of ['val', 'val2', 'othertask']. (the
     # middle '--mylist' was handled semi-correctly.)
     mylist = result[0].args.mylist.value
     assert mylist == ["val", "val2"]
     contexts = len(result)
     err = "Got {} parse context results instead of 2!".format(contexts)
     assert contexts == 2, err
     assert result[1].name == "othertask"
Example #15
0
 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"
Example #16
0
 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"
Example #17
0
 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
Example #18
0
 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"])
Example #19
0
 def _parser(self, arguments=None):
     if arguments is None:
         arguments = (
             Argument(
                 names=("foo", "f"), optional=True, default="mydefault"
             ),
         )
     self.context = Context("mytask", args=arguments)
     self.parser = Parser([self.context])
     return self.parser
Example #20
0
 def list_kind_triggers_append_instead_of_overwrite(self):
     # TODO: when put this way it makes the API look pretty strange;
     # maybe a sign we should switch to explicit setter methods
     # (selected on kind, perhaps) instead of using an implicit setter
     a = Argument("mylist", kind=list)
     assert a.value == []
     a.value = "val1"
     assert a.value == ["val1"]
     a.value = "val2"
     assert a.value == ["val1", "val2"]
Example #21
0
 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])
     assert mytask.positional_args == [pos1, pos2]
     r = Parser([mytask]).parse_argv(
         [
             "mytask",
             "--nonpos",
             "wut",
             "--pos2",
             "pos2val",
             "pos1val",
         ]
     )[0]
     assert r.args["pos1"].value == "pos1val"
     assert r.args["pos2"].value == "pos2val"
     assert r.args["nonpos"].value == "wut"
Example #22
0
 def incrementable_True_triggers_increment_of_default(self):
     a = Argument("verbose", kind=int, default=0, incrementable=True)
     assert a.value == 0
     # NOTE: parser currently just goes "Argument.takes_value is false?
     # Gonna stuff True/False in there." So this looks pretty silly out
     # of context (as with list-types above.)
     a.value = True
     assert a.value == 1
     for _ in range(4):
         a.value = True
     assert a.value == 5
Example #23
0
 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
Example #24
0
 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"
Example #25
0
 def can_take_Argument_instance(self):
     a = Argument(names=("foo",))
     self.c.add_arg(a)
     assert self.c.args["foo"] is a
Example #26
0
 def args_show_as_repr(self):
     string = str(Context("bar", args=[Argument("arg1")]))
     assert (
         string == "<parser/Context 'bar': {'arg1': <Argument: arg1>}>"
     )  # noqa
Example #27
0
 def _assert_order(self, name_tuples, expected_flag_order):
     c = Context(args=[Argument(names=x) for x in name_tuples])
     expected = [c.help_for(x) for x in expected_flag_order]
     assert c.help_tuples() == expected
Example #28
0
 def true_default_args(self):
     c = Context(args=(Argument("truthy", kind=bool, default=True),))
     assert c.help_for("--truthy") == ("--[no_]truthy", "")
Example #29
0
 def underscored_args(self):
     c = Context(args=(Argument("i_have_underscores", help="yup"),))
     result = c.help_for("--i_have_underscores")
     assert result == ("--i_have_underscores=STRING", "yup")
Example #30
0
 def may_give_arg_list_at_init_time(self):
     a1 = Argument("foo")
     a2 = Argument("bar")
     c = Context(name="name", args=(a1, a2))
     assert c.args["foo"] is a1