Example #1
0
 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"])
Example #2
0
 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"])
Example #3
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 #4
0
 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'])
Example #5
0
 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'])
Example #6
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 #7
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 #8
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 #9
0
 def task_has_no_help_shows_per_task_help(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 #10
0
 def task_has_no_help_shows_per_task_help(self):
     task1 = Context('mytask')
     init = Context(args=[Argument('help', optional=True)])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(['mytask', '--help'])
     eq_(len(result), 2)
     eq_(result[0].args.help.value, 'mytask')
     ok_('help' not in result[1].args)
Example #11
0
 def flag_values_not_parsed_as_flags(self):
     a = Argument('foo', default=False, optional=True)
     b = Argument('bar', default='')
     c = Context(name='mytask', args=(a, b))
     p = Parser(contexts=(c,))
     r = p.parse_argv(['mytask', '--bar=--foo'])
     assert not r[0].args['foo'].value
     eq_(r[0].args['bar'].value, '--foo')
Example #12
0
 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
Example #13
0
 def task_has_no_h_shortflag_shows_per_task_help(self):
     task1 = Context("mytask")
     arg = Argument(names=("help", "h"), optional=True)
     init = Context(args=[arg])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(["mytask", "-h"])
     assert len(result) == 2
     assert result[0].args.help.value == "mytask"
     assert "help" not in result[1].args
Example #14
0
 def task_has_no_h_shortflag_shows_per_task_help(self):
     task1 = Context('mytask')
     arg = Argument(names=('help', 'h'), optional=True)
     init = Context(args=[arg])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(['mytask', '-h'])
     eq_(len(result), 2)
     eq_(result[0].args.help.value, 'mytask')
     ok_('help' not in result[1].args)
Example #15
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 #16
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 #17
0
 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
Example #18
0
 def task_has_no_h_shortflag_shows_per_task_help(self):
     task1 = Context('mytask')
     arg = Argument(names=('help', 'h'), optional=True)
     init = Context(args=[arg])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(['mytask', '-h'])
     eq_(len(result), 2)
     eq_(result[0].args.help.value, 'mytask')
     ok_('help' not in result[1].args)
 def task_has_no_h_shortflag_shows_per_task_help(self):
     task1 = Context("mytask")
     arg = Argument(names=("help", "h"), optional=True)
     init = Context(args=[arg])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(["mytask", "-h"])
     assert len(result) == 2
     assert result[0].args.help.value == "mytask"
     assert "help" not in result[1].args
Example #20
0
 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
Example #21
0
 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
Example #22
0
def _parse(argstr, parser=None, collection=None):
    # TODO: this will clearly turn into the actual main cli stub sometime.
    # TODO: Replace with actual objects at that time.
    # Set up a parser
    if parser is None:
        parser = Parser()
    # Naive string-to-argv: split on whitespace.
    # It's probably safe to assume most tests won't be using spaces in flag
    # values and stuff (e.g. "invoke --foo='biz baz'")
    argv = argstr.split()
    return parser.parse_argv(argv)
Example #23
0
        class parsing_errors:
            def setup(self):
                self.p = Parser([Context(name='foo', args=[Argument('bar')])])

            @raises(ParseError)
            def missing_flag_values_raise_ParseError(self):
                self.p.parse_argv(['foo', '--bar'])

            def attaches_context_to_ParseErrors(self):
                try:
                    self.p.parse_argv(['foo', '--bar'])
                except ParseError, e:
                    assert e.context is not None
Example #24
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 #25
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 #26
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 #27
0
 def clones_initial_context(self):
     a = Argument('foo', kind=bool)
     eq_(a.value, 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
     eq_(a.value, None)
     eq_(a2.value, True)
Example #28
0
 def clones_initial_context(self):
     a = Argument('foo', kind=bool)
     eq_(a.value, 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
     eq_(a.value, None)
     eq_(a2.value, True)
Example #29
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 #30
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 #31
0
 def clones_noninitial_contexts(self):
     a = Argument('foo')
     eq_(a.value, 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
     eq_(a.value, None)
     eq_(a2.value, 'val')
Example #32
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 #33
0
 def clones_noninitial_contexts(self):
     a = Argument('foo')
     eq_(a.value, 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
     eq_(a.value, None)
     eq_(a2.value, 'val')
Example #34
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 #35
0
 def always_includes_initial_context_if_one_was_given(self):
     # Even if no core/initial flags were seen
     t1 = Context('t1')
     init = Context()
     result = Parser((t1, ), initial=init).parse_argv(['t1'])
     eq_(result[0].name, None)
     eq_(result[1].name, 't1')
Example #36
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)
     eq_(result[0].args['arg'].value, 'myval')
     eq_(result[0].args['arg2'].value, 25)
Example #37
0
 def task_args_work_correctly(self):
     task1 = Context('mytask', args=(Argument('meh'),))
     result = Parser((task1,)).parse_argv(
         ['mytask', '--meh', 'mehval1', 'mytask', '--meh', 'mehval2']
     )
     eq_(result[0].args.meh.value, 'mehval1')
     eq_(result[1].args.meh.value, 'mehval2')
Example #38
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)
     return Parser([self.context])
Example #39
0
 def always_includes_initial_context_if_one_was_given(self):
     # Even if no core/initial flags were seen
     t1 = Context("t1")
     init = Context()
     result = Parser((t1,), initial=init).parse_argv(["t1"])
     assert result[0].name is None
     assert result[1].name == "t1"
Example #40
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 #41
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
     eq_(a.foo.value, True)
     eq_(a.bar.value, True)
Example #42
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
 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 #44
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 #45
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 #46
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'])
     eq_(c.args['age'].value, 7)
Example #47
0
        class parsing_errors:
            def setup(self):
                self.p = Parser([Context(name='foo', args=[Argument('bar')])])

            @raises(ParseError)
            def missing_flag_values_raise_ParseError(self):
                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
Example #48
0
        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
Example #49
0
        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
Example #50
0
class ParseResult_(Spec):
    "ParseResult"
    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 acts_as_a_list_of_parsed_contexts(self):
        eq_(len(self.result), 1)
        eq_(self.result[0].name, 'mytask')

    def exhibits_remainder_attribute(self):
        eq_(self.result.remainder, 'my remainder')

    def to_dict_returns_parsed_contexts_and_args_as_nested_dicts(self):
        eq_(
            self.result.to_dict(),
            {'mytask': {'foo': 'foo-val', 'bar': None}}
        )
Example #51
0
 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'])
Example #52
0
 def per_task_flag_wins_over_core_flag(self):
     task1 = Context("mytask", args=[Argument("help")])
     init = Context(args=[Argument("help", optional=True)])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(["mytask", "--help", "foo"])
     assert result[1].args.help.value == "foo"
Example #53
0
 def setup(self):
     self.p = Parser([Context(name='foo', args=[Argument('bar')])])
Example #54
0
 def per_task_flag_wins_over_core_flag(self):
     task1 = Context('mytask', args=[Argument('help')])
     init = Context(args=[Argument('help', optional=True)])
     parser = Parser(initial=init, contexts=[task1])
     result = parser.parse_argv(['mytask', '--help', 'foo'])
     assert result[1].args.help.value == 'foo'
Example #55
0
 def setup(self):
     self.p = Parser([Context(name="foo", args=[Argument("bar")])])
Example #56
0
 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"]
Example #57
0
 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)