From dcafd4e6ee7069009c8ea87f69cd8d6f9fe99067 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 23 Apr 2020 13:57:30 -0400 Subject: Fixed issue where subcommand usage text could contain a subcommand alias instead of the actual name --- cmd2/decorators.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'cmd2') diff --git a/cmd2/decorators.py b/cmd2/decorators.py index dc196032..7d097534 100644 --- a/cmd2/decorators.py +++ b/cmd2/decorators.py @@ -85,6 +85,7 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str): """ Recursively set prog attribute of a parser and all of its subparsers so that the root command is a command name and not sys.argv[0]. + :param parser: the parser being edited :param prog: value for the current parsers prog attribute """ @@ -94,11 +95,25 @@ def _set_parser_prog(parser: argparse.ArgumentParser, prog: str): # Set the prog value for the parser's subcommands for action in parser._actions: if isinstance(action, argparse._SubParsersAction): - - # Set the prog value for each subcommand - for sub_cmd, sub_cmd_parser in action.choices.items(): - sub_cmd_prog = parser.prog + ' ' + sub_cmd - _set_parser_prog(sub_cmd_parser, sub_cmd_prog) + # The keys of action.choices are subcommand names as well as subcommand aliases. The aliases point to the + # same parser as the actual subcommand. We want to avoid placing an alias into a parser's prog value. + # Unfortunately there is nothing about an action.choices entry which tells us it's an alias. In most cases + # we can filter out the aliases by checking the contents of action._choices_actions. This list only contains + # help information and names for the subcommands and not aliases. However, subcommands without help text + # won't show up in that list. Since dictionaries are ordered in Python 3.6 and above and argparse inserts the + # subcommand name into choices dictionary before aliases, we should be OK assuming the first time we see a + # parser, the dictionary key is a subcommand and not alias. + processed_parsers = [] + + # Set the prog value for each subcommand's parser + for subcmd_name, subcmd_parser in action.choices.items(): + # Check if we've already edited this parser + if subcmd_parser in processed_parsers: + continue + + subcmd_prog = parser.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 -- cgit v1.2.1