Skip to content

Commit bcd5c08

Browse files
kmvanbruntanselor
authored andcommitted
Added handling for disabled commands to CommandSet functions
1 parent 11a86e5 commit bcd5c08

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

cmd2/cmd2.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,11 @@ def uninstall_command_set(self, cmdset: CommandSet):
517517
for method in methods:
518518
cmd_name = method[0][len(COMMAND_FUNC_PREFIX):]
519519

520+
# Enable the command before uninstalling it to make sure we remove both
521+
# the real functions and the ones used by the DisabledCommand object.
522+
if cmd_name in self.disabled_commands:
523+
self.enable_command(cmd_name)
524+
520525
delattr(self, COMMAND_FUNC_PREFIX + cmd_name)
521526

522527
if hasattr(self, COMPLETER_FUNC_PREFIX + cmd_name):
@@ -553,11 +558,15 @@ def _register_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
553558
parser_args = getattr(method, constants.SUBCMD_ATTR_PARSER_ARGS, {})
554559

555560
# Search for the base command function and verify it has an argparser defined
556-
command_func = self.cmd_func(command_name)
557-
if command_func is None or not hasattr(command_func, constants.CMD_ATTR_ARGPARSER):
561+
if command_name in self.disabled_commands:
562+
command_func = self.disabled_commands[command_name].command_function
563+
else:
564+
command_func = self.cmd_func(command_name)
565+
566+
if command_func is None:
558567
raise TypeError('Could not find command "{}" needed by subcommand: {}'
559568
.format(command_name, str(method)))
560-
command_parser = getattr(command_func, constants.CMD_ATTR_ARGPARSER)
569+
command_parser = getattr(command_func, constants.CMD_ATTR_ARGPARSER, None)
561570
if command_parser is None:
562571
raise TypeError('Could not find argparser for command "{}" needed by subcommand: {}'
563572
.format(command_name, str(method)))
@@ -596,11 +605,15 @@ def _unregister_subcommands(self, cmdset: Union[CommandSet, 'Cmd']) -> None:
596605
command_name = getattr(method, constants.SUBCMD_ATTR_COMMAND)
597606

598607
# Search for the base command function and verify it has an argparser defined
599-
command_func = self.cmd_func(command_name)
600-
if command_func is None or not hasattr(command_func, constants.CMD_ATTR_ARGPARSER):
608+
if command_name in self.disabled_commands:
609+
command_func = self.disabled_commands[command_name].command_function
610+
else:
611+
command_func = self.cmd_func(command_name)
612+
613+
if command_func is None:
601614
raise TypeError('Could not find command "{}" needed by subcommand: {}'
602615
.format(command_name, str(method)))
603-
command_parser = getattr(command_func, constants.CMD_ATTR_ARGPARSER)
616+
command_parser = getattr(command_func, constants.CMD_ATTR_ARGPARSER, None)
604617
if command_parser is None:
605618
raise TypeError('Could not find argparser for command "{}" needed by subcommand: {}'
606619
.format(command_name, str(method)))

0 commit comments

Comments
 (0)