You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/features/disable_commands.md
+36-22Lines changed: 36 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@
10
10
11
11
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:
12
12
13
+
```py
13
14
classRemoveBuiltinCommand(cmd2.Cmd):
14
15
"""An app which removes a built-in command from cmd2"""
15
16
@@ -18,24 +19,31 @@ When a command has been removed, the command method has been deleted from the ob
18
19
# To remove built-in commands entirely, delete
19
20
# the "do_*" function from the cmd2.Cmd class
20
21
del cmd2.Cmd.do_edit
22
+
```
21
23
22
24
## Hide A Command
23
25
24
26
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:
25
27
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
+
classHiddenCommands(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
+
```
31
35
32
36
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:
33
37
34
-
self.hidden_commands = [cmd for cmd in self.hidden_commands if cmd != 'py']
38
+
```py
39
+
self.hidden_commands = [cmd for cmd inself.hidden_commands if cmd !='py']
40
+
```
35
41
36
42
You might be thinking that the list comprehension is overkill and you'd rather do something like:
37
43
38
-
self.hidden_commands.remove('py')
44
+
```py
45
+
self.hidden_commands.remove('py')
46
+
```
39
47
40
48
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.
41
49
@@ -45,30 +53,36 @@ One way to disable a command is to add code to the command method which determin
45
53
46
54
`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:
47
55
48
-
class DisabledCommands(cmd2.Cmd):
49
-
"""An application which disables and enables commands"""
56
+
```py
57
+
classDisabledCommands(cmd2.Cmd):
58
+
"""An application which disables and enables commands"""
50
59
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
+
defdo_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')
54
63
55
-
def do_unlock(self, line):
56
-
self.enable_command('open')
57
-
self.poutput('the door is unlocked')
64
+
defdo_unlock(self, line):
65
+
self.enable_command('open')
66
+
self.poutput('the door is unlocked')
58
67
59
-
def do_open(self, line):
60
-
"""open the door"""
61
-
self.poutput('opening the door')
68
+
defdo_open(self, line):
69
+
"""open the door"""
70
+
self.poutput('opening the door')
71
+
```
62
72
63
73
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.
64
74
65
75
## Disable A Category of Commands
66
76
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:
68
78
69
-
not_connected_msg = 'You must be connected to use this command'
0 commit comments