diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-08-27 20:56:56 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-08-27 23:17:05 -0400 |
commit | e3ed15ed375674729d65e4f594a8958ea91ae684 (patch) | |
tree | a05524c8f7fc3c56776e32648b5808252c7e6d51 /tests/test_argparse.py | |
parent | 47f8652fa467b2d140b1097b3167f968b0188451 (diff) | |
download | cmd2-git-e3ed15ed375674729d65e4f594a8958ea91ae684.tar.gz |
Fixed issue where subcommand added with @as_subcommand_to decorator did not display help when called with -h/--help.
'add_help=False' no longer has to be passed to parsers used in @as_subcommand_to decorator.
Diffstat (limited to 'tests/test_argparse.py')
-rw-r--r-- | tests/test_argparse.py | 46 |
1 files changed, 37 insertions, 9 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py index 20d05bed..7059e9d3 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -289,24 +289,32 @@ class SubcommandApp(cmd2.Cmd): func = getattr(args, 'func') func(self, args) - # Add a subcommand using as_subcommand_to decorator - has_subcmd_parser = cmd2.Cmd2ArgumentParser(description="Tests as_subcmd_to decorator") - has_subcmd_subparsers = has_subcmd_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - has_subcmd_subparsers.required = True + # Add subcommands using as_subcommand_to decorator + has_subcmds_parser = cmd2.Cmd2ArgumentParser(description="Tests as_subcmd_to decorator") + has_subcmds_subparsers = has_subcmds_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') + has_subcmds_subparsers.required = True - @cmd2.with_argparser(has_subcmd_parser) + @cmd2.with_argparser(has_subcmds_parser) def do_test_subcmd_decorator(self, args: argparse.Namespace): handler = args.cmd2_handler.get() handler(args) - subcmd_parser = cmd2.Cmd2ArgumentParser(add_help=False, description="The subcommand") + subcmd_parser = cmd2.Cmd2ArgumentParser(description="A subcommand") - @cmd2.as_subcommand_to('test_subcmd_decorator', 'subcmd', subcmd_parser, help='the subcommand') + @cmd2.as_subcommand_to('test_subcmd_decorator', 'subcmd', subcmd_parser, help=subcmd_parser.description.lower()) def subcmd_func(self, args: argparse.Namespace): - # Make sure printing the Namespace works. The way we originally added get_hander() - # to it resulted in a RecursionError when printing. + # Make sure printing the Namespace works. The way we originally added cmd2_hander to it resulted in a RecursionError. self.poutput(args) + helpless_subcmd_parser = cmd2.Cmd2ArgumentParser(add_help=False, description="A subcommand with no help") + + @cmd2.as_subcommand_to('test_subcmd_decorator', 'helpless_subcmd', helpless_subcmd_parser, + help=helpless_subcmd_parser.description.lower()) + def helpless_subcmd_func(self, args: argparse.Namespace): + # Make sure vars(Namespace) works. The way we originally added cmd2_hander to it resulted in a RecursionError. + self.poutput(vars(args)) + + @pytest.fixture def subcommand_app(): app = SubcommandApp() @@ -391,9 +399,29 @@ def test_add_another_subcommand(subcommand_app): def test_subcmd_decorator(subcommand_app): + # Test subcommand that has help option out, err = run_cmd(subcommand_app, 'test_subcmd_decorator subcmd') assert out[0].startswith('Namespace(') + out, err = run_cmd(subcommand_app, 'help test_subcmd_decorator subcmd') + assert out[0] == 'Usage: test_subcmd_decorator subcmd [-h]' + + out, err = run_cmd(subcommand_app, 'test_subcmd_decorator subcmd -h') + assert out[0] == 'Usage: test_subcmd_decorator subcmd [-h]' + + # Test subcommand that has no help option + out, err = run_cmd(subcommand_app, 'test_subcmd_decorator helpless_subcmd') + assert "'subcommand': 'helpless_subcmd'" in out[0] + + out, err = run_cmd(subcommand_app, 'help test_subcmd_decorator helpless_subcmd') + assert out[0] == 'Usage: test_subcmd_decorator helpless_subcmd' + assert not err + + out, err = run_cmd(subcommand_app, 'test_subcmd_decorator helpless_subcmd -h') + assert not out + assert err[0] == 'Usage: test_subcmd_decorator [-h] SUBCOMMAND ...' + assert err[1] == 'Error: unrecognized arguments: -h' + def test_unittest_mock(): from unittest import mock |