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

Method register_command_set

cmd2/cmd2.py:834–911  ·  view source on GitHub ↗

Installs a CommandSet, loading all commands defined in the CommandSet. :param cmdset: CommandSet to load

(self, cmdset: CommandSet[Any])

Source from the content-addressed store, hash-verified

832 load_commandset_by_type(all_commandset_defs)
833
834 def register_command_set(self, cmdset: CommandSet[Any]) -> None:
835 """Installs a CommandSet, loading all commands defined in the CommandSet.
836
837 :param cmdset: CommandSet to load
838 """
839 existing_commandset_types = [type(command_set) for command_set in self._installed_command_sets]
840 if type(cmdset) in existing_commandset_types:
841 raise CommandSetRegistrationError("CommandSet " + type(cmdset).__name__ + " is already installed")
842
843 all_settables = self.settables
844 if self.always_prefix_settables:
845 if not cmdset.settable_prefix.strip():
846 raise CommandSetRegistrationError("CommandSet settable prefix must not be empty")
847 for key in cmdset.settables:
848 prefixed_name = f"{cmdset.settable_prefix}.{key}"
849 if prefixed_name in all_settables:
850 raise CommandSetRegistrationError(f"Duplicate settable: {key}")
851
852 else:
853 for key in cmdset.settables:
854 if key in all_settables:
855 raise CommandSetRegistrationError(f"Duplicate settable {key} is already registered")
856
857 cmdset.on_register(self)
858 methods = cast(
859 list[tuple[str, Callable[..., Any]]],
860 inspect.getmembers(
861 cmdset,
862 predicate=lambda meth: ( # type: ignore[arg-type]
863 isinstance(meth, Callable) # type: ignore[arg-type]
864 and hasattr(meth, "__name__")
865 and meth.__name__.startswith(COMMAND_FUNC_PREFIX)
866 ),
867 ),
868 )
869
870 installed_attributes = []
871 try:
872 for cmd_func_name, command_method in methods:
873 command = cmd_func_name[len(COMMAND_FUNC_PREFIX) :]
874
875 self._install_command_function(cmd_func_name, command_method, type(cmdset).__name__)
876 installed_attributes.append(cmd_func_name)
877
878 completer_func_name = COMPLETER_FUNC_PREFIX + command
879 cmd_completer = getattr(cmdset, completer_func_name, None)
880 if cmd_completer is not None:
881 self._install_completer_function(command, cmd_completer)
882 installed_attributes.append(completer_func_name)
883
884 help_func_name = HELP_FUNC_PREFIX + command
885 cmd_help = getattr(cmdset, help_func_name, None)
886 if cmd_help is not None:
887 self._install_help_function(command, cmd_help)
888 installed_attributes.append(help_func_name)
889
890 self._cmd_to_command_sets[command] = cmdset
891

Callers 15

__init__Method · 0.95
test_load_commandsFunction · 0.80
test_subcommandsFunction · 0.80
test_commandset_sigintFunction · 0.80
test_nested_subcommandsFunction · 0.80

Calls 14

_get_command_categoryMethod · 0.95
disable_commandMethod · 0.95
_register_subcommandsMethod · 0.95
appendMethod · 0.80
addMethod · 0.80
removeMethod · 0.80
on_registerMethod · 0.45
on_registeredMethod · 0.45

Tested by 14

test_load_commandsFunction · 0.64
test_subcommandsFunction · 0.64
test_commandset_sigintFunction · 0.64
test_nested_subcommandsFunction · 0.64
test_path_completeFunction · 0.64
test_ns_providerFunction · 0.64