示例#1
0
    def test_multiple_argument(self):
        cli = CLI()

        @cli.command
        def multi(a, b: list):
            return b

        ret = cli.run(["multi", "", "--b", "1", "--b", "2"])
        self.assertEqual(ret, ["1", "2"])
示例#2
0
    def test_arguments(self):
        cli = CLI()

        @cli.command
        def add(a, b):
            return a + b

        ret = cli.run(["add", "1", "2"])
        self.assertEqual(ret, "12")
示例#3
0
    def test_bool_argument(self):
        cli = CLI()

        @cli.command
        def close(switch: bool = True):
            return switch

        ret = cli.run(["close", "--switch"])
        self.assertIs(ret, False)
示例#4
0
    def test_annotation(self):
        cli = CLI()

        @cli.command
        def add(a: int, b: int):
            return a + b

        ret = cli.run(["add", "1", "2"])
        self.assertEqual(ret, 3)
示例#5
0
    def test_argument_default(self):
        cli = CLI()

        @cli.command
        def add(a: int, b: int = 3):
            return a + b

        ret = cli.run(["add", "1"])
        self.assertEqual(ret, 4)
示例#6
0
    def test_version(self, mock_stdout, mock_stderr):
        cli = CLI(prog="app", version="v1.0.0")

        with self.assertRaises(SystemExit):
            cli.run(["-v"])

        self.assertIn("app v1.0.0\n",
                      [mock_stdout.getvalue(),
                       mock_stderr.getvalue()])
示例#7
0
    def test_common(self, mock_stdout):
        cli = CLI()

        @cli.command
        def test():
            print("test", end='')

        cli.run(["test"])
        self.assertEqual(mock_stdout.getvalue(), "test")
示例#8
0
    def test_custom_subcommand_info(self, mock_stdout):
        cli = CLI()

        @cli.command_with_args(title="test1",
                               help="unittest",
                               description="use for unittest")
        def test():
            pass

        with self.assertRaises(SystemExit):
            cli.run(["test1", "-h"])

        self.assertIn("use for unittest", mock_stdout.getvalue())
示例#9
0
    def test_multiple_subcommands(self, mock_stdout):
        cli = CLI()

        @cli.command
        def test1():
            print("test1", end='')

        @cli.command
        def test2():
            print("test2", end='')

        cli.run(["test1"])
        cli.run(["test2"])
        self.assertEqual(mock_stdout.getvalue(), "test1" + "test2")
示例#10
0
    def test_argparse_compatible(self):
        cli = CLI()

        cli.add_argument("-a", action="store", default=1)
        cli.add_argument("-b", action="store_true", default=False)

        namespace = cli.parse_args(["-b"])
        self.assertEqual(namespace.b, True)
示例#11
0
    def test_duplicate_error(self):
        cli = CLI()

        @cli.command
        def test1():
            pass

        with self.assertRaises(argparse.ArgumentError):

            @cli.command
            def test1():
                pass