Пример #1
0
    def parse_test(self):
        command = TestCommand()
        command.add_option("foo", Option("-a", positional()))
        for args, expected in single:
            self.assert_equal(
                command.run(["-a"] + args),
                ((), {"foo": expected})
            )

        command.add_option("bar", Option("-b", positional(remaining=True)))
        for args, expected in remaining:
            self.assert_equal(
                command.run(["-b"] + args, passthrough_errors=True),
                ((), {"bar": expected})
            )

        command.add_option(
            "baz",
            Option("-c", positional(), positional(optional=True))
        )
        for args, expected in optional:
            self.assert_equal(
                command.run(["-c"] + args, passthrough_errors=True),
                ((), {"baz": expected})
            )
Пример #2
0
    def test_signature(self):
        command = TestCommand(
            options=[("option", Option("-o", String(), String(), String()))]
        )
        for args in [["-o"], ["-o", "foo"], ["-o", "foo", "bar"]]:
            with self.assert_raises(ArgumentMissing):
                command.run(args, passthrough_errors=True)

        command = TestCommand(
            options=[("option", Option("-o", String(), [String(), String()]))]
        )
        self.assert_equal(
            command.run(["-o", "a"]),
            ((), {"option": [u("a")]})
        )
        self.assert_equal(
            command.run(["-o", "a", "b", "c"]),
            ((), {"option": ["a", "b", "c"]})
        )
        for args in [["-o"], ["-o", "a", "b"]]:
            with self.assert_raises(ArgumentMissing):
                command.run(args, passthrough_errors=True)

        command = TestCommand(
            options=[("option", Option("-o", String(), [String(), [String()]]))]
        )
        args = ["-o", "a", "b", "c"]
        for i in range(2, len(args) + 1):
            self.assert_equal(
                command.run(args[:i]),
                ((), {"option": args[1:i] or [missing]})
            )
Пример #3
0
 def test_multiple_options_for_name(self):
     command = TestCommand(
         options=[
             ("foo", Option("-a", Integer())),
             ("foo", Option("-b", Integer()))
         ]
     )
     self.assert_equal(
         command.run(["-a", "1", "-b", "2"]),
         ((), {"foo": 2})
     )
Пример #4
0
 def test_parse(self):
     command = TestCommand(
         options=[
             ("foo", Option(
                 "-o",
                 Any([Integer()], u("{argument!r} is not an integer"))
             ))
         ]
     )
     self.assert_equal(command.run(["-o", "1"]), ((), {"foo": 1}))
     with self.assert_raises(UserTypeError):
         command.run(["-o", "foo"])
Пример #5
0
 def test_multiple_abbreviations(self):
     command = TestCommand(
         options=[
             ("a", Option("-a", String())),
             ("b", Option("-b", String())),
             ("c", Option("-c", String()))
         ]
     )
     self.assert_equal(
         command.run(["-abc", "foo", "bar", "baz"]),
         ((), {"a": u("foo"), "b": u("bar"), "c": u("baz")})
     )
Пример #6
0
 def test_parse(self):
     action = TestCommand(
         options=[("foo", Option("-a", Choice(Integer(), [1, 2])))]
     )
     self.assert_equal(
         action.run(["-a", "1"]),
         ((), {"foo": 1})
     )
     self.assert_equal(
         action.run(["-a", "2"]),
         ((), {"foo": 2})
     )
     with self.assert_raises(UserTypeError):
         action.run(["-a", "3"], passthrough_errors=True)
Пример #7
0
    def test_parse(self):
        command = TestCommand()
        command.add_option("foo", Option("-a", Boolean()))
        self.assert_equal(
            command.run(["-a"]),
            ((), {"foo": True})
        )

        command.add_option("bar", Option("-b", Boolean(store=False)))
        self.assert_equal(
            command.run(["-b"]),
            ((), {"bar": False})
        )
Пример #8
0
    def test_parse(self):
        command = TestCommand(
            options=[
                ("foo", Option("-o", Mapping(NativeString(), {"spam": 1})))
            ]
        )
        with self.assert_raises(UserTypeError):
            command.run(["-o", "eggs"])
        self.assert_equal(command.run(["-o", "spam"]), ((), {"foo": 1}))

        command = TestCommand(
            options=[
                ("foo", Option("-o", Mapping(
                    NativeString(), {"spam": 1}, remaining=True
                )))
            ]
        )
        self.assert_equal(
            command.run(["-o", "spam", "spam"]),
            ((), {"foo": [1, 1]})
        )