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 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]
     eq_(r.args['pos'].value, 'lolwut')
     eq_(r.args['nonpos'].value, 'default')
     eq_(len(result), 1)  # Not 2
Example #4
0
    class help_for:
        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(myarg, otherarg, optval):
                pass

            col = Collection(mytask)
            self.tasked = col.to_contexts()[0]

        @raises(ValueError)
        def raises_ValueError_for_non_flag_values(self):
            self.vanilla.help_for('foo')

        def vanilla_no_helpstr(self):
            eq_(self.vanilla.help_for('--foo'), ("--foo=STRING", ""))

        def vanilla_with_helpstr(self):
            eq_(self.vanilla.help_for('--bar'),
                ("--bar=STRING", "bar the baz"))

        def task_driven_with_helpstr(self):
            eq_(self.tasked.help_for('--otherarg'),
                ("-o STRING, --otherarg=STRING", "other help"))

        # Yes, the next 3 tests are identical in form, but technically they
        # test different behaviors. HERPIN' AN' DERPIN'
        def task_driven_no_helpstr(self):
            eq_(self.tasked.help_for('--myarg'),
                ("-m STRING, --myarg=STRING", ""))

        def short_form_before_long_form(self):
            eq_(self.tasked.help_for('--myarg'),
                ("-m STRING, --myarg=STRING", ""))

        def equals_sign_for_long_form_only(self):
            eq_(self.tasked.help_for('--myarg'),
                ("-m STRING, --myarg=STRING", ""))

        def kind_to_placeholder_map(self):
            # str=STRING, int=INT, etc etc
            skip()

        def shortflag_inputs_work_too(self):
            eq_(self.tasked.help_for('-m'), self.tasked.help_for('--myarg'))

        def optional_values_use_brackets(self):
            eq_(self.tasked.help_for('--optval'),
                ("-p [STRING], --optval[=STRING]", ""))
Example #5
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 #6
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 #7
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 #8
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 #9
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 #10
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 #11
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 #12
0
 def setup(self):
     self.arg = Argument('--boolean')
     self.orig = Context(
         name='mytask',
         args=(self.arg,),
         aliases=('othername',)
     )
     self.new = copy.deepcopy(self.orig)
 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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
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 #23
0
 def represents_whether_all_positional_args_have_values(self):
     c = Context(name='foo', args=(
         Argument('arg1', positional=True),
         Argument('arg2', positional=False),
         Argument('arg3', positional=True),
     ))
     eq_(c.needs_positional_arg, True)
     c.positional_args[0].value = 'wat'
     eq_(c.needs_positional_arg, True)
     c.positional_args[1].value = 'hrm'
     eq_(c.needs_positional_arg, False)
Example #24
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 #25
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 #26
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 #27
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 #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 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 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 #31
0
 def setup(self):
     self.c = Context()
Example #32
0
 def setup(self):
     self.c = Context(args=(
         Argument('foo'),
         Argument(names=('bar', 'biz')),
         Argument('baz', attr_name='wat'),
     ))
Example #33
0
    class add_arg:
        def setup(self):
            self.c = Context()

        def can_take_Argument_instance(self):
            a = Argument(names=('foo', ))
            self.c.add_arg(a)
            assert self.c.args['foo'] is a

        def can_take_name_arg(self):
            self.c.add_arg('foo')
            assert 'foo' in self.c.args

        def can_take_kwargs_for_single_Argument(self):
            self.c.add_arg(names=('foo', 'bar'))
            assert 'foo' in self.c.args and 'bar' in self.c.args

        @raises(ValueError)
        def raises_ValueError_on_duplicate(self):
            self.c.add_arg(names=('foo', 'bar'))
            self.c.add_arg(name='bar')

        def adds_flaglike_name_to_dot_flags(self):
            "adds flaglike name to .flags"
            self.c.add_arg('foo')
            assert '--foo' in self.c.flags

        def adds_all_names_to_dot_flags(self):
            "adds all names to .flags"
            self.c.add_arg(names=('foo', 'bar'))
            assert '--foo' in self.c.flags
            assert '--bar' in self.c.flags

        def adds_true_bools_to_inverse_flags(self):
            self.c.add_arg(name='myflag', default=True, kind=bool)
            assert '--myflag' in self.c.flags
            assert '--no-myflag' in self.c.inverse_flags
            eq_(self.c.inverse_flags['--no-myflag'], '--myflag')

        def inverse_flags_works_right_with_task_driven_underscored_names(self):
            # Use a Task here instead of creating a raw argument, we're partly
            # testing Task.get_arguments()' transform of underscored names
            # here. Yes that makes this an integration test, but it's nice to
            # test it here at this level & not just in cli tests.
            @task
            def mytask(underscored_option=True):
                pass

            self.c.add_arg(mytask.get_arguments()[0])
            eq_(self.c.inverse_flags['--no-underscored-option'],
                '--underscored-option')

        def turns_single_character_names_into_short_flags(self):
            self.c.add_arg('f')
            assert '-f' in self.c.flags
            assert '--f' not in self.c.flags

        def adds_positional_args_to_positional_args(self):
            self.c.add_arg(name='pos', positional=True)
            eq_(self.c.positional_args[0].name, 'pos')

        def positional_args_empty_when_none_given(self):
            eq_(len(self.c.positional_args), 0)

        def positional_args_filled_in_order(self):
            self.c.add_arg(name='pos1', positional=True)
            eq_(self.c.positional_args[0].name, 'pos1')
            self.c.add_arg(name='abc', positional=True)
            eq_(self.c.positional_args[1].name, 'abc')

        def positional_arg_modifications_affect_args_copy(self):
            self.c.add_arg(name='hrm', positional=True)
            eq_(self.c.args['hrm'].value, self.c.positional_args[0].value)
            self.c.positional_args[0].value = 17
            eq_(self.c.args['hrm'].value, self.c.positional_args[0].value)
Example #34
0
 def setup(self):
     self.c = Context()
Example #35
0
 def _assert_order(self, name_tuples, expected_flag_order):
     ctx = Context(args=[Argument(names=x) for x in name_tuples])
     return eq_(
         ctx.help_tuples(),
         [ctx.help_for(x) for x in expected_flag_order]
     )
Example #36
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 #37
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 #38
0
 def can_take_kwargs(self):
     c = Context()
     c.add_arg(names=('foo', 'bar'))
     assert c.has_arg('foo') and c.has_arg('bar')
Example #39
0
 def can_take_name_arg(self):
     c = Context()
     c.add_arg('foo')
     assert c.has_arg('foo')
Example #40
0
 def returns_True_if_flag_is_valid_arg(self):
     c = Context()
     c.add_arg(Argument(names=('foo',)))
     eq_(c.has_arg('foo'), True)
Example #41
0
 def raises_ValueError_on_duplicate(self):
     c = Context()
     c.add_arg(names=('foo', 'bar'))
     c.add_arg(name='bar')
Example #42
0
 def returns_Argument_for_given_name(self):
     c = Context()
     a = Argument(name='foo')
     c.add_arg(a)
     assert c.get_arg('foo') is a
Example #43
0
 def _assert_order(self, name_tuples, expected_flag_order):
     ctx = Context(args=map(lambda x: Argument(names=x), name_tuples))
     return eq_(ctx.help_tuples(), map(ctx.help_for, expected_flag_order))
Example #44
0
    class add_arg:
        def setup(self):
            self.c = Context()

        def can_take_Argument_instance(self):
            a = Argument(names=('foo',))
            self.c.add_arg(a)
            assert self.c.args['foo'] is a

        def can_take_name_arg(self):
            self.c.add_arg('foo')
            assert 'foo' in self.c.args

        def can_take_kwargs_for_single_Argument(self):
            self.c.add_arg(names=('foo', 'bar'))
            assert 'foo' in self.c.args and 'bar' in self.c.args

        @raises(ValueError)
        def raises_ValueError_on_duplicate(self):
            self.c.add_arg(names=('foo', 'bar'))
            self.c.add_arg(name='bar')

        def adds_flaglike_name_to_dot_flags(self):
            "adds flaglike name to .flags"
            self.c.add_arg('foo')
            assert '--foo' in self.c.flags

        def adds_all_names_to_dot_flags(self):
            "adds all names to .flags"
            self.c.add_arg(names=('foo', 'bar'))
            assert '--foo' in self.c.flags
            assert '--bar' in self.c.flags

        def turns_single_character_names_into_short_flags(self):
            self.c.add_arg('f')
            assert '-f' in self.c.flags
            assert '--f' not in self.c.flags

        def adds_positional_args_to_positional_args(self):
            self.c.add_arg(name='pos', positional=True)
            eq_(self.c.positional_args[0].name, 'pos')

        def positional_args_empty_when_none_given(self):
            eq_(len(self.c.positional_args), 0)

        def positional_args_filled_in_order(self):
            self.c.add_arg(name='pos1', positional=True)
            eq_(self.c.positional_args[0].name, 'pos1')
            self.c.add_arg(name='abc', positional=True)
            eq_(self.c.positional_args[1].name, 'abc')

        def positional_arg_modifications_affect_args_copy(self):
            self.c.add_arg(name='hrm', positional=True)
            eq_(self.c.args['hrm'].value, self.c.positional_args[0].value)
            self.c.positional_args[0].value = 17
            eq_(self.c.args['hrm'].value, self.c.positional_args[0].value)
Example #45
0
    class help_for:
        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]

        def raises_ValueError_for_non_flag_values(self):
            with raises(ValueError):
                self.vanilla.help_for('foo')

        def vanilla_no_helpstr(self):
            assert self.vanilla.help_for('--foo') == ("--foo=STRING", "")

        def vanilla_with_helpstr(self):
            result = self.vanilla.help_for('--bar')
            assert result == ("--bar=STRING", "bar the baz")

        def task_driven_with_helpstr(self):
            result = self.tasked.help_for('--otherarg')
            assert result == ("-o STRING, --otherarg=STRING", "other help")

        # Yes, the next 3 tests are identical in form, but technically they
        # test different behaviors. HERPIN' AN' DERPIN'
        def task_driven_no_helpstr(self):
            result = self.tasked.help_for('--myarg')
            assert result == ("-m STRING, --myarg=STRING", "")

        def short_form_before_long_form(self):
            result = self.tasked.help_for('--myarg')
            assert result == ("-m STRING, --myarg=STRING", "")

        def equals_sign_for_long_form_only(self):
            result = self.tasked.help_for('--myarg')
            assert result == ("-m STRING, --myarg=STRING", "")

        def kind_to_placeholder_map(self):
            # Strings
            helpfor = self.tasked.help_for('--myarg')
            assert helpfor == ("-m STRING, --myarg=STRING", "")
            # Ints
            helpfor = self.tasked.help_for('--intval')
            assert helpfor == ("-i INT, --intval=INT", "")
            # TODO: others

        def shortflag_inputs_work_too(self):
            m = self.tasked.help_for('-m')
            myarg = self.tasked.help_for('--myarg')
            assert m == myarg

        def optional_values_use_brackets(self):
            result = self.tasked.help_for('--optval')
            assert result == ("-p [STRING], --optval[=STRING]", "")

        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')

        def true_default_args(self):
            c = Context(args=(Argument('truthy', kind=bool, default=True),))
            assert c.help_for('--truthy') == ('--[no-]truthy', '')
Example #46
0
 def can_take_Argument_instance(self):
     c = Context()
     a = Argument(names=('foo',))
     c.add_arg(a)
     assert c.get_arg('foo') is a
Example #47
0
    class add_arg:
        def setup(self):
            self.c = Context()

        def can_take_Argument_instance(self):
            a = Argument(names=('foo',))
            self.c.add_arg(a)
            assert self.c.args['foo'] is a

        def can_take_name_arg(self):
            self.c.add_arg('foo')
            assert 'foo' in self.c.args

        def can_take_kwargs(self):
            self.c.add_arg(names=('foo', 'bar'))
            assert 'foo' in self.c.args and 'bar' in self.c.args

        @raises(ValueError)
        def raises_ValueError_on_duplicate(self):
            self.c.add_arg(names=('foo', 'bar'))
            self.c.add_arg(name='bar')

        def adds_flaglike_name_to_dot_flags(self):
            "adds flaglike name to .flags"
            self.c.add_arg('foo')
            assert '--foo' in self.c.flags

        def adds_all_names_to_dot_flags(self):
            "adds all names to .flags"
            self.c.add_arg(names=('foo', 'bar'))
            assert '--foo' in self.c.flags
            assert '--bar' in self.c.flags

        def turns_single_character_names_into_short_flags(self):
            self.c.add_arg('f')
            assert '-f' in self.c.flags
            assert '--f' not in self.c.flags
Example #48
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.get_arg('foo') is a1
Example #49
0
 def true_default_args(self):
     c = Context(args=(Argument('truthy', kind=bool, default=True),))
     assert c.help_for('--truthy') == ('--[no-]truthy', '')
Example #50
0
 def underscored_args(self):
     c = Context(args=(Argument('i_have_underscores', help='yup'),))
     eq_(c.help_for('--i-have-underscores'), ('--i-have-underscores=STRING', 'yup'))
Example #51
0
    class add_arg:
        def setup(self):
            self.c = Context()

        def can_take_Argument_instance(self):
            a = Argument(names=('foo',))
            self.c.add_arg(a)
            assert self.c.args['foo'] is a

        def can_take_name_arg(self):
            self.c.add_arg('foo')
            assert 'foo' in self.c.args

        def can_take_kwargs_for_single_Argument(self):
            self.c.add_arg(names=('foo', 'bar'))
            assert 'foo' in self.c.args and 'bar' in self.c.args

        def raises_ValueError_on_duplicate(self):
            self.c.add_arg(names=('foo', 'bar'))
            with raises(ValueError):
                self.c.add_arg(name='bar')

        def adds_flaglike_name_to_dot_flags(self):
            "adds flaglike name to .flags"
            self.c.add_arg('foo')
            assert '--foo' in self.c.flags

        def adds_all_names_to_dot_flags(self):
            "adds all names to .flags"
            self.c.add_arg(names=('foo', 'bar'))
            assert '--foo' in self.c.flags
            assert '--bar' in self.c.flags

        def adds_true_bools_to_inverse_flags(self):
            self.c.add_arg(name='myflag', default=True, kind=bool)
            assert '--myflag' in self.c.flags
            assert '--no-myflag' in self.c.inverse_flags
            assert self.c.inverse_flags['--no-myflag'] == '--myflag'

        def inverse_flags_works_right_with_task_driven_underscored_names(self):
            # Use a Task here instead of creating a raw argument, we're partly
            # testing Task.get_arguments()' transform of underscored names
            # here. Yes that makes this an integration test, but it's nice to
            # test it here at this level & not just in cli tests.
            @task
            def mytask(c, underscored_option=True):
                pass
            self.c.add_arg(mytask.get_arguments()[0])
            flags = self.c.inverse_flags['--no-underscored-option']
            assert flags == '--underscored-option'

        def turns_single_character_names_into_short_flags(self):
            self.c.add_arg('f')
            assert '-f' in self.c.flags
            assert '--f' not in self.c.flags

        def adds_positional_args_to_positional_args(self):
            self.c.add_arg(name='pos', positional=True)
            assert self.c.positional_args[0].name == 'pos'

        def positional_args_empty_when_none_given(self):
            assert len(self.c.positional_args) == 0

        def positional_args_filled_in_order(self):
            self.c.add_arg(name='pos1', positional=True)
            assert self.c.positional_args[0].name == 'pos1'
            self.c.add_arg(name='abc', positional=True)
            assert self.c.positional_args[1].name == 'abc'

        def positional_arg_modifications_affect_args_copy(self):
            self.c.add_arg(name='hrm', positional=True)
            assert self.c.args['hrm'].value == self.c.positional_args[0].value
            self.c.positional_args[0].value = 17
            assert self.c.args['hrm'].value == self.c.positional_args[0].value
Example #52
0
 def true_default_args(self):
     c = Context(args=(Argument("truthy", kind=bool, default=True),))
     assert c.help_for("--truthy") == ("--[no-]truthy", "")
Example #53
0
    class help_for:
        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'})
            def mytask(myarg, otherarg):
                pass
            col = Collection(mytask)
            self.tasked = col.to_contexts()[0]

        @raises(ValueError)
        def raises_ValueError_for_non_flag_values(self):
            self.vanilla.help_for('foo')

        def vanilla_no_helpstr(self):
            eq_(
                self.vanilla.help_for('--foo'),
                ("--foo=STRING", "")
            )

        def vanilla_with_helpstr(self):
            eq_(
                self.vanilla.help_for('--bar'),
                ("--bar=STRING", "bar the baz")
            )

        def task_driven_with_helpstr(self):
            eq_(
                self.tasked.help_for('--otherarg'),
                ("-o STRING, --otherarg=STRING", "other help")
            )

        # Yes, the next 3 tests are identical in form, but technically they
        # test different behaviors. HERPIN' AN' DERPIN'
        def task_driven_no_helpstr(self):
            eq_(
                self.tasked.help_for('--myarg'),
                ("-m STRING, --myarg=STRING", "")
            )

        def short_form_before_long_form(self):
            eq_(
                self.tasked.help_for('--myarg'),
                ("-m STRING, --myarg=STRING", "")
            )

        def equals_sign_for_long_form_only(self):
            eq_(
                self.tasked.help_for('--myarg'),
                ("-m STRING, --myarg=STRING", "")
            )

        def kind_to_placeholder_map(self):
            # str=STRING, int=INT, etc etc
            skip()

        def shortflag_inputs_work_too(self):
            eq_(self.tasked.help_for('-m'), self.tasked.help_for('--myarg'))
Example #54
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")