| 1 | # Disabling Commands |
| 2 | |
| 3 | `cmd2` allows a developer to: |
| 4 | |
| 5 | - Remove commands included in `cmd2` |
| 6 | - Prevent commands from appearing in the help menu (hide commands) |
| 7 | - Disable and re-enable commands at runtime |
| 8 | |
| 9 | See |
| 10 | [remove_builtin_commands.py](https://github.com/python-cmd2/cmd2/blob/main/examples/remove_builtin_commands.py) |
| 11 | for and example of removing or hiding built-in commands. |
| 12 | |
| 13 | See [command_sets.py](https://github.com/python-cmd2/cmd2/blob/main/examples/command_sets.py) for an |
| 14 | example of dynamically enabling and disabling custom commands at runtime. |
| 15 | |
| 16 | ## Remove A Command |
| 17 | |
| 18 | When a command has been removed, the command method has been deleted from the object. The command |
| 19 | doesn't show up in help and it can't be executed. This approach is appropriate if you never want a |
| 20 | built-in command to be part of your application. Delete the command method in your initialization |
| 21 | code: |
| 22 | |
| 23 | ```py |
| 24 | class RemoveBuiltinCommand(cmd2.Cmd): |
| 25 | """An app which removes a built-in command from cmd2""" |
| 26 | |
| 27 | def __init__(self): |
| 28 | super().__init__() |
| 29 | # To remove built-in commands entirely, delete |
| 30 | # the "do_*" function from the cmd2.Cmd class |
| 31 | del cmd2.Cmd.do_edit |
| 32 | ``` |
| 33 | |
| 34 | ## Hide A Command |
| 35 | |
| 36 | When a command is hidden, it won't show up in the help menu and it won't tab-complete, but if the |
| 37 | user knows it's there and types the command, it will be executed. You hide a command by adding it to |
| 38 | the `hidden_commands` list: |
| 39 | |
| 40 | ```py |
| 41 | class HiddenCommands(cmd2.Cmd): |
| 42 | """An app which demonstrates how to hide a command""" |
| 43 | def __init__(self): |
| 44 | super().__init__() |
| 45 | self.hidden_commands.append('py') |
| 46 | ``` |
| 47 | |
| 48 | As shown above, you would typically do this as part of initializing your application. If you decide |
| 49 | you want to unhide a command later in the execution of your application, you can by doing: |
| 50 | |
| 51 | ```py |
| 52 | self.hidden_commands = [cmd for cmd in self.hidden_commands if cmd != 'py'] |
| 53 | ``` |
| 54 | |
| 55 | You might be thinking that the list comprehension is overkill and you'd rather do something like: |
| 56 | |
| 57 | ```py |
| 58 | self.hidden_commands.remove('py') |
| 59 | ``` |
| 60 | |
| 61 | You may be right, but `remove()` will raise a `ValueError` if `py` isn't in the list, and it will |
| 62 | only remove the first one if it's in the list multiple times. |
| 63 | |
| 64 | ## Disable A Command |
| 65 | |
| 66 | One way to disable a command is to add code to the command method which determines whether the |
| 67 | command should be executed or not. If the command should not be executed, your code can print an |
| 68 | appropriate error message and return. |
| 69 | |
| 70 | `cmd2` also provides another way to accomplish the same thing. Here's a simple app which disables |
| 71 | the `open` command if the door is locked: |
| 72 | |
| 73 | ```py |
| 74 | class DisabledCommands(cmd2.Cmd): |
| 75 | """An application which disables and enables commands""" |
| 76 | |
| 77 | def do_lock(self, line): |
| 78 | self.disable_command('open', "you can't open the door because it is locked") |
| 79 | self.poutput('the door is locked') |
| 80 | |
| 81 | def do_unlock(self, line): |
| 82 | self.enable_command('open') |
| 83 | self.poutput('the door is unlocked') |
| 84 | |
| 85 | def do_open(self, line): |
| 86 | """open the door""" |
| 87 | self.poutput('opening the door') |
| 88 | ``` |
| 89 | |
| 90 | This method has the added benefit of removing disabled commands from the help menu. But, this method |
| 91 | only works if you know in advance that the command should be disabled, and if the conditions for |
| 92 | re-enabling it are likewise known in advance. |
| 93 | |
| 94 | ## Disable A Category of Commands |
| 95 | |
| 96 | You can group or categorize commands as shown in |
| 97 | [Categorizing Command](./help.md#categorizing-commands). If you do so, you can disable and enable |
| 98 | all the commands in a category with a single method call. Say you have created a category of |
| 99 | commands called "Server Information". You can disable all commands in that category: |
| 100 | |
| 101 | ```py |
| 102 | not_connected_msg = 'You must be connected to use this command' |
| 103 | self.disable_category('Server Information', not_connected_msg) |
| 104 | ``` |
| 105 | |
| 106 | Similarly, you can re-enable all the commands in a category: |
| 107 | |
| 108 | ```py |
| 109 | self.enable_category('Server Information') |
| 110 | ``` |
| 111 | |
| 112 | See [help_categories.py](https://github.com/python-cmd2/cmd2/blob/main/examples/help_categories.py) |
| 113 | for an example of enabling and disabling and entire category of commands dynamically at runtime. |