From 9fc568357e2c3441e1814ffed7a7f377223eeaa2 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 5 Nov 2024 01:02:26 -0500 Subject: [PATCH] Use add_subparsers() 'required' keyword argument added in Python 3.7. --- cmd2/cmd2.py | 6 ++---- tests/test_argparse.py | 6 ++---- tests/test_argparse_completer.py | 3 +-- tests_isolated/test_commandset/test_argparse_subcommands.py | 3 +-- tests_isolated/test_commandset/test_commandset.py | 3 +-- 5 files changed, 7 insertions(+), 14 deletions(-) diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index a508f305..b839205b 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -3404,8 +3404,7 @@ def _cmdloop(self) -> None: alias_description = "Manage aliases\n" "\n" "An alias is a command that enables replacement of a word by another string." alias_epilog = "See also:\n" " macro" alias_parser = argparse_custom.DEFAULT_ARGUMENT_PARSER(description=alias_description, epilog=alias_epilog) - alias_subparsers = alias_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - alias_subparsers.required = True + alias_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) # Preserve quotes since we are passing strings to other commands @with_argparser(alias_parser, preserve_quotes=True) @@ -3573,8 +3572,7 @@ def _alias_list(self, args: argparse.Namespace) -> None: macro_description = "Manage macros\n" "\n" "A macro is similar to an alias, but it can contain argument placeholders." macro_epilog = "See also:\n" " alias" macro_parser = argparse_custom.DEFAULT_ARGUMENT_PARSER(description=macro_description, epilog=macro_epilog) - macro_subparsers = macro_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - macro_subparsers.required = True + macro_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) # Preserve quotes since we are passing strings to other commands @with_argparser(macro_parser, preserve_quotes=True) diff --git a/tests/test_argparse.py b/tests/test_argparse.py index 4fe884b9..cf1eda92 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -250,8 +250,7 @@ def test_preservelist(argparse_app): def _build_has_subcmd_parser() -> cmd2.Cmd2ArgumentParser: 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 + has_subcmds_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) return has_subcmds_parser @@ -273,8 +272,7 @@ def base_helpless(self, args): # create the top-level parser for the base command base_parser = cmd2.Cmd2ArgumentParser() - base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - base_subparsers.required = True + base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) # create the parser for the "foo" subcommand parser_foo = base_subparsers.add_parser('foo', help='foo help') diff --git a/tests/test_argparse_completer.py b/tests/test_argparse_completer.py index 7368774c..1f9178f8 100644 --- a/tests/test_argparse_completer.py +++ b/tests/test_argparse_completer.py @@ -1249,8 +1249,7 @@ def do_custom_completer(self, args: argparse.Namespace) -> None: # Test as_subcommand_to decorator with custom completer top_parser = Cmd2ArgumentParser(description="Top Command") - top_subparsers = top_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - top_subparsers.required = True + top_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) @with_argparser(top_parser) def do_top(self, args: argparse.Namespace) -> None: diff --git a/tests_isolated/test_commandset/test_argparse_subcommands.py b/tests_isolated/test_commandset/test_argparse_subcommands.py index 0fc8c47e..3a9ab595 100644 --- a/tests_isolated/test_commandset/test_argparse_subcommands.py +++ b/tests_isolated/test_commandset/test_argparse_subcommands.py @@ -35,8 +35,7 @@ def base_helpless(self, args): # create the top-level parser for the base command base_parser = cmd2.Cmd2ArgumentParser() - base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - base_subparsers.required = True + base_subparsers = base_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) # create the parser for the "foo" subcommand parser_foo = base_subparsers.add_parser('foo', help='foo help') diff --git a/tests_isolated/test_commandset/test_commandset.py b/tests_isolated/test_commandset/test_commandset.py index c7293d41..da46c5d1 100644 --- a/tests_isolated/test_commandset/test_commandset.py +++ b/tests_isolated/test_commandset/test_commandset.py @@ -93,8 +93,7 @@ def do_elderberry(self, ns: argparse.Namespace): # Test that CommandSet with as_subcommand_to decorator successfully loads # during `cmd2.Cmd.__init__()`. main_parser = cmd2.Cmd2ArgumentParser(description="Main Command") - main_subparsers = main_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND') - main_subparsers.required = True + main_parser.add_subparsers(dest='subcommand', metavar='SUBCOMMAND', required=True) @cmd2.with_category('Alone') @cmd2.with_argparser(main_parser)