Exemple #1
0
    def test_get_usage(self):
        command = Command()
        command.add_option("foo", Option("-o", String()))
        self.assert_equal(command.get_usage(), u("[-h] [-o foo]"))

        command.add_command("bar", Command())
        self.assert_equal(command.get_usage(), u("[-h] [-o foo] {bar}"))

        command.add_positional(String(metavar="baz"))
        self.assert_equal(command.get_usage(), u("[-h] [-o foo] {bar} baz"))
Exemple #2
0
    def test_run(self):
        class TestCommand(Command):
            commands = {"foo": Command()}
        with self.assert_raises(CommandMissing):
            TestCommand().run([], passthrough_errors=True)

        with self.assert_raises(NotImplementedError):
            Command().run([], passthrough_errors=True)

        with self.assert_raises(UnexpectedArgument):
            Command().run(["--unexpected"], passthrough_errors=True)

        with self.assert_raises(UnexpectedArgument):
            Command().run(["-u"], passthrough_errors=True)

        with self.assert_raises(UnexpectedArgument):
            Command().run(["unexpected"], passthrough_errors=True)

        command = Command()
        command.add_option("foo", Option("-b", String()))
        with self.assert_raises(UnexpectedArgument):
            command.run(["-a"], passthrough_errors=True)
Exemple #3
0
    def test_add_option(self):
        command = Command()
        a = Option("-a", "--foobar", String())
        command.add_option("foo", a)
        with self.assert_raises(OptionConflict):
            command.add_option("bar", a)
        with self.assert_raises(OptionConflict):
            command.add_option("baz", Option("--foobar", String()))

        command = Command()
        command.add_option("foo", Option("-a", "--asd", String()))
        command.add_option("bar", Option("-b", "--qwe", String()))
        command.add_option(
            "spam",
            Option("-a", "--zxc", String()),
            resolve_conflicts=True
        )
        self.assert_equal(command.option_shorts["-a"].long, "--asd")
        command.add_option(
            "eggs",
            Option("-c", "--zxc", String()),
            resolve_conflicts=True
        )
        self.assert_is(command.option_shorts["-c"].long, None)

        with self.assert_raises(OptionConflict):
            command.add_option(
                "blubb",
                Option("-a", "--asd", String()),
                resolve_conflicts=True
            )
        command.add_option(
            "blubb",
            Option("-a", "--asd", String()),
            resolve_conflicts=True,
            force=True
        )
        command.add_option(
            "bla",
            Option("-a", "--asd", String()),
            force=True
        )
Exemple #4
0
 def test_option_shorts_and_longs(self):
     command = Command()
     command.add_option("foo", Option("-a", String()))
     command.add_option("bar", Option("--abc", String()))
     self.assert_not_in(None, command.option_shorts)
     self.assert_not_in(None, command.option_longs)