Skip to content

Commit 9fd5f62

Browse files
committed
Add documentation and example for removing built-in commands
For #765
1 parent 65cf54a commit 9fd5f62

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

docs/examples/index.rst

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

77
first_app
8-
removing_builtin_commands
8+
remove_builtin_commands
99
alternate_event_loops
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Remove Built-in Commands
2+
=========================
3+
4+
``cmd2`` comes with a bunch of built-in commands. These commands add lots of
5+
useful functionality, but you might not want them in your application. You can
6+
either hide these commands, or remove them completely.
7+
8+
9+
Hiding Commands
10+
---------------
11+
12+
When a command is hidden, it is still available to run, but it won't show in
13+
the help menu. To hide a command::
14+
15+
class HideBuiltinCommand(cmd2.Cmd):
16+
"""Hide a built-in command."""
17+
18+
def __init__(self):
19+
super().__init__()
20+
21+
# To prevent commands from displaying in the help menu
22+
# add them to the hidden_commands list
23+
self.hidden_commands.append('py')
24+
25+
26+
Removing Commands
27+
-----------------
28+
29+
You can remove a command from your application is defined in ``cmd2.Cmd`` or
30+
inherited from a :ref:`plugin <features/plugins:Plugins>`. Delete the
31+
command method in your initialization code::
32+
33+
class RemoveBuiltinCommand(cmd2.Cmd):
34+
"""Remove an undesired built-in command."""
35+
36+
def __init__(self):
37+
super().__init__()
38+
39+
# To remove built-in commands entirely, delete
40+
# the "do_*" function from the cmd2.Cmd class
41+
del cmd2.Cmd.do_edit

docs/examples/removing_builtin_commands.rst

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

docs/features/commands.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,11 @@ catch it and display it for you. The `debug` :ref:`setting
181181
`debug` is `false`, which is the default, ``cmd2`` will display the exception
182182
name and message. If `debug` is `true`, ``cmd2`` will display a traceback, and
183183
then display the exception name and message.
184+
185+
186+
Remove Built-in Commands
187+
------------------------
188+
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.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import cmd2
1313

1414

15-
class RemoveUnusedBuiltinCommands(cmd2.Cmd):
15+
class RemoveBuiltinCommands(cmd2.Cmd):
1616
""" Example cmd2 application where we remove some unused built-in commands."""
1717

1818
def __init__(self):
@@ -27,5 +27,5 @@ def __init__(self):
2727

2828
if __name__ == '__main__':
2929
import sys
30-
app = RemoveUnusedBuiltinCommands()
30+
app = RemoveBuiltinCommands()
3131
sys.exit(app.cmdloop())

0 commit comments

Comments
 (0)