Skip to content

Commit 1316bdd

Browse files
committed
Updated disable_commands.md
1 parent df159c0 commit 1316bdd

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

docs/features/disable_commands.md

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
When a command has been removed, the command method has been deleted from the object. The command doesn't show up in help, and it can't be executed. This approach is appropriate if you never want a built-in command to be part of your application. Delete the command method in your initialization code:
1212

13+
```py
1314
class RemoveBuiltinCommand(cmd2.Cmd):
1415
"""An app which removes a built-in command from cmd2"""
1516

@@ -18,24 +19,31 @@ When a command has been removed, the command method has been deleted from the ob
1819
# To remove built-in commands entirely, delete
1920
# the "do_*" function from the cmd2.Cmd class
2021
del cmd2.Cmd.do_edit
22+
```
2123

2224
## Hide A Command
2325

2426
When a command is hidden, it won't show up in the help menu, but if the user knows it's there and types the command, it will be executed. You hide a command by adding it to the `hidden_commands` list:
2527

26-
class HiddenCommands(cmd2.Cmd):
27-
""An app which demonstrates how to hide a command"""
28-
def __init__(self):
29-
super().__init__()
30-
self.hidden_commands.append('py')
28+
```py
29+
class HiddenCommands(cmd2.Cmd):
30+
"""An app which demonstrates how to hide a command"""
31+
def __init__(self):
32+
super().__init__()
33+
self.hidden_commands.append('py')
34+
```
3135

3236
As shown above, you would typically do this as part of initializing your application. If you decide you want to unhide a command later in the execution of your application, you can by doing:
3337

34-
self.hidden_commands = [cmd for cmd in self.hidden_commands if cmd != 'py']
38+
```py
39+
self.hidden_commands = [cmd for cmd in self.hidden_commands if cmd != 'py']
40+
```
3541

3642
You might be thinking that the list comprehension is overkill and you'd rather do something like:
3743

38-
self.hidden_commands.remove('py')
44+
```py
45+
self.hidden_commands.remove('py')
46+
```
3947

4048
You may be right, but `remove()` will raise a `ValueError` if `py` isn't in the list, and it will only remove the first one if it's in the list multiple times.
4149

@@ -45,30 +53,36 @@ One way to disable a command is to add code to the command method which determin
4553

4654
`cmd2` also provides another way to accomplish the same thing. Here's a simple app which disables the `open` command if the door is locked:
4755

48-
class DisabledCommands(cmd2.Cmd):
49-
"""An application which disables and enables commands"""
56+
```py
57+
class DisabledCommands(cmd2.Cmd):
58+
"""An application which disables and enables commands"""
5059

51-
def do_lock(self, line):
52-
self.disable_command('open', "you can't open the door because it is locked")
53-
self.poutput('the door is locked')
60+
def do_lock(self, line):
61+
self.disable_command('open', "you can't open the door because it is locked")
62+
self.poutput('the door is locked')
5463

55-
def do_unlock(self, line):
56-
self.enable_command('open')
57-
self.poutput('the door is unlocked')
64+
def do_unlock(self, line):
65+
self.enable_command('open')
66+
self.poutput('the door is unlocked')
5867

59-
def do_open(self, line):
60-
"""open the door"""
61-
self.poutput('opening the door')
68+
def do_open(self, line):
69+
"""open the door"""
70+
self.poutput('opening the door')
71+
```
6272

6373
This method has the added benefit of removing disabled commands from the help menu. But, this method only works if you know in advance that the command should be disabled, and if the conditions for re-enabling it are likewise known in advance.
6474

6575
## Disable A Category of Commands
6676

67-
You can group or categorize commands as shown in `features/help:Categorizing Commands`{.interpreted-text role="ref"}. If you do so, you can disable and enable all the commands in a category with a single method call. Say you have created a category of commands called "Server Information". You can disable all commands in that category:
77+
You can group or categorize commands as shown in [Categorizing Command](./help.md#categorizing-commands). If you do so, you can disable and enable all the commands in a category with a single method call. Say you have created a category of commands called "Server Information". You can disable all commands in that category:
6878

69-
not_connected_msg = 'You must be connected to use this command'
70-
self.disable_category('Server Information', not_connected_msg)
79+
```py
80+
not_connected_msg = 'You must be connected to use this command'
81+
self.disable_category('Server Information', not_connected_msg)
82+
```
7183

7284
Similarly, you can re-enable all the commands in a category:
7385

74-
self.enable_category('Server Information')
86+
```py
87+
self.enable_category('Server Information')
88+
```

0 commit comments

Comments
 (0)