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

Method _register_subcommands

cmd2/cmd2.py:1091–1139  ·  view source on GitHub ↗

Register subcommands with their base command. :param cmdset: CommandSet or cmd2.Cmd subclass containing subcommands

(self, cmdset: CmdOrSet)

Source from the content-addressed store, hash-verified

1089 check_parser_uninstallable(command_parser)
1090
1091 def _register_subcommands(self, cmdset: CmdOrSet) -> None:
1092 """Register subcommands with their base command.
1093
1094 :param cmdset: CommandSet or cmd2.Cmd subclass containing subcommands
1095 """
1096 if not (cmdset is self or cmdset in self._installed_command_sets):
1097 raise CommandSetRegistrationError("Cannot register subcommands with an unregistered CommandSet")
1098
1099 # find methods that have the required attributes necessary to be recognized as a sub-command
1100 methods = inspect.getmembers(
1101 cmdset,
1102 predicate=lambda meth: (
1103 isinstance(meth, Callable) # type: ignore[arg-type]
1104 and hasattr(meth, constants.SUBCMD_ATTR_NAME)
1105 and hasattr(meth, constants.SUBCMD_ATTR_COMMAND)
1106 and hasattr(meth, constants.CMD_ATTR_ARGPARSER)
1107 ),
1108 )
1109
1110 # iterate through all matching methods
1111 for _method_name, method in methods:
1112 subcommand_name: str = getattr(method, constants.SUBCMD_ATTR_NAME)
1113 full_command_name: str = getattr(method, constants.SUBCMD_ATTR_COMMAND)
1114 subcmd_parser_builder = getattr(method, constants.CMD_ATTR_ARGPARSER)
1115
1116 subcommand_valid, errmsg = self.statement_parser.is_valid_command(subcommand_name, is_subcommand=True)
1117 if not subcommand_valid:
1118 raise CommandSetRegistrationError(f"Subcommand {subcommand_name} is not valid: {errmsg}")
1119
1120 # Create the subcommand parser and configure it
1121 subcmd_parser = self._build_parser(cmdset, subcmd_parser_builder)
1122 if subcmd_parser.description is None and method.__doc__:
1123 subcmd_parser.description = strip_doc_annotations(method.__doc__)
1124
1125 # Set the subcommand handler
1126 defaults = {constants.NS_ATTR_SUBCMD_HANDLER: method}
1127 subcmd_parser.set_defaults(**defaults)
1128
1129 # Set what instance the handler is bound to
1130 setattr(subcmd_parser, constants.PARSER_ATTR_COMMANDSET_ID, id(cmdset))
1131
1132 # Get add_parser() kwargs (aliases, help, etc.) defined by the decorator
1133 add_parser_kwargs = getattr(method, constants.SUBCMD_ATTR_ADD_PARSER_KWARGS, {})
1134
1135 # Attach existing parser as a subcommand
1136 try:
1137 self.attach_subcommand(full_command_name, subcommand_name, subcmd_parser, **add_parser_kwargs)
1138 except ValueError as ex:
1139 raise CommandSetRegistrationError(str(ex)) from ex
1140
1141 def _unregister_subcommands(self, cmdset: CmdOrSet) -> None:
1142 """Unregister subcommands from their base command.

Callers 3

__init__Method · 0.95
register_command_setMethod · 0.95
test_subcommandsFunction · 0.80

Calls 5

_build_parserMethod · 0.95
attach_subcommandMethod · 0.95
strip_doc_annotationsFunction · 0.85
is_valid_commandMethod · 0.80

Tested by 1

test_subcommandsFunction · 0.64