Disable a command and replace its functions with disabled versions. :param command: the command being disabled :param message_to_print: what to print when this command is run or help is called on it while disabled The variable cmd2.COMMAND_NAME can
(self, command: str, message_to_print: str)
| 5639 | del self.disabled_categories[category] |
| 5640 | |
| 5641 | def disable_command(self, command: str, message_to_print: str) -> None: |
| 5642 | """Disable a command and replace its functions with disabled versions. |
| 5643 | |
| 5644 | :param command: the command being disabled |
| 5645 | :param message_to_print: what to print when this command is run or help is called on it while disabled |
| 5646 | |
| 5647 | The variable cmd2.COMMAND_NAME can be used as a placeholder for the name of the |
| 5648 | command being disabled. |
| 5649 | ex: message_to_print = f"{cmd2.COMMAND_NAME} is currently disabled" |
| 5650 | """ |
| 5651 | # If the command is already disabled, then return |
| 5652 | if command in self.disabled_commands: |
| 5653 | return |
| 5654 | |
| 5655 | # Make sure this is an actual command |
| 5656 | command_func = self.get_command_func(command) |
| 5657 | if command_func is None: |
| 5658 | raise AttributeError(f"'{command}' does not refer to a command") |
| 5659 | |
| 5660 | command_func_name = constants.COMMAND_FUNC_PREFIX + command |
| 5661 | |
| 5662 | help_func_name = constants.HELP_FUNC_PREFIX + command |
| 5663 | help_func = getattr(self, help_func_name, None) |
| 5664 | |
| 5665 | completer_func_name = constants.COMPLETER_FUNC_PREFIX + command |
| 5666 | completer_func = getattr(self, completer_func_name, None) |
| 5667 | |
| 5668 | # Add the disabled command record |
| 5669 | self.disabled_commands[command] = DisabledCommand( |
| 5670 | command_func=command_func, |
| 5671 | help_func=help_func, |
| 5672 | completer_func=completer_func, |
| 5673 | ) |
| 5674 | |
| 5675 | # Replace command and help functions to report the disabled message |
| 5676 | message_to_print = message_to_print.replace(constants.COMMAND_NAME, command) |
| 5677 | new_cmd_func = functools.partial( |
| 5678 | self._report_disabled_command_usage, |
| 5679 | message_to_print=message_to_print, |
| 5680 | ) |
| 5681 | |
| 5682 | # Ensure the replacement function identifies as the original for introspection |
| 5683 | functools.update_wrapper(new_cmd_func, command_func) |
| 5684 | setattr(self, command_func_name, new_cmd_func) |
| 5685 | |
| 5686 | new_help_func = functools.partial( |
| 5687 | self._report_disabled_command_usage, |
| 5688 | message_to_print=message_to_print, |
| 5689 | ) |
| 5690 | if help_func is not None: |
| 5691 | functools.update_wrapper(new_help_func, help_func) |
| 5692 | setattr(self, help_func_name, new_help_func) |
| 5693 | |
| 5694 | # Replace completer with a function that returns nothing |
| 5695 | new_completer_func = functools.partial(self._disabled_completer) |
| 5696 | if completer_func is not None: |
| 5697 | functools.update_wrapper(new_completer_func, completer_func) |
| 5698 | setattr(self, completer_func_name, new_completer_func) |