| 1 | # Help |
| 2 | |
| 3 | From our experience, end users rarely read documentation no matter how high-quality or useful that |
| 4 | documentation might be. So it is important that you provide good built-in help within your |
| 5 | application. Fortunately, `cmd2` makes this easy. |
| 6 | |
| 7 | ## Getting Help |
| 8 | |
| 9 | `cmd2` makes it easy for end users of `cmd2` applications to get help via the built-in `help` |
| 10 | command. The `help` command by itself displays a list of the commands available: |
| 11 | |
| 12 | ```text |
| 13 | (Cmd) help |
| 14 | |
| 15 | Documented Commands |
| 16 | ─────────────────── |
| 17 | alias help ipy py run_pyscript set shortcuts |
| 18 | edit history macro quit run_script shell |
| 19 | ``` |
| 20 | |
| 21 | The `help` command can also be used to provide detailed help for a specific command: |
| 22 | |
| 23 | ```text |
| 24 | (Cmd) help quit |
| 25 | Usage: quit [-h] |
| 26 | |
| 27 | Exit this application. |
| 28 | |
| 29 | Optional Arguments: |
| 30 | -h, --help show this help message and exit |
| 31 | ``` |
| 32 | |
| 33 | ## Providing Help |
| 34 | |
| 35 | `cmd2` makes it easy for developers of `cmd2` applications to provide this help. By default, the |
| 36 | help for a command is the docstring for the `do_*` method defining the command - e.g. for a command |
| 37 | **foo**, that command is implemented by defining the `do_foo` method and the docstring for that |
| 38 | method is the help. |
| 39 | |
| 40 | For commands which use the [@with_argparser][cmd2.with_argparser] decorator to parse arguments, help |
| 41 | is provided by `argparse`. See [Help Messages](./argument_processing.md#help-messages) for more |
| 42 | information. |
| 43 | |
| 44 | Occasionally there might be an unusual circumstance where providing static help text isn't good |
| 45 | enough and you want to provide dynamic information in the help text for a command. To meet this |
| 46 | need, if a `help_foo` method is defined to match the `do_foo` method, then that method will be used |
| 47 | to provide the help for command **foo**. This dynamic help is only supported for commands which do |
| 48 | not use an `argparse` decorator because we didn't want different output for `help cmd` than for |
| 49 | `cmd -h`. |
| 50 | |
| 51 | ## Categorizing Commands |
| 52 | |
| 53 | In `cmd2`, the `help` command organizes its output into categories. Every command belongs to a |
| 54 | category, and the display is driven by the `DEFAULT_CATEGORY` class variable. |
| 55 | |
| 56 | There are 3 methods of specifying command categories: |
| 57 | |
| 58 | 1. Using the `DEFAULT_CATEGORY` class variable (Automatic) |
| 59 | 1. Using the [@with_category][cmd2.with_category] decorator (Manual) |
| 60 | 1. Using the [categorize()][cmd2.categorize] function (Manual) |
| 61 | |
| 62 | ### Automatic Categorization |
| 63 | |
| 64 | The most efficient way to categorize commands is by defining the `DEFAULT_CATEGORY` class variable |
| 65 | in your `Cmd` or `CommandSet` class. Any command defined in that class that does not have an |
| 66 | explicit category override will automatically be placed in this category. |
| 67 | |
| 68 | By default, `cmd2.Cmd` defines its `DEFAULT_CATEGORY` as `"Cmd2 Commands"`. |
| 69 | |
| 70 | ```py |
| 71 | class MyApp(cmd2.Cmd): |
| 72 | # All commands defined in this class will be grouped here |
| 73 | DEFAULT_CATEGORY = 'Application Commands' |
| 74 | |
| 75 | def do_echo(self, arg): |
| 76 | """Echo command""" |
| 77 | self.poutput(arg) |
| 78 | ``` |
| 79 | |
| 80 | This also works for [Command Sets](./modular_commands.md): |
| 81 | |
| 82 | ```py |
| 83 | class Plugin(cmd2.CommandSet): |
| 84 | DEFAULT_CATEGORY = 'Plugin Commands' |
| 85 | |
| 86 | def do_plugin_cmd(self, _): |
| 87 | """Plugin command""" |
| 88 | self._cmd.poutput('Plugin') |
| 89 | ``` |
| 90 | |
| 91 | When using inheritance, `cmd2` uses the `DEFAULT_CATEGORY` of the class where the command was |
| 92 | actually defined. This means built-in commands (like `help`, `history`, and `quit`) stay in the |
| 93 | `"Cmd2 Commands"` category, while your commands move to your custom category. |
| 94 | |
| 95 | If you want to rename the built-in category itself, you can do so by reassigning |
| 96 | `cmd2.Cmd.DEFAULT_CATEGORY` at the class level within your `Cmd` subclass: |
| 97 | |
| 98 | ```py |
| 99 | class MyApp(cmd2.Cmd): |
| 100 | # Rename the framework's built-in category |
| 101 | cmd2.Cmd.DEFAULT_CATEGORY = 'Shell Commands' |
| 102 | |
| 103 | # Set the category for your own commands |
| 104 | DEFAULT_CATEGORY = 'Application Commands' |
| 105 | ``` |
| 106 | |
| 107 | For a complete demonstration of this functionality, see the |
| 108 | [default_categories.py](https://github.com/python-cmd2/cmd2/blob/main/examples/default_categories.py) |
| 109 | example. |
| 110 | |
| 111 | ### Manual Categorization |
| 112 | |
| 113 | If you need to move an individual command to a different category than the class default, you can |
| 114 | use the `@with_category` decorator or the `categorize()` function. These manual settings always take |
| 115 | precedence over the `DEFAULT_CATEGORY`. |
| 116 | |
| 117 | Using the `@with_category` decorator: |
| 118 | |
| 119 | ```py |
| 120 | @with_category('Connecting') |
| 121 | def do_which(self, _): |
| 122 | """Which command""" |
| 123 | self.poutput('Which') |
| 124 | ``` |
| 125 | |
| 126 | Using the `categorize()` function: |
| 127 | |
| 128 | You can call with a single function: |
| 129 | |
| 130 | ```py |
| 131 | def do_connect(self, _): |
| 132 | """Connect command""" |
| 133 | self.poutput('Connect') |
| 134 | |
| 135 | # Tag the above command functions under the category Connecting |
| 136 | categorize(do_connect, CMD_CAT_CONNECTING) |
| 137 | ``` |
| 138 | |
| 139 | Or with an Iterable container of functions: |
| 140 | |
| 141 | ```py |
| 142 | def do_undeploy(self, _): |
| 143 | """Undeploy command""" |
| 144 | self.poutput('Undeploy') |
| 145 | |
| 146 | def do_stop(self, _): |
| 147 | """Stop command""" |
| 148 | self.poutput('Stop') |
| 149 | |
| 150 | def do_findleakers(self, _): |
| 151 | """Find Leakers command""" |
| 152 | self.poutput('Find Leakers') |
| 153 | |
| 154 | # Tag the above command functions under the category Application Management |
| 155 | categorize((do_undeploy, |
| 156 | do_stop, |
| 157 | do_findleakers), CMD_CAT_APP_MGMT) |
| 158 | ``` |
| 159 | |
| 160 | The `help` command also has a verbose option (`help -v` or `help --verbose`) that combines the help |
| 161 | categories with per-command help messages: |
| 162 | |
| 163 | Application Management |
| 164 | ───────────────────────────────────── |
| 165 | Name Description |
| 166 | ───────────────────────────────────── |
| 167 | deploy Deploy command. |
| 168 | expire Expire command. |
| 169 | findleakers Find Leakers command. |
| 170 | list List command. |
| 171 | redeploy Redeploy command. |
| 172 | restart Restart command. |
| 173 | sessions Sessions command. |
| 174 | start Start command. |
| 175 | stop Stop command. |
| 176 | undeploy Undeploy command. |
| 177 | |
| 178 | |
| 179 | Command Management |
| 180 | ───────────────────────────────────────────────────────────────── |
| 181 | Name Description |
| 182 | ───────────────────────────────────────────────────────────────── |
| 183 | disable_commands Disable the Application Management commands. |
| 184 | enable_commands Enable the Application Management commands. |
| 185 | |
| 186 | |
| 187 | Connecting |
| 188 | ──────────────────────────── |
| 189 | Name Description |
| 190 | ──────────────────────────── |
| 191 | connect Connect command. |
| 192 | which Which command. |
| 193 | |
| 194 | |
| 195 | Server Information |
| 196 | ───────────────────────────────────────────────────────────────────────────────────────────────── |
| 197 | Name Description |
| 198 | ───────────────────────────────────────────────────────────────────────────────────────────────── |
| 199 | resources Resources command. |
| 200 | serverinfo Server Info command. |
| 201 | sslconnectorciphers SSL Connector Ciphers command is an example of a command that contains |
| 202 | multiple lines of help information for the user. Each line of help in a |
| 203 | contiguous set of lines will be printed and aligned in the verbose output |
| 204 | provided with 'help --verbose'. |
| 205 | status Status command. |
| 206 | thread_dump Thread Dump command. |
| 207 | vminfo VM Info command. |
| 208 | |
| 209 | |
| 210 | Other |
| 211 | ───────────────────────────────────────────────────────────────────────────────────────── |
| 212 | Name Description |
| 213 | ───────────────────────────────────────────────────────────────────────────────────────── |
| 214 | alias Manage aliases. |
| 215 | config Config command. |
| 216 | edit Run a text editor and optionally open a file with it. |
| 217 | help List available commands or provide detailed help for a specific command. |
| 218 | history View, run, edit, save, or clear previously entered commands. |
| 219 | macro Manage macros. |
| 220 | quit Exit this application. |
| 221 | run_pyscript Run Python script within this application's environment. |
| 222 | run_script Run text script. |
| 223 | set Set a settable parameter or show current settings of parameters. |
| 224 | shell Execute a command as if at the OS prompt. |
| 225 | shortcuts List available shortcuts. |
| 226 | version Version command. |
| 227 | |
| 228 | When called with the `-v` flag for verbose help, the one-line description for each command is |
| 229 | provided by the first line of the docstring for that command's associated `do_*` method. |