Skip to content

Commit b9d21a2

Browse files
committed
Document hiding and disabling commands
For #765
1 parent 9fd5f62 commit b9d21a2

File tree

5 files changed

+116
-52
lines changed

5 files changed

+116
-52
lines changed

docs/examples/index.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ Examples
55
:maxdepth: 1
66

77
first_app
8-
remove_builtin_commands
98
alternate_event_loops

docs/examples/remove_builtin_commands.rst

Lines changed: 0 additions & 41 deletions
This file was deleted.

docs/features/commands.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,12 @@ Exit Codes
140140
``cmd2`` has basic infrastructure to support sh/ksh/csh/bash type exit codes.
141141
The ``cmd2.Cmd`` object sets an ``exit_code`` attribute to zero when it is
142142
instantiated. The value of this attribute is returned from the ``cmdloop()``
143-
call, so if you want to get an exit code back to the operating system shell,
144-
you can do so like this::
143+
call. Therefore, if you don't do anything with this attribute in your code,
144+
``cmdloop()`` will (almost) always return zero. There are a few built-in
145+
``cmd2`` commands which set ``exit_code`` to ``-1`` if an error occers.
146+
147+
You can use this capability to easily return your own values to the operating
148+
system shell::
145149

146150
#!/usr/bin/env python
147151
"""A simple cmd2 application."""
@@ -183,9 +187,12 @@ name and message. If `debug` is `true`, ``cmd2`` will display a traceback, and
183187
then display the exception name and message.
184188

185189

186-
Remove Built-in Commands
187-
------------------------
190+
Disabling or Hiding Commands
191+
----------------------------
192+
193+
See :ref:`features/disable_commands:Disabling Commands` for details of how
194+
to:
188195

189-
See the :ref:`examples/remove_builtin_commands:Remove Built-in Commands`
190-
example for information on hiding or removing commands included in ``cmd2``
191-
which you might not want in your application.
196+
- removing commands included in ``cmd2``
197+
- hiding commands from the help menu
198+
- disabling and re-enabling commands at runtime

docs/features/disable_commands.rst

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,103 @@
11
Disabling Commands
22
==================
33

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')

docs/features/help.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Use ``help_method()`` to custom roll your own help messages.
77

88
See :ref:`features/argument_processing:Help Messages`
99

10-
Grouping Commands
11-
-----------------
10+
Categorizing Commands
11+
---------------------
1212

1313
By default, the ``help`` command displays::
1414

0 commit comments

Comments
 (0)