Skip to content

Commit ef23633

Browse files
committed
Added a convenience function for tagging command categories.
1 parent 5d1e981 commit ef23633

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

cmd2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import sys
4343
import tempfile
4444
import traceback
45+
from typing import Union, Callable
4546
import unittest
4647
from code import InteractiveConsole
4748

@@ -212,6 +213,18 @@ class RlType(Enum):
212213
HELP_CATEGORY = 'help_category'
213214

214215

216+
def categorize(func: Union[Callable, Iterable], category: str):
217+
"""
218+
Categorize a function
219+
The help command output will group this function under the specified category heading
220+
"""
221+
if isinstance(func, Iterable):
222+
for item in func:
223+
setattr(item, HELP_CATEGORY, category)
224+
else:
225+
setattr(func, HELP_CATEGORY, category)
226+
227+
215228
def set_posix_shlex(val):
216229
""" Allows user of cmd2 to choose between POSIX and non-POSIX splitting of args for decorated commands.
217230

examples/help_categories.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
A sample application for tagging categories on commands.
55
"""
66

7-
from cmd2 import Cmd, HELP_CATEGORY, __version__
7+
from cmd2 import Cmd, categorize, __version__
88

99

1010
class HelpCategories(Cmd):
@@ -28,8 +28,8 @@ def do_which(self, _):
2828
self.poutput('Which')
2929

3030
# 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)
31+
categorize(do_connect, CMD_CAT_CONNECTING)
32+
categorize(do_which, CMD_CAT_CONNECTING)
3333

3434
def do_list(self, _):
3535
"""List command"""
@@ -72,16 +72,16 @@ def do_findleakers(self, _):
7272
self.poutput('Find Leakers')
7373

7474
# 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)
75+
categorize((do_list,
76+
do_deploy,
77+
do_start,
78+
do_sessions,
79+
do_redeploy,
80+
do_restart,
81+
do_expire,
82+
do_undeploy,
83+
do_stop,
84+
do_findleakers), CMD_CAT_APP_MGMT)
8585

8686
def do_resources(self, _):
8787
"""Resources command"""
@@ -108,12 +108,12 @@ def do_vminfo(self, _):
108108
self.poutput('VM Info')
109109

110110
# 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)
111+
categorize(do_resources, CMD_CAT_SERVER_INFO)
112+
categorize(do_status, CMD_CAT_SERVER_INFO)
113+
categorize(do_serverinfo, CMD_CAT_SERVER_INFO)
114+
categorize(do_thread_dump, CMD_CAT_SERVER_INFO)
115+
categorize(do_sslconnectorciphers, CMD_CAT_SERVER_INFO)
116+
categorize(do_vminfo, CMD_CAT_SERVER_INFO)
117117

118118
# The following command functions don't have the HELP_CATEGORY attribute set
119119
# and show up in the 'Other' group

0 commit comments

Comments
 (0)