Skip to content

Commit 6cbc0b4

Browse files
committed
Continuing to update documentation
1 parent ef30d8d commit 6cbc0b4

File tree

5 files changed

+95
-90
lines changed

5 files changed

+95
-90
lines changed

docs/features/history.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ Each time a command is executed (this gets complex, see
1414
[cmd2.Statement][] is appended to `cmd2.Cmd.history`.
1515

1616
`cmd2` adds the option of making this history persistent via optional arguments to
17-
`cmd2.Cmd.__init__`. If you pass a filename in the `persistent_history_file` argument, the contents
18-
of `cmd2.Cmd.history` will be written as compressed JSON to that history file. We chose this format
19-
instead of plain text to preserve the complete `cmd2.Statement` object for each command.
17+
[cmd2.Cmd.\_\_init\_\_][]. If you pass a filename in the `persistent_history_file` argument, the
18+
contents of `cmd2.Cmd.history` will be written as compressed JSON to that history file. We chose
19+
this format instead of plain text to preserve the complete `cmd2.Statement` object for each command.
2020

2121
!!! note
2222

@@ -25,21 +25,22 @@ instead of plain text to preserve the complete `cmd2.Statement` object for each
2525
However, this design choice causes an inconsistency between the `readline` history and the `cmd2` history when you enter an invalid command: it is saved to the `readline` history, but not to the `cmd2` history.
2626

2727
The `cmd2.Cmd.history` attribute, the `cmd2.history.History` class, and the
28-
`cmd2.history.HistoryItem` class are all part of the public API for `cmd2.Cmd`. You could use these
29-
classes to implement write your own `history` command (see below for documentation on how the
30-
included `history` command works).
28+
[cmd2.history.HistoryItem][] class are all part of the public API for `cmd2.Cmd`. You could use
29+
these classes to implement your own `history` command (see below for documentation on how the
30+
built-in `history` command works).
3131

3232
## For Users
3333

34-
You can use the up and down arrow keys to move through the history of previously entered commands.
34+
You can use the :arrow_up: up and :arrow_down: down arrow keys to move through the history of
35+
previously entered commands.
3536

3637
If the `readline` module is installed, you can press `Control-p` to move to the previously entered
3738
command, and `Control-n` to move to the next command. You can also search through the command
3839
history using `Control-r`.
3940

40-
Eric Johnson hosts a nice [readline cheat sheet](http://readline.kablamo.org/emacs.html), or you can
41-
dig into the [GNU Readline User Manual](http://man7.org/linux/man-pages/man3/readline.3.html) for
42-
all the details, including instructions for customizing the key bindings.
41+
You can refer to the [readline cheat sheet](http://readline.kablamo.org/emacs.html) or you can dig
42+
into the [GNU Readline User Manual](http://man7.org/linux/man-pages/man3/readline.3.html) for all
43+
the details, including instructions for customizing the key bindings.
4344

4445
`cmd2` makes a third type of history access available with the `history` command. Each time the user
4546
enters a command, `cmd2` saves the input. The `history` command lets you do interesting things with

docs/features/hooks.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,23 @@ called, the `cmd2.Cmd.postloop` method is called.
5454
Preloop and postloop hook methods are not passed any parameters and any return value is ignored.
5555

5656
The approach of registering hooks instead of overriding methods allows multiple hooks to be called
57-
before the command loop begins or ends. Plugin authors should review [Hooks](./hooks.md) for best
58-
practices writing hooks.
57+
before the command loop begins or ends. Plugin authors should review this page carefully in full for
58+
best practices writing hooks.
5959

6060
## Application Lifecycle Attributes
6161

62-
There are numerous attributes on `cmd2.Cmd` which affect application behavior upon entering or
62+
There are numerous attributes on [cmd2.Cmd][] which affect application behavior upon entering or
6363
during the command loop:
6464

65-
- `cmd2.Cmd.intro` - if provided this serves as the intro banner printed once at start of
65+
- [cmd2.Cmd.intro][] - if provided this serves as the intro banner printed once at start of
6666
application, after `cmd2.Cmd.preloop` is called
67-
- `cmd2.Cmd.prompt` - see [Prompt](./prompt.md) for more information
68-
- `cmd2.Cmd.continuation_prompt` - The prompt issued to solicit input for the 2nd and subsequent
67+
- [cmd2.Cmd.prompt][] - see [Prompt](./prompt.md) for more information
68+
- [cmd2.Cmd.continuation_prompt][] - The prompt issued to solicit input for the 2nd and subsequent
6969
lines of a [Multiline Command](./multiline_commands.md)
70-
- `cmd2.Cmd.echo` - if `True` write the prompt and the command into the output stream
70+
- [cmd2.Cmd.echo][] - if `True` write the prompt and the command into the output stream
7171

72-
In addition, several arguments to `cmd2.Cmd.__init__` also affect the command loop behavior:
72+
In addition, several arguments to [cmd2.Cmd.\_\_init\_\_][cmd2.Cmd.__init__] also affect the command
73+
loop behavior:
7374

7475
- `allow_cli_args` - allows commands to be specified on the operating system command line which are
7576
executed before the command processing loop begins
@@ -84,25 +85,25 @@ application exits:
8485

8586
1. Output the prompt
8687
1. Accept user input
87-
1. Parse user input into a `cmd2.Statement` object
88-
1. Call methods registered with `cmd2.Cmd.register_postparsing_hook()`
88+
1. Parse user input into a [cmd2.Statement][] object
89+
1. Call methods registered with [cmd2.Cmd.register_postparsing_hook][]
8990
1. Redirect output, if user asked for it and it's allowed
9091
1. Start timer
91-
1. Call methods registered with `cmd2.Cmd.register_precmd_hook`
92-
1. Call `cmd2.Cmd.precmd` - for backwards compatibility with `cmd.Cmd`
92+
1. Call methods registered with [cmd2.Cmd.register_precmd_hook][]
93+
1. Call [cmd2.Cmd.precmd][] - for backwards compatibility with `cmd.Cmd`
9394
1. Add statement to [History](./history.md)
9495
1. Call `do_command` method
95-
1. Call methods registered with `cmd2.Cmd.register_postcmd_hook()`
96-
1. Call `cmd2.Cmd.postcmd` - for backwards compatibility with `cmd.Cmd`
96+
1. Call methods registered with [cmd2.Cmd.register_postcmd_hook][]
97+
1. Call [cmd2.Cmd.postcmd][] - for backwards compatibility with `cmd.Cmd`
9798
1. Stop timer and display the elapsed time
9899
1. Stop redirecting output if it was redirected
99-
1. Call methods registered with `cmd2.Cmd.register_cmdfinalization_hook()`
100+
1. Call methods registered with [cmd2.Cmd.register_cmdfinalization_hook][]
100101

101102
By registering hook methods, multiple steps allow you to run code during, and control the flow of
102103
the command processing loop. Be aware that plugins also utilize these hooks, so there may be code
103-
running that is not part of your application. Methods registered for a hook are called in the order
104-
they were registered. You can register a function more than once, and it will be called each time it
105-
was registered.
104+
running that is not directly part of your application code. Methods registered for a hook are called
105+
in the order they were registered. You can register a function more than once, and it will be called
106+
each time it was registered.
106107

107108
Postparsing, precommand, and postcommand hook methods share some common ways to influence the
108109
command processing loop.
@@ -146,7 +147,7 @@ class App(cmd2.Cmd):
146147
a `TypeError` if it has the wrong number of parameters. It will also raise a `TypeError` if the
147148
passed parameter and return value are not annotated as `PostparsingData`.
148149

149-
The hook method will be passed one parameter, a `cmd2.plugin.PostparsingData` object which we will
150+
The hook method will be passed one parameter, a [cmd2.plugin.PostparsingData][] object which we will
150151
refer to as `params`. `params` contains two attributes. `params.statement` is a `cmd2.Statement`
151152
object which describes the parsed user input. There are many useful attributes in the
152153
`cmd2.Statement` object, including `.raw` which contains exactly what the user typed. `params.stop`
@@ -162,10 +163,10 @@ To modify the user input, you create a new `cmd2.Statement` object and return it
162163
be dragons. Instead, use the various attributes in a `cmd2.Statement` object to construct a new
163164
string, and then parse that string to create a new `cmd2.Statement` object.
164165

165-
`cmd2.Cmd` uses an instance of `cmd2.parsing.StatementParser` to parse user input. This instance has
166-
been configured with the proper command terminators, multiline commands, and other parsing related
167-
settings. This instance is available as the `cmd2.Cmd.statement_parser` attribute. Here's a simple
168-
example which shows the proper technique:
166+
`cmd2.Cmd` uses an instance of [cmd2.parsing.StatementParser][] to parse user input. This instance
167+
has been configured with the proper command terminators, multiline commands, and other parsing
168+
related settings. This instance is available as the `cmd2.Cmd.statement_parser` attribute. Here's a
169+
simple example which shows the proper technique:
169170

170171
```py
171172
def myhookmethod(self, params: cmd2.plugin.PostparsingData) -> cmd2.plugin.PostparsingData:
@@ -211,8 +212,8 @@ parameters and return value are not annotated as `PrecommandData`.
211212
You may choose to modify the user input by creating a new `cmd2.Statement` with different properties
212213
(see above). If you do so, assign your new `cmd2.Statement` object to `data.statement`.
213214

214-
The precommand hook must return a `cmd2.plugin.PrecommandData` object. You don't have to create this
215-
object from scratch, you can just return the one passed into the hook.
215+
The precommand hook must return a [cmd2.plugin.PrecommandData][] object. You don't have to create
216+
this object from scratch, you can just return the one passed into the hook.
216217

217218
After all registered precommand hooks have been called, `cmd2.Cmd.precmd` will be called. To retain
218219
full backward compatibility with `cmd.Cmd`, this method is passed a `cmd2.Statement`, not a
@@ -236,7 +237,7 @@ class App(cmd2.Cmd):
236237
return data
237238
```
238239

239-
Your hook will be passed a `cmd2.plugin.PostcommandData` object, which has a
240+
Your hook will be passed a [cmd2.plugin.PostcommandData][] object, which has a
240241
`cmd2.plugin.PostcommandData.statement` attribute that describes the command which was executed. If
241242
your postcommand hook method gets called, you are guaranteed that the command method was called, and
242243
that it didn't raise an exception.
@@ -263,8 +264,8 @@ otherwise.
263264
To purposefully and silently skip postcommand hooks, commands can raise any of the following
264265
exceptions.
265266

266-
- `cmd2.exceptions.SkipPostcommandHooks`
267-
- `cmd2.exceptions.Cmd2ArgparseError`
267+
- [cmd2.exceptions.SkipPostcommandHooks][]
268+
- [cmd2.exceptions.Cmd2ArgparseError][]
268269

269270
## Command Finalization Hooks
270271

@@ -282,7 +283,7 @@ class App(cmd2.Cmd):
282283
```
283284

284285
Command Finalization hooks must check whether the `cmd2.plugin.CommandFinalizationData.statement`
285-
attribute of the passed `cmd2.plugin.CommandFinalizationData` object contains a value. There are
286+
attribute of the passed [cmd2.plugin.CommandFinalizationData][] object contains a value. There are
286287
certain circumstances where these hooks may be called before the user input has been parsed, so you
287288
can't always rely on having a `cmd2.plugin.CommandFinalizationData.statement`.
288289

docs/features/initialization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Here is a basic example `cmd2` application which demonstrates many capabilities
1212

1313
## Cmd class initializer
1414

15-
A `cmd2.Cmd` instance or subclass instance is an interactive CLI application framework. There is no good reason to instantiate `Cmd` itself; rather, it's useful as a superclass of a class you define yourself in order to inherit `Cmd`'s methods and encapsulate action methods.
15+
A [cmd2.Cmd][] instance or subclass instance is an interactive CLI application framework. There is no good reason to instantiate `Cmd` itself; rather, it's useful as a superclass of a class you define yourself in order to inherit `Cmd`'s methods and encapsulate action methods.
1616

1717
Certain things must be initialized within the `__init__()` method of your class derived from `cmd2.Cmd`(all arguments to `__init__()` are optional):
1818

docs/features/misc.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ available when the application is in a specific state. When a command is disable
4141
up in the help menu or tab complete. If a user tries to run the command, a command-specific message
4242
supplied by the developer will be printed. The following functions support this feature.
4343

44-
- **enable_command** : Enable an individual command
45-
- **enable_category** : Enable an entire category of commands
46-
- **disable_command** : Disable an individual command and set the message that will print when this
47-
command is run or help is called on it while disabled
48-
- **disable_category** : Disable an entire category of commands and set the message that will print
49-
when anything in this category is run or help is called on it while disabled
44+
- [enable_command][cmd2.Cmd.enable_command] : Enable an individual command
45+
- [enable_category][cmd2.Cmd.enable_category] : Enable an entire category of commands
46+
- [disable_command][cmd2.Cmd.disable_command] : Disable an individual command and set the message
47+
that will print when this command is run or help is called on it while disabled
48+
- [disable_category][cmd2.Cmd.disable_category] : Disable an entire category of commands and set the
49+
message that will print when anything in this category is run or help is called on it while
50+
disabled
5051

5152
See the definitions of these functions for descriptions of their arguments.
5253

5354
See the `do_enable_commands()` and `do_disable_commands()` functions in the
54-
[HelpCategories](https://github.com/python-cmd2/cmd2/blob/main/examples/help_categories.py) example
55-
for a demonstration.
55+
[help_categories.py](https://github.com/python-cmd2/cmd2/blob/main/examples/help_categories.py)
56+
example for a demonstration.
5657

5758
## Default to shell
5859

@@ -64,9 +65,9 @@ shortcut:
6465
(Cmd) !which python
6566
/usr/bin/python
6667

67-
However, if the parameter `default_to_shell` is `True`, then _every_ command will be attempted on
68-
the operating system. Only if that attempt fails (i.e., produces a nonzero return value) will the
69-
application's own `default` method be called.
68+
However, if the parameter `default_to_shell` is `True`, then _every_ thing entered which doesn't
69+
match another command will be attempted on the operating system. Only if that attempt fails (i.e.,
70+
produces a nonzero return value) will the application's own `default` method be called.
7071

7172
(Cmd) which python
7273
/usr/bin/python

0 commit comments

Comments
 (0)