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"])
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")
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)
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)
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)
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()])
def test_common(self, mock_stdout): cli = CLI() @cli.command def test(): print("test", end='') cli.run(["test"]) self.assertEqual(mock_stdout.getvalue(), "test")
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())
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")
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)
def test_duplicate_error(self): cli = CLI() @cli.command def test1(): pass with self.assertRaises(argparse.ArgumentError): @cli.command def test1(): pass