Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use add_subparsers() 'required' keyword argument added in Python 3.7. #1372

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions tests/test_argparse_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions tests_isolated/test_commandset/test_commandset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading