Debian Codegraph @

examples/help_categories.py

raw symbols SCIP
1 #!/usr/bin/env python
2 """A sample application for tagging categories on commands.
3
4 It also demonstrates the effects of decorator order when it comes to argparse errors occurring.
5 """
6
7 import functools
8
9 import cmd2
10 from cmd2 import COMMAND_NAME
11
12
13 def my_decorator(f):
14 @functools.wraps(f)
15 def wrapper(*args, **kwds):
16 print("Calling decorated function")
17 return f(*args, **kwds)
18
19 return wrapper
20
21
22 class HelpCategories(cmd2.Cmd):
23 """Example cmd2 application."""
24
25 START_TIMES = ("now", "later", "sometime", "whenever")
26
27 # Command categories
28 CMD_CAT_CONNECTING = "Connecting"
29 CMD_CAT_APP_MGMT = "Application Management"
30 CMD_CAT_SERVER_INFO = "Server Information"
31
32 # Show all other commands in "Other" category
33 cmd2.Cmd.DEFAULT_CATEGORY = "Other"
34
35 def __init__(self) -> None:
36 super().__init__()
37
38 def do_connect(self, _) -> None:
39 """Connect command."""
40 self.poutput("Connect")
41
42 # Tag the above command functions under the category Connecting
43 cmd2.categorize(do_connect, CMD_CAT_CONNECTING)
44
45 @cmd2.with_category(CMD_CAT_CONNECTING)
46 def do_which(self, _) -> None:
47 """Which command."""
48 self.poutput("Which")
49
50 def do_list(self, _) -> None:
51 """List command."""
52 self.poutput("List")
53
54 def do_deploy(self, _) -> None:
55 """Deploy command."""
56 self.poutput("Deploy")
57
58 start_parser = cmd2.Cmd2ArgumentParser(
59 description="Start",
60 epilog="my_decorator runs even with argparse errors",
61 )
62 start_parser.add_argument("when", choices=START_TIMES, help="Specify when to start")
63
64 @my_decorator
65 @cmd2.with_argparser(start_parser)
66 def do_start(self, _) -> None:
67 """Start command."""
68 self.poutput("Start")
69
70 def do_sessions(self, _) -> None:
71 """Sessions command."""
72 self.poutput("Sessions")
73
74 def do_redeploy(self, _) -> None:
75 """Redeploy command."""
76 self.poutput("Redeploy")
77
78 restart_parser = cmd2.Cmd2ArgumentParser(
79 description="Restart",
80 epilog="my_decorator does not run when argparse errors",
81 )
82 restart_parser.add_argument("when", choices=START_TIMES, help="Specify when to restart")
83
84 @cmd2.with_argparser(restart_parser)
85 @cmd2.with_category(CMD_CAT_APP_MGMT)
86 @my_decorator
87 def do_restart(self, _) -> None:
88 """Restart command."""
89 self.poutput("Restart")
90
91 def do_expire(self, _) -> None:
92 """Expire command."""
93 self.poutput("Expire")
94
95 def do_undeploy(self, _) -> None:
96 """Undeploy command."""
97 self.poutput("Undeploy")
98
99 def do_stop(self, _) -> None:
100 """Stop command."""
101 self.poutput("Stop")
102
103 def do_findleakers(self, _) -> None:
104 """Find Leakers command."""
105 self.poutput("Find Leakers")
106
107 # Tag the above command functions under the category Application Management
108 cmd2.categorize(
109 (do_list, do_deploy, do_start, do_sessions, do_redeploy, do_expire, do_undeploy, do_stop, do_findleakers),
110 CMD_CAT_APP_MGMT,
111 )
112
113 def do_resources(self, _) -> None:
114 """Resources command."""
115 self.poutput("Resources")
116
117 def do_status(self, _) -> None:
118 """Status command."""
119 self.poutput("Status")
120
121 def do_serverinfo(self, _) -> None:
122 """Server Info command."""
123 self.poutput("Server Info")
124
125 def do_thread_dump(self, _) -> None:
126 """Thread Dump command."""
127 self.poutput("Thread Dump")
128
129 def do_sslconnectorciphers(self, _) -> None:
130 """SSL Connector Ciphers command is an example of a command that contains
131 multiple lines of help information for the user. Each line of help in a
132 contiguous set of lines will be printed and aligned in the verbose output
133 provided with 'help --verbose'.
134
135 This is after a blank line and won't de displayed in the verbose help
136 """
137 self.poutput("SSL Connector Ciphers")
138
139 def do_vminfo(self, _) -> None:
140 """VM Info command."""
141 self.poutput("VM Info")
142
143 # Tag the above command functions under the category Server Information
144 cmd2.categorize(do_resources, CMD_CAT_SERVER_INFO)
145 cmd2.categorize(do_status, CMD_CAT_SERVER_INFO)
146 cmd2.categorize(do_serverinfo, CMD_CAT_SERVER_INFO)
147 cmd2.categorize(do_thread_dump, CMD_CAT_SERVER_INFO)
148 cmd2.categorize(do_sslconnectorciphers, CMD_CAT_SERVER_INFO)
149 cmd2.categorize(do_vminfo, CMD_CAT_SERVER_INFO)
150
151 # The following command functions don't have the HELP_CATEGORY attribute set
152 # and show up in the 'Other' group
153 def do_config(self, _) -> None:
154 """Config command."""
155 self.poutput("Config")
156
157 def do_version(self, _) -> None:
158 """Version command."""
159 self.poutput(cmd2.__version__)
160
161 @cmd2.with_category("Command Management")
162 def do_disable_commands(self, _) -> None:
163 """Disable the Application Management commands."""
164 message_to_print = f"{COMMAND_NAME} is not available while {self.CMD_CAT_APP_MGMT} commands are disabled"
165 self.disable_category(self.CMD_CAT_APP_MGMT, message_to_print)
166 self.poutput("The Application Management commands have been disabled")
167
168 @cmd2.with_category("Command Management")
169 def do_enable_commands(self, _) -> None:
170 """Enable the Application Management commands."""
171 self.enable_category(self.CMD_CAT_APP_MGMT)
172 self.poutput("The Application Management commands have been enabled")
173
174
175 if __name__ == "__main__":
176 import sys
177
178 c = HelpCategories()
179 sys.exit(c.cmdloop())