Skip to content

Commit 6b84bd4

Browse files
committed
More documentation updates
1 parent 7460055 commit 6b84bd4

File tree

6 files changed

+69
-51
lines changed

6 files changed

+69
-51
lines changed

docs/features/builtin_commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ history. See [History](history.md) for more information.
3535

3636
### ipy (optional)
3737

38-
This optional opt-in command enters an interactive IPython shell. See
38+
This optional opt-in command enters an interactive :simple-jupyter: IPython shell. See
3939
[IPython (optional)](./embedded_python_shells.md#ipython-optional) for more information.
4040

4141
### macro

docs/features/completion.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ user. These include the following example cases:
7878
- A previous command line argument that determines the data set being completed is invalid
7979
- Tab completion hints
8080

81-
`cmd2` provides the `cmd2.exceptions.CompletionError` exception class for this capability. If an
82-
error occurs in which it is more desirable to display a message than a stack trace, then raise a
81+
`cmd2` provides the [CompletionError][cmd2.CompletionError] exception class for this capability. If
82+
an error occurs in which it is more desirable to display a message than a stack trace, then raise a
8383
`CompletionError`. By default, the message displays in red like an error. However, `CompletionError`
8484
has a member called `apply_style`. Set this False if the error style should not be applied. For
8585
instance, `ArgparseCompleter` sets it to False when displaying completion hints.
@@ -90,7 +90,7 @@ When using `cmd2`'s [@with_argparser][cmd2.with_argparser] decorator, `cmd2` pro
9090
completion of flag names.
9191

9292
Tab completion of argument values can be configured by using one of three parameters to
93-
`argparse.ArgumentParser.add_argument`
93+
[argparse.ArgumentParser.add_argument](https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument)
9494

9595
- `choices`
9696
- `choices_provider`
@@ -105,17 +105,17 @@ example for a demonstration of how to use the `choices_provider` parameter. See
105105
[argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
106106
example for a demonstration of how to use the `completer` parameter.
107107

108-
When tab completing flags or argument values for a `cmd2` command using one of these decorators,
109-
`cmd2` keeps track of state so that once a flag has already previously been provided, it won't
110-
attempt to tab complete it again. When no completion results exist, a hint for the current argument
111-
will be displayed to help the user.
108+
When tab completing flags or argument values for a `cmd2` command using the `@with_argparser`
109+
decorator, `cmd2` keeps track of state so that once a flag has already previously been provided, it
110+
won't attempt to tab complete it again. When no completion results exist, a hint for the current
111+
argument will be displayed to help the user.
112112

113113
## CompletionItem For Providing Extra Context
114114

115115
When tab completing things like a unique ID from a database, it can often be beneficial to provide
116116
the user with some extra context about the item being completed, such as a description. To
117-
facilitate this, `cmd2` defines the `cmd2.CompletionItem` class which can be returned from any of
118-
the 3 completion parameters: `choices`, `choices_provider`, and `completer`.
117+
facilitate this, `cmd2` defines the [CompletionItem][cmd2.CompletionItem] class which can be
118+
returned from any of the 3 completion parameters: `choices`, `choices_provider`, and `completer`.
119119

120120
See the
121121
[argparse_completion](https://github.com/python-cmd2/cmd2/blob/main/examples/argparse_completion.py)
@@ -124,8 +124,8 @@ demonstration of how this is used.
124124

125125
## Custom Completion with `read_input()`
126126

127-
`cmd2` provides `cmd2.Cmd.read_input` as an alternative to Python's `input()` function. `read_input`
128-
supports configurable tab completion and up-arrow history at the prompt. See
127+
`cmd2` provides [cmd2.Cmd.read_input][] as an alternative to Python's `input()` function.
128+
`read_input` supports configurable tab completion and up-arrow history at the prompt. See
129129
[read_input](https://github.com/python-cmd2/cmd2/blob/main/examples/read_input.py) example for a
130130
demonstration.
131131

docs/features/disable_commands.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
`cmd2` allows a developer to:
44

5-
- remove commands included in `cmd2`
6-
- prevent commands from appearing in the help menu (hide commands)
7-
- disable and re-enable commands at runtime
5+
- Remove commands included in `cmd2`
6+
- Prevent commands from appearing in the help menu (hide commands)
7+
- Disable and re-enable commands at runtime
8+
9+
See
10+
[remove_builtin_commands.py](https://github.com/python-cmd2/cmd2/blob/main/examples/remove_builtin_commands.py)
11+
for and example of removing or hiding built-in commands.
12+
13+
See [command_sets.py](https://github.com/python-cmd2/cmd2/blob/main/examples/command_sets.py) for an
14+
example of dynamically enabling and dis-abling custom commands at runtime.
815

916
## Remove A Command
1017

1118
When a command has been removed, the command method has been deleted from the object. The command
12-
doesn't show up in help, and it can't be executed. This approach is appropriate if you never want a
19+
doesn't show up in help and it can't be executed. This approach is appropriate if you never want a
1320
built-in command to be part of your application. Delete the command method in your initialization
1421
code:
1522

@@ -26,9 +33,9 @@ code:
2633

2734
## Hide A Command
2835

29-
When a command is hidden, it won't show up in the help menu, but if the user knows it's there and
30-
types the command, it will be executed. You hide a command by adding it to the `hidden_commands`
31-
list:
36+
When a command is hidden, it won't show up in the help menu and it won't tab-complete, but if the
37+
user knows it's there and types the command, it will be executed. You hide a command by adding it to
38+
the `hidden_commands` list:
3239

3340
```py
3441
class HiddenCommands(cmd2.Cmd):
@@ -101,3 +108,6 @@ Similarly, you can re-enable all the commands in a category:
101108
```py
102109
self.enable_category('Server Information')
103110
```
111+
112+
See [help_categories.py](https://github.com/python-cmd2/cmd2/blob/main/examples/help_categories.py)
113+
for an example of enabling and disabling and entire category of commands dynamically at runtime.

docs/features/embedded_python_shells.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Python (optional)
44

5-
If the `cmd2.Cmd` class is instantiated with `include_py=True`, then the optional `py` command will
6-
be present and run an interactive Python shell:
5+
If the [cmd2.Cmd][] class is instantiated with `include_py=True`, then the optional `py` command
6+
will be present and run an interactive Python shell:
77

88
```py
99
from cmd2 import Cmd
@@ -33,10 +33,12 @@ All of these parameters are also available to Python scripts which run in your a
3333
- has the ability to pass command-line arguments to the scripts invoked
3434

3535
This command provides a more complex and powerful scripting capability than that provided by the
36-
simple text file scripts. Python scripts can include conditional control flow logic. See the
37-
**python_scripting.py** `cmd2` application and the **script_conditional.py** script in the
38-
`examples` source code directory for an example of how to achieve this in your own applications. See
39-
[Scripting](./scripting.md) for an explanation of both scripting methods in **cmd2** applications.
36+
simple text file scripts. Python scripts can include conditional control flow logic. See
37+
[python_scripting.py](https://github.com/python-cmd2/cmd2/blob/main/examples/python_scripting.py)
38+
`cmd2` and the
39+
[conditional.py](https://github.com/python-cmd2/cmd2/blob/main/examples/scripts/conditional.py)
40+
script for an example of how to achieve this in your own applications. See
41+
[Scripting](./scripting.md) for an explanation of both scripting methods in `cmd2` applications.
4042

4143
A simple example of using `run_pyscript` is shown below along with the
4244
[arg_printer](https://github.com/python-cmd2/cmd2/blob/main/examples/scripts/arg_printer.py) script:

docs/features/generating_output.md

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ complex output.
2626

2727
## Ordinary Output
2828

29-
The `cmd2.Cmd.poutput` method is similar to the Python
30-
[built-in print function](https://docs.python.org/3/library/functions.html#print).
31-
`cmd2.Cmd.poutput` adds two conveniences:
29+
The [poutput][cmd2.Cmd.poutput] method is similar to the Python built-in
30+
[print](https://docs.python.org/3/library/functions.html#print) function. `poutput` adds a few
31+
conveniences:
3232

3333
1. Since users can pipe output to a shell command, it catches `BrokenPipeError` and outputs the
3434
contents of `self.broken_pipe_warning` to `stderr`. `self.broken_pipe_warning` defaults to an
3535
empty string so this method will just swallow the exception. If you want to show an error
3636
message, put it in `self.broken_pipe_warning` when you initialize `cmd2.Cmd`.
3737
2. It examines and honors the [allow_style](./settings.md#allow_style) setting. See
3838
[Colored Output](#colored-output) below for more details.
39+
3. It allows printing arbitrary `rich` renderable objects which can get visually quite complex.
3940

4041
Here's a simple command that shows this method in action:
4142

@@ -48,20 +49,22 @@ def do_echo(self, args):
4849
## Error Messages
4950

5051
When an error occurs in your program, you can display it on `sys.stderr` by calling the
51-
`.cmd2.Cmd.perror` method. By default this method applies `Cmd2Style.ERROR` to the output.
52+
[perror][cmd2.Cmd.perror] method. By default this method applies
53+
[Cmd2Style.ERROR][cmd2.Cmd2Style.ERROR] to the output.
5254

5355
## Warning Messages
5456

55-
`cmd2.Cmd.pwarning` is just like `cmd2.Cmd.perror` but applies `Cmd2Style.WARNING` to the output.
57+
[pwarning][cmd2.Cmd.pwarning] is just like `cmd2.Cmd.perror` but applies `Cmd2Style.WARNING` to the
58+
output.
5659

5760
## Feedback
5861

5962
You may have the need to display information to the user which is not intended to be part of the
6063
generated output. This could be debugging information or status information about the progress of
6164
long running commands. It's not output, it's not error messages, it's feedback. If you use the
6265
[Timing](./settings.md#timing) setting, the output of how long it took the command to run will be
63-
output as feedback. You can use the `cmd2.Cmd.pfeedback` method to produce this type of output, and
64-
several [Settings](./settings.md) control how it is handled.
66+
output as feedback. You can use the [pfeedback][cmd2.Cmd.pfeedback] method to produce this type of
67+
output, and several [Settings](./settings.md) control how it is handled.
6568

6669
If the [quiet](./settings.md#quiet) setting is `True`, then calling `cmd2.Cmd.pfeedback` produces no
6770
output. If [quiet](./settings.md#quiet) is `False`, the
@@ -71,17 +74,19 @@ send the output to `stdout` or `stderr`.
7174
## Exceptions
7275

7376
If your app catches an exception and you would like to display the exception to the user, the
74-
`cmd2.Cmd.pexcept` method can help. The default behavior is to just display the message contained
75-
within the exception. However, if the [debug](./settings.md#debug) setting is `True`, then the
76-
entire stack trace will be displayed.
77+
[pexcept][cmd2.Cmd.pexcept] method can help. The default behavior is to just display the message
78+
contained within the exception. However, if the [debug](./settings.md#debug) setting is `True`, then
79+
the entire stack trace will be displayed.
7780

7881
## Paging Output
7982

8083
If you know you are going to generate a lot of output, you may want to display it in a way that the
8184
user can scroll forwards and backwards through it. If you pass all of the output to be displayed in
82-
a single call to `.cmd2.Cmd.ppaged`, it will be piped to an operating system appropriate shell
83-
command to page the output. On Windows, the output is piped to `more`; on Unix-like operating
84-
systems like MacOS and Linux, it is piped to `less`.
85+
a single call to [ppaged][cmd2.Cmd.ppaged], it will be piped to an operating system appropriate
86+
shell command to page the output. On Windows, the output is piped to
87+
[more](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/more); on
88+
Unix-like operating systems like MacOS and Linux, it is piped to
89+
[less](https://man7.org/linux/man-pages/man1/less.1.html).
8590

8691
## Colored Output
8792

@@ -102,20 +107,21 @@ all colors available to your `cmd2` application.
102107

103108
### Custom Themes
104109

105-
`cmd2` uses a `rich` `Theme` object to define styles for various UI elements. You can define your
106-
own custom theme using `cmd2.rich_utils.set_theme`. See the
110+
`cmd2` uses a `rich` [Theme](https://rich.readthedocs.io/en/stable/reference/theme.html) object to
111+
define styles for various UI elements. You can define your own custom theme using
112+
[cmd2.rich_utils.set_theme][]. See the
107113
[rich_theme.py](https://github.com/python-cmd2/cmd2/blob/main/examples/rich_theme.py) example for
108114
more information.
109115

110116
After adding the desired escape sequences to your output, you should use one of these methods to
111117
present the output to the user:
112118

113-
- `cmd2.Cmd.poutput`
114-
- `cmd2.Cmd.perror`
115-
- `cmd2.Cmd.pwarning`
116-
- `cmd2.Cmd.pexcept`
117-
- `cmd2.Cmd.pfeedback`
118-
- `cmd2.Cmd.ppaged`
119+
- [cmd2.Cmd.poutput][]
120+
- [cmd2.Cmd.perror][]
121+
- [cmd2.Cmd.pwarning][]
122+
- [cmd2.Cmd.pexcept][]
123+
- [cmd2.Cmd.pfeedback][]
124+
- [cmd2.Cmd.ppaged][]
119125

120126
These methods all honor the [allow_style](./settings.md#allow_style) setting, which users can modify
121127
to control whether these escape codes are passed through to the terminal or not.
@@ -125,9 +131,9 @@ to control whether these escape codes are passed through to the terminal or not.
125131
If you would like to generate output which is left, center, or right aligned within a specified
126132
width or the terminal width, the following functions can help:
127133

128-
- `cmd2.string_utils.align_left`
129-
- `cmd2.string_utils.align_center`
130-
- `cmd2.string_utils.align_right`
134+
- [cmd2.string_utils.align_left][]
135+
- [cmd2.string_utils.align_center][]
136+
- [cmd2.string_utils.align_right][]
131137

132138
These functions differ from Python's string justifying functions in that they support characters
133139
with display widths greater than 1. Additionally, ANSI style sequences are safely ignored and do not
@@ -141,6 +147,6 @@ you can pad it appropriately with spaces. However, there are categories of Unico
141147
occupy 2 cells, and other that occupy 0. To further complicate matters, you might have included ANSI
142148
escape sequences in the output to generate colors on the terminal.
143149

144-
The `cmd2.string_utils.str_width` function solves both of these problems. Pass it a string, and
150+
The [cmd2.string_utils.str_width][] function solves both of these problems. Pass it a string, and
145151
regardless of which Unicode characters and ANSI text style escape sequences it contains, it will
146152
tell you how many characters on the screen that string will consume when printed.

examples/remove_builtin_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self) -> None:
1818
super().__init__()
1919

2020
# To hide commands from displaying in the help menu, add them to the hidden_commands list
21-
self.hidden_commands.append('py')
21+
self.hidden_commands.append('history')
2222

2323
# To remove built-in commands entirely, delete their "do_*" function from the cmd2.Cmd class
2424
del cmd2.Cmd.do_edit

0 commit comments

Comments
 (0)