Skip to content

Commit 5d1e981

Browse files
committed
Added ability to group commands by category when printing the help menu.
Added example of multiple commands grouped by categories
1 parent 98301d0 commit 5d1e981

File tree

2 files changed

+151
-3
lines changed

2 files changed

+151
-3
lines changed

cmd2.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class RlType(Enum):
209209
# Used for tab completion and word breaks. Do not change.
210210
QUOTES = ['"', "'"]
211211
REDIRECTION_CHARS = ['|', '<', '>']
212+
HELP_CATEGORY = 'help_category'
212213

213214

214215
def set_posix_shlex(val):
@@ -2913,18 +2914,34 @@ def _help_menu(self):
29132914

29142915
cmds_doc = []
29152916
cmds_undoc = []
2917+
cmds_cats = {}
29162918

29172919
for command in visible_commands:
29182920
if command in help_topics:
29192921
cmds_doc.append(command)
29202922
help_topics.remove(command)
29212923
elif getattr(self, self._func_named(command)).__doc__:
2922-
cmds_doc.append(command)
2924+
if hasattr(getattr(self, self._func_named(command)), HELP_CATEGORY):
2925+
category = getattr(getattr(self, self._func_named(command)), HELP_CATEGORY)
2926+
cmds_cats.setdefault(category, [])
2927+
cmds_cats[category].append(command)
2928+
else:
2929+
cmds_doc.append(command)
29232930
else:
29242931
cmds_undoc.append(command)
29252932

2926-
self.poutput("%s\n" % str(self.doc_leader))
2927-
self.print_topics(self.doc_header, cmds_doc, 15, 80)
2933+
if len(cmds_cats) == 0:
2934+
# No categories found, fall back to standard behavior
2935+
self.poutput("%s\n" % str(self.doc_leader))
2936+
self.print_topics(self.doc_header, cmds_doc, 15, 80)
2937+
else:
2938+
# Categories found, Organize all commands by category
2939+
self.poutput("%s\n" % str(self.doc_leader))
2940+
self.poutput("%s\n\n" % str(self.doc_header))
2941+
for category in cmds_cats:
2942+
self.print_topics(category, cmds_cats[category], 15, 80)
2943+
self.print_topics('Other', cmds_doc, 15, 80)
2944+
29282945
self.print_topics(self.misc_header, help_topics, 15, 80)
29292946
self.print_topics(self.undoc_header, cmds_undoc, 15, 80)
29302947

examples/help_categories.py

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
A sample application for tagging categories on commands.
5+
"""
6+
7+
from cmd2 import Cmd, HELP_CATEGORY, __version__
8+
9+
10+
class HelpCategories(Cmd):
11+
""" Example cmd2 application. """
12+
13+
# Command categories
14+
CMD_CAT_CONNECTING = 'Connecting'
15+
CMD_CAT_APP_MGMT = 'Application Management'
16+
CMD_CAT_SERVER_INFO = 'Server Information'
17+
18+
def __init__(self):
19+
# Set use_ipython to True to enable the "ipy" command which embeds and interactive IPython shell
20+
Cmd.__init__(self, use_ipython=False)
21+
22+
def do_connect(self, _):
23+
"""Connect command"""
24+
self.poutput('Connect')
25+
26+
def do_which(self, _):
27+
"""Which command"""
28+
self.poutput('Which')
29+
30+
# Tag the above command functions under the category Connecting
31+
setattr(do_connect, HELP_CATEGORY, CMD_CAT_CONNECTING)
32+
setattr(do_which, HELP_CATEGORY, CMD_CAT_CONNECTING)
33+
34+
def do_list(self, _):
35+
"""List command"""
36+
self.poutput('List')
37+
38+
def do_deploy(self, _):
39+
"""Deploy command"""
40+
self.poutput('Which')
41+
42+
def do_start(self, _):
43+
"""Start command"""
44+
self.poutput('Start')
45+
46+
def do_sessions(self, _):
47+
"""Sessions command"""
48+
self.poutput('Sessions')
49+
50+
def do_redeploy(self, _):
51+
"""Redeploy command"""
52+
self.poutput('Redeploy')
53+
54+
def do_restart(self, _):
55+
"""Restart command"""
56+
self.poutput('Restart')
57+
58+
def do_expire(self, _):
59+
"""Expire command"""
60+
self.poutput('Expire')
61+
62+
def do_undeploy(self, _):
63+
"""Undeploy command"""
64+
self.poutput('Undeploy')
65+
66+
def do_stop(self, _):
67+
"""Stop command"""
68+
self.poutput('Stop')
69+
70+
def do_findleakers(self, _):
71+
"""Find Leakers command"""
72+
self.poutput('Find Leakers')
73+
74+
# Tag the above command functions under the category Application Management
75+
setattr(do_list, HELP_CATEGORY, CMD_CAT_APP_MGMT)
76+
setattr(do_deploy, HELP_CATEGORY, CMD_CAT_APP_MGMT)
77+
setattr(do_start, HELP_CATEGORY, CMD_CAT_APP_MGMT)
78+
setattr(do_sessions, HELP_CATEGORY, CMD_CAT_APP_MGMT)
79+
setattr(do_redeploy, HELP_CATEGORY, CMD_CAT_APP_MGMT)
80+
setattr(do_restart, HELP_CATEGORY, CMD_CAT_APP_MGMT)
81+
setattr(do_expire, HELP_CATEGORY, CMD_CAT_APP_MGMT)
82+
setattr(do_undeploy, HELP_CATEGORY, CMD_CAT_APP_MGMT)
83+
setattr(do_stop, HELP_CATEGORY, CMD_CAT_APP_MGMT)
84+
setattr(do_findleakers, HELP_CATEGORY, CMD_CAT_APP_MGMT)
85+
86+
def do_resources(self, _):
87+
"""Resources command"""
88+
self.poutput('Resources')
89+
90+
def do_status(self, _):
91+
"""Status command"""
92+
self.poutput('Status')
93+
94+
def do_serverinfo(self, _):
95+
"""Server Info command"""
96+
self.poutput('Server Info')
97+
98+
def do_thread_dump(self, _):
99+
"""Thread Dump command"""
100+
self.poutput('Thread Dump')
101+
102+
def do_sslconnectorciphers(self, _):
103+
"""SSL Connector Ciphers command"""
104+
self.poutput('SSL Connector Ciphers')
105+
106+
def do_vminfo(self, _):
107+
"""VM Info command"""
108+
self.poutput('VM Info')
109+
110+
# Tag the above command functions under the category Server Information
111+
setattr(do_resources, HELP_CATEGORY, CMD_CAT_SERVER_INFO)
112+
setattr(do_status, HELP_CATEGORY, CMD_CAT_SERVER_INFO)
113+
setattr(do_serverinfo, HELP_CATEGORY, CMD_CAT_SERVER_INFO)
114+
setattr(do_thread_dump, HELP_CATEGORY, CMD_CAT_SERVER_INFO)
115+
setattr(do_sslconnectorciphers, HELP_CATEGORY, CMD_CAT_SERVER_INFO)
116+
setattr(do_vminfo, HELP_CATEGORY, CMD_CAT_SERVER_INFO)
117+
118+
# The following command functions don't have the HELP_CATEGORY attribute set
119+
# and show up in the 'Other' group
120+
def do_config(self, _):
121+
"""Config command"""
122+
self.poutput('Config')
123+
124+
def do_version(self, _):
125+
"""Version command"""
126+
self.poutput(__version__)
127+
128+
129+
if __name__ == '__main__':
130+
c = HelpCategories()
131+
c.cmdloop()

0 commit comments

Comments
 (0)