MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / update_prog

Method update_prog

cmd2/argparse_utils.py:714–755  ·  view source on GitHub ↗

Recursively update the prog attribute of this parser and all of its subparsers. :param prog: new value for this parser's prog attribute

(self, prog: str)

Source from the content-addressed store, hash-verified

712 return temp_parser.add_subparsers()._prog_prefix
713
714 def update_prog(self, prog: str) -> None:
715 """Recursively update the prog attribute of this parser and all of its subparsers.
716
717 :param prog: new value for this parser's prog attribute
718 """
719 # Set the prog value for this parser
720 self.prog = prog
721
722 try:
723 subparsers_action = self.get_subparsers_action()
724 except ValueError:
725 # This parser has no subcommands
726 return
727
728 # Get all positional arguments which appear before the subcommand.
729 positionals: list[argparse.Action] = []
730 for action in self._actions:
731 if action is subparsers_action:
732 break
733
734 # Save positional argument
735 if not action.option_strings:
736 positionals.append(action)
737
738 # Update _prog_prefix. This ensures that any subcommands added later via
739 # add_parser() will have the correct prog value.
740 subparsers_action._prog_prefix = self._build_subparsers_prog_prefix(positionals)
741
742 # subparsers_action._name_parser_map includes aliases. Since primary names are inserted
743 # first, we skip already updated parsers to ensure primary names are used in 'prog'.
744 # We can't rely on subparsers_action._choices_actions to filter out aliases because while
745 # it contains only primary names, it omits any subcommands that lack help text.
746 updated_parsers: set[Cmd2ArgumentParser] = set()
747
748 # Set the prog value for each subcommand's parser
749 for subcmd_name, subcmd_parser in subparsers_action._name_parser_map.items():
750 if subcmd_parser in updated_parsers:
751 continue
752
753 subcmd_prog = f"{subparsers_action._prog_prefix} {subcmd_name}"
754 subcmd_parser.update_prog(subcmd_prog)
755 updated_parsers.add(subcmd_parser)
756
757 def find_parser(self, subcommand_path: Iterable[str]) -> "Cmd2ArgumentParser":
758 """Find a parser in the hierarchy based on a sequence of subcommand names.

Callers 3

test_update_progFunction · 0.95
attach_subcommandMethod · 0.80
getMethod · 0.80

Calls 4

get_subparsers_actionMethod · 0.95
appendMethod · 0.80
addMethod · 0.80

Tested by 1

test_update_progFunction · 0.76