Skip to content

Add required args to subcommand program #1293

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

Merged
merged 7 commits into from
Sep 13, 2024
Merged
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions cmd2/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str) -> None:
"""
# Set the prog value for this parser
parser.prog = prog
req_args = []

# Set the prog value for the parser's subcommands
for action in parser._actions:
Expand All @@ -233,13 +234,18 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str) -> None:
if subcmd_parser in processed_parsers:
continue

subcmd_prog = parser.prog + ' ' + subcmd_name
subcmd_prog = parser.prog
if req_args:
subcmd_prog += " " + " ".join(req_args)
subcmd_prog += " " + subcmd_name
_set_parser_prog(subcmd_parser, subcmd_prog)
processed_parsers.append(subcmd_parser)

# We can break since argparse only allows 1 group of subcommands per level
break

# need to save required args so they can be prepended to the subcommand usage
elif action.required:
req_args.append(action.dest)

#: Function signature for a Command Function that uses an argparse.ArgumentParser to process user input
#: and optionally returns a boolean
Expand Down