Skip to content

Commit 647f072

Browse files
committed
Updated examples and documentation pertaining to disabling commands
1 parent 102b6c1 commit 647f072

File tree

5 files changed

+185
-134
lines changed

5 files changed

+185
-134
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
* Added **-v**, **--verbose** flag
1111
* display history and include expanded commands if they differ from the typed command
1212
* Added ``matches_sort_key`` to override the default way tab completion matches are sorted
13+
* Added ability to disable/enable individual commands and entire categories of commands. When a command
14+
is disabled, it will not show up in the help menu or tab complete. If a user tries to run the command
15+
or call help on it, a command-specific message supplied by the developer will be printed. The following
16+
commands were added to support this feature.
17+
* ``enable_command()``
18+
* ``enable_category()``
19+
* ``disable_command()``
20+
* ``disable_category()``
1321
* Potentially breaking changes
1422
* Made ``cmd2_app`` a positional and required argument of ``AutoCompleter`` since certain functionality now
1523
requires that it can't be ``None``.

cmd2/cmd2.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3654,7 +3654,7 @@ def disable_command(self, command: str, message_to_print: str) -> None:
36543654
"""
36553655
Disable a command and overwrite its functions
36563656
:param command: the command being disabled
3657-
:param message_to_print: what to print when this command or its help function is run while disabled
3657+
:param message_to_print: what to print when this command is run or help is called on it while disabled
36583658
"""
36593659
import functools
36603660

@@ -3683,7 +3683,8 @@ def disable_category(self, category: str, message_to_print: str) -> None:
36833683
"""
36843684
Disable an entire category of commands
36853685
:param category: the category to disable
3686-
:param message_to_print: what to print when anything in this category is run while disabled
3686+
:param message_to_print: what to print when anything in this category is run or help is called on it
3687+
while disabled
36873688
"""
36883689
all_commands = self.get_all_commands()
36893690

docs/argument_processing.rst

Lines changed: 0 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -195,138 +195,6 @@ Which yields:
195195
.. _argparse: https://docs.python.org/3/library/argparse.html
196196

197197

198-
Grouping Commands
199-
=================
200-
201-
By default, the ``help`` command displays::
202-
203-
Documented commands (type help <topic>):
204-
========================================
205-
alias findleakers pyscript sessions status vminfo
206-
config help quit set stop which
207-
connect history redeploy shell thread_dump
208-
deploy list resources shortcuts unalias
209-
edit load restart sslconnectorciphers undeploy
210-
expire py serverinfo start version
211-
212-
If you have a large number of commands, you can optionally group your commands into categories.
213-
Here's the output from the example ``help_categories.py``::
214-
215-
Documented commands (type help <topic>):
216-
217-
Application Management
218-
======================
219-
deploy findleakers redeploy sessions stop
220-
expire list restart start undeploy
221-
222-
Connecting
223-
==========
224-
connect which
225-
226-
Server Information
227-
==================
228-
resources serverinfo sslconnectorciphers status thread_dump vminfo
229-
230-
Other
231-
=====
232-
alias edit history py quit shell unalias
233-
config help load pyscript set shortcuts version
234-
235-
236-
There are 2 methods of specifying command categories, using the ``@with_category`` decorator or with the
237-
``categorize()`` function. Once a single command category is detected, the help output switches to a categorized
238-
mode of display. All commands with an explicit category defined default to the category `Other`.
239-
240-
Using the ``@with_category`` decorator::
241-
242-
@with_category(CMD_CAT_CONNECTING)
243-
def do_which(self, _):
244-
"""Which command"""
245-
self.poutput('Which')
246-
247-
Using the ``categorize()`` function:
248-
249-
You can call with a single function::
250-
251-
def do_connect(self, _):
252-
"""Connect command"""
253-
self.poutput('Connect')
254-
255-
# Tag the above command functions under the category Connecting
256-
categorize(do_connect, CMD_CAT_CONNECTING)
257-
258-
Or with an Iterable container of functions::
259-
260-
def do_undeploy(self, _):
261-
"""Undeploy command"""
262-
self.poutput('Undeploy')
263-
264-
def do_stop(self, _):
265-
"""Stop command"""
266-
self.poutput('Stop')
267-
268-
def do_findleakers(self, _):
269-
"""Find Leakers command"""
270-
self.poutput('Find Leakers')
271-
272-
# Tag the above command functions under the category Application Management
273-
categorize((do_undeploy,
274-
do_stop,
275-
do_findleakers), CMD_CAT_APP_MGMT)
276-
277-
The ``help`` command also has a verbose option (``help -v`` or ``help --verbose``) that combines
278-
the help categories with per-command Help Messages::
279-
280-
Documented commands (type help <topic>):
281-
282-
Application Management
283-
================================================================================
284-
deploy Deploy command
285-
expire Expire command
286-
findleakers Find Leakers command
287-
list List command
288-
redeploy Redeploy command
289-
restart usage: restart [-h] {now,later,sometime,whenever}
290-
sessions Sessions command
291-
start Start command
292-
stop Stop command
293-
undeploy Undeploy command
294-
295-
Connecting
296-
================================================================================
297-
connect Connect command
298-
which Which command
299-
300-
Server Information
301-
================================================================================
302-
resources Resources command
303-
serverinfo Server Info command
304-
sslconnectorciphers SSL Connector Ciphers command is an example of a command that contains
305-
multiple lines of help information for the user. Each line of help in a
306-
contiguous set of lines will be printed and aligned in the verbose output
307-
provided with 'help --verbose'
308-
status Status command
309-
thread_dump Thread Dump command
310-
vminfo VM Info command
311-
312-
Other
313-
================================================================================
314-
alias Define or display aliases
315-
config Config command
316-
edit Edit a file in a text editor
317-
help List available commands with "help" or detailed help with "help cmd"
318-
history usage: history [-h] [-r | -e | -s | -o FILE | -t TRANSCRIPT] [arg]
319-
load Runs commands in script file that is encoded as either ASCII or UTF-8 text
320-
py Invoke python command, shell, or script
321-
pyscript Runs a python script file inside the console
322-
quit Exits this application
323-
set usage: set [-h] [-a] [-l] [settable [settable ...]]
324-
shell Execute a command as if at the OS prompt
325-
shortcuts Lists shortcuts available
326-
unalias Unsets aliases
327-
version Version command
328-
329-
330198
Receiving an argument list
331199
==========================
332200

docs/unfreefeatures.rst

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,165 @@ set_window_title()
237237
The easiest way to understand these functions is to see the AsyncPrinting_ example for a demonstration.
238238

239239
.. _AsyncPrinting: https://github.com/python-cmd2/cmd2/blob/master/examples/async_printing.py
240+
241+
242+
Grouping Commands
243+
=================
244+
245+
By default, the ``help`` command displays::
246+
247+
Documented commands (type help <topic>):
248+
========================================
249+
alias findleakers pyscript sessions status vminfo
250+
config help quit set stop which
251+
connect history redeploy shell thread_dump
252+
deploy list resources shortcuts unalias
253+
edit load restart sslconnectorciphers undeploy
254+
expire py serverinfo start version
255+
256+
If you have a large number of commands, you can optionally group your commands into categories.
257+
Here's the output from the example ``help_categories.py``::
258+
259+
Documented commands (type help <topic>):
260+
261+
Application Management
262+
======================
263+
deploy findleakers redeploy sessions stop
264+
expire list restart start undeploy
265+
266+
Connecting
267+
==========
268+
connect which
269+
270+
Server Information
271+
==================
272+
resources serverinfo sslconnectorciphers status thread_dump vminfo
273+
274+
Other
275+
=====
276+
alias edit history py quit shell unalias
277+
config help load pyscript set shortcuts version
278+
279+
280+
There are 2 methods of specifying command categories, using the ``@with_category`` decorator or with the
281+
``categorize()`` function. Once a single command category is detected, the help output switches to a categorized
282+
mode of display. All commands with an explicit category defined default to the category `Other`.
283+
284+
Using the ``@with_category`` decorator::
285+
286+
@with_category(CMD_CAT_CONNECTING)
287+
def do_which(self, _):
288+
"""Which command"""
289+
self.poutput('Which')
290+
291+
Using the ``categorize()`` function:
292+
293+
You can call with a single function::
294+
295+
def do_connect(self, _):
296+
"""Connect command"""
297+
self.poutput('Connect')
298+
299+
# Tag the above command functions under the category Connecting
300+
categorize(do_connect, CMD_CAT_CONNECTING)
301+
302+
Or with an Iterable container of functions::
303+
304+
def do_undeploy(self, _):
305+
"""Undeploy command"""
306+
self.poutput('Undeploy')
307+
308+
def do_stop(self, _):
309+
"""Stop command"""
310+
self.poutput('Stop')
311+
312+
def do_findleakers(self, _):
313+
"""Find Leakers command"""
314+
self.poutput('Find Leakers')
315+
316+
# Tag the above command functions under the category Application Management
317+
categorize((do_undeploy,
318+
do_stop,
319+
do_findleakers), CMD_CAT_APP_MGMT)
320+
321+
The ``help`` command also has a verbose option (``help -v`` or ``help --verbose``) that combines
322+
the help categories with per-command Help Messages::
323+
324+
Documented commands (type help <topic>):
325+
326+
Application Management
327+
================================================================================
328+
deploy Deploy command
329+
expire Expire command
330+
findleakers Find Leakers command
331+
list List command
332+
redeploy Redeploy command
333+
restart usage: restart [-h] {now,later,sometime,whenever}
334+
sessions Sessions command
335+
start Start command
336+
stop Stop command
337+
undeploy Undeploy command
338+
339+
Connecting
340+
================================================================================
341+
connect Connect command
342+
which Which command
343+
344+
Server Information
345+
================================================================================
346+
resources Resources command
347+
serverinfo Server Info command
348+
sslconnectorciphers SSL Connector Ciphers command is an example of a command that contains
349+
multiple lines of help information for the user. Each line of help in a
350+
contiguous set of lines will be printed and aligned in the verbose output
351+
provided with 'help --verbose'
352+
status Status command
353+
thread_dump Thread Dump command
354+
vminfo VM Info command
355+
356+
Other
357+
================================================================================
358+
alias Define or display aliases
359+
config Config command
360+
edit Edit a file in a text editor
361+
help List available commands with "help" or detailed help with "help cmd"
362+
history usage: history [-h] [-r | -e | -s | -o FILE | -t TRANSCRIPT] [arg]
363+
load Runs commands in script file that is encoded as either ASCII or UTF-8 text
364+
py Invoke python command, shell, or script
365+
pyscript Runs a python script file inside the console
366+
quit Exits this application
367+
set usage: set [-h] [-a] [-l] [settable [settable ...]]
368+
shell Execute a command as if at the OS prompt
369+
shortcuts Lists shortcuts available
370+
unalias Unsets aliases
371+
version Version command
372+
373+
374+
Disabling Commands
375+
==================
376+
377+
``cmd2`` supports disabling commands during runtime. This is useful if certain commands should only be available
378+
when the application is in a specific state. When a command is disabled, it will not show up in the help menu or
379+
tab complete. If a user tries to run the command, a command-specific message supplied by the developer will be
380+
printed. The following functions support this feature.
381+
382+
enable_command()
383+
Enable an individual command
384+
385+
enable_category()
386+
Enable an entire category of commands
387+
388+
disable_command()
389+
Disable an individual command and set the message that will print when this command is run or help is called
390+
on it while disabled
391+
392+
disable_category()
393+
Disable an entire category of commands and set the message that will print when anything in this category is
394+
run or help is called on it while disabled
395+
396+
See the definitions of these functions for descriptions of their arguments.
397+
398+
See the ``do_enable_commands()`` and ``do_disable_commands()`` functions in the HelpCategories_ example for
399+
a demonstration.
400+
401+
.. _HelpCategories: https://github.com/python-cmd2/cmd2/blob/master/examples/help_categories.py

examples/help_categories.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,18 @@ def do_version(self, _):
140140
"""Version command"""
141141
self.poutput(cmd2.__version__)
142142

143+
@cmd2.with_category("Command Management")
144+
def do_disable_commands(self, _):
145+
"""Disable the Application Management commands"""
146+
self.disable_category(self.CMD_CAT_APP_MGMT, "Application Management is currently disabled")
147+
self.poutput("The Application Management commands have been disabled")
148+
149+
@cmd2.with_category("Command Management")
150+
def do_enable_commands(self, _):
151+
"""Enable the Application Management commands"""
152+
self.enable_category(self.CMD_CAT_APP_MGMT)
153+
self.poutput("The Application Management commands have been enabled")
154+
143155

144156
if __name__ == '__main__':
145157
c = HelpCategories()

0 commit comments

Comments
 (0)