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