Skip to content

Commit 1cb3550

Browse files
committed
Addressed PR comments
1 parent 17a6332 commit 1cb3550

File tree

8 files changed

+68
-20
lines changed

8 files changed

+68
-20
lines changed

docs/features/argument_processing.md

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,32 @@ All of these decorators accept an optional **preserve_quotes** argument which de
3434
Setting this argument to `True` is useful for cases where you are passing the arguments to another
3535
command which might have its own argument parsing.
3636

37+
## with_argparser decorator
38+
39+
The [@with_argparser][cmd2.with_argparser] decorator can accept the following for its first
40+
argument:
41+
42+
1. An existing instance of `argparse.ArgumentParser`
43+
2. A function or static method which returns an instance of `argparse.ArgumentParser`
44+
3. Cmd or CommandSet class method which returns an instance of `argparse.ArgumentParser`
45+
46+
In all cases the `@with_argparser` decorator creates a deep copy of the parser instance which it
47+
stores internally. A consequence is that parsers don't need to be unique across commands.
48+
49+
!!! warning
50+
51+
Since the `@with_argparser` decorator is making a deep-copy of the parser provided, if you wish
52+
to dynamically modify this parser at a later time, you need to retrieve this deep copy. This can
53+
be done using `self._command_parsers.get(self.do_commandname)`.
54+
3755
## Argument Parsing
3856

39-
For each command in the `cmd2.Cmd` subclass which requires argument parsing, create a unique
40-
instance of `argparse.ArgumentParser()` which can parse the input appropriately for the command.
41-
Then decorate the command method with the `@with_argparser` decorator, passing the argument parser
42-
as the first parameter to the decorator. This changes the second argument of the command method,
43-
which will contain the results of `ArgumentParser.parse_args()`.
57+
For each command in the `cmd2.Cmd` subclass which requires argument parsing, create an instance of
58+
`argparse.ArgumentParser()` which can parse the input appropriately for the command (or provide a
59+
function/method that returns such a parser). Then decorate the command method with the
60+
`@with_argparser` decorator, passing the argument parser as the first parameter to the decorator.
61+
This changes the second argument of the command method, which will contain the results of
62+
`ArgumentParser.parse_args()`.
4463

4564
Here's what it looks like:
4665

@@ -82,9 +101,9 @@ to set the description for the `argparse.ArgumentParser`.
82101

83102
!!! tip "description and epilog fields are rich objects"
84103

85-
While the `help` text itself is simply a string, both the `description` and `epilog` can contain
86-
[rich](https://github.com/Textualize/rich) objects. For the `description` and `epilog` fields, you can pass
87-
in any `rich` object, including Text, Tables, Markdown.
104+
While the `help` field is simply a string, both the `description` and `epilog` fields can accept any
105+
[rich](https://github.com/Textualize/rich) renderable. This allows you to include all of rich's
106+
built-in objects like `Text`, `Table`, and `Markdown`.
88107

89108
With this code:
90109

docs/features/disable_commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ See
1111
for and example of removing or hiding built-in commands.
1212

1313
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.
14+
example of dynamically enabling and disabling custom commands at runtime.
1515

1616
## Remove A Command
1717

docs/features/generating_output.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,31 @@ output you generate must be sent to `self.stdout`. You can use the methods descr
1919
everything will work fine. [cmd2.Cmd][] also includes a number of output related methods which you
2020
may use to enhance the output your application produces.
2121

22-
Since `cmd2` has a dependency on the [rich](https://github.com/Textualize/rich) library, all of
23-
`cmd2`'s output methods can natively render `rich`
22+
Since `cmd2` has a dependency on the [rich](https://github.com/Textualize/rich) library, the
23+
following [cmd2.Cmd][] output methods can natively render `rich`
2424
[renderable objects](https://rich.readthedocs.io/en/latest/protocol.html), enabling beautiful and
25-
complex output.
25+
complex output:
26+
27+
- [poutput][cmd2.Cmd.poutput]
28+
- [perror][cmd2.Cmd.perror]
29+
- [psuccess][cmd2.Cmd.psuccess]
30+
- [pwarning][cmd2.Cmd.pwarning]
31+
- [pfeedback][cmd2.Cmd.pfeedback]
32+
- [ppaged][cmd2.Cmd.ppaged]
33+
34+
!!! tip "Advanced output customization"
35+
36+
Each of the above methods accepts additional optional parameters that help control how the output is
37+
formatted:
38+
39+
- `sep`: string to write between printed text. Defaults to " "
40+
- `end`: string to write at end of printed text. Defaults to a newline
41+
- `style`: optional style to apply to output
42+
- `soft_wrap`: Enable soft wrap mode. If True, lines of text will not be word-wrapped or cropped to fit the terminal width. Defaults to True
43+
- `emoji`: If True, Rich will replace emoji codes (e.g., 😃) with their corresponding Unicode characters. Defaults to False
44+
- `markup`: If True, Rich will interpret strings with tags (e.g., [bold]hello[/bold]) as styled output. Defaults to False
45+
- `highlight`: If True, Rich will automatically apply highlighting to elements within strings, such as common Python data types like numbers, booleans, or None.
46+
- `rich_print_kwargs`: optional additional keyword arguments to pass to Rich's `Console.print()`
2647

2748
## Ordinary Output
2849

@@ -140,6 +161,11 @@ with display widths greater than 1. Additionally, ANSI style sequences are safel
140161
count toward the display width. This means colored text is supported. If text has line breaks, then
141162
each line is aligned independently.
142163

164+
!!! tip "Advanced alignment customization"
165+
166+
You can also control output alignment using the `rich_print_kwargs.justify` member when calling
167+
`cmd2`'s print methods.
168+
143169
## Columnar Output
144170

145171
When generating output in multiple columns, you often need to calculate the width of each item so

docs/features/scripting.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ script for more information.
112112
If your cmd2 application follows the
113113
[Unix design philosophy](https://en.wikipedia.org/wiki/Unix_philosophy) a scripter will have the
114114
most flexibility to create workflows using different commands. If the designer's application is more
115-
complete and less likely to be augmented in the future a scripter can use simple serial scripts with
116-
little control flow. In either case, choices made by the designer will have effects on scripters.
115+
complete and less likely to be augmented in the future, a scripter can use simple serial scripts
116+
with little control flow. In either case, choices made by the designer will have effects on
117+
scripters.
117118

118119
The following diagram illustrates the different boundaries to keep in mind.
119120

docs/features/shortcuts_aliases_macros.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class App(Cmd):
2929

3030
This warning applies in general to many other attributes which are not settable at runtime.
3131

32-
!!! tip
32+
!!! note
3333

3434
Command, alias, and macro names cannot start with a shortcut
3535

@@ -62,7 +62,9 @@ Use `alias delete` to remove aliases
6262

6363
For more details run: `help alias delete`
6464

65-
Note: Aliases cannot have the same name as a command or macro
65+
!!! note
66+
67+
Aliases cannot have the same name as a command or macro
6668

6769
## Macros
6870

@@ -99,6 +101,6 @@ For more details on listing macros run: `help macro list`
99101

100102
For more details on deleting macros run: `help macro delete`
101103

102-
!!! warning
104+
!!! note
103105

104106
Macros cannot have the same name as a command or alias

docs/features/theme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Theme
22

33
`cmd2` provides the ability to configure an overall theme for your application using the
4-
[cmd2.rich_utils.set_theme][] function. This is based o the
4+
[cmd2.rich_utils.set_theme][] function. This is based on the
55
[rich.theme](https://rich.readthedocs.io/en/stable/reference/theme.html) container for style
66
information. You can use this to brand your application and set an overall consistent look and feel
77
that is appealing to your user base.

docs/migrating/minimum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ application, you may be able to remove them. See [Exiting](../features/misc.md#e
4343
If you are distributing your application, you'll also need to ensure that `cmd2` is properly
4444
installed. You will need to add the following dependency to your `pyproject.toml` or `setup.py`:
4545

46-
'cmd2>=2.7'
46+
'cmd2>=3,<4'
4747

4848
See [Integrate cmd2 Into Your Project](../overview/integrating.md) for more details.

docs/overview/integrating.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Once installed, you will want to ensure that your project's dependencies include `cmd2`. Make sure
44
your `pyproject.toml` or `setup.py` includes the following dependency
55

6-
'cmd2>=2.7'
6+
'cmd2>=3,<4'
77

88
The `cmd2` project uses :simple-semver: [Semantic Versioning](https://semver.org), which means that
99
any incompatible API changes will be release with a new major version number. The public API is

0 commit comments

Comments
 (0)