From e3ed15ed375674729d65e4f594a8958ea91ae684 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 27 Aug 2020 20:56:56 -0400 Subject: 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. --- tests/test_argparse.py | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'tests') 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 -- cgit v1.2.1