Skip to content

Commit 90f72d3

Browse files
committed
Added info on description and epliog fields within argument parsers being rich objects and info on HelpFormatter classes
1 parent 3916696 commit 90f72d3

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

docs/features/argument_processing.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ By default, `cmd2` uses the docstring of the command method when a user asks for
8080
command. When you use the `@with_argparser` decorator, the docstring for the `do_*` method is used
8181
to set the description for the `argparse.ArgumentParser`.
8282

83+
!!! tip "description and epilog fields are rich objects"
84+
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.
88+
8389
With this code:
8490

8591
```py
@@ -174,7 +180,32 @@ This command cannot generate tags with no content, like <br/>
174180

175181
!!! warning
176182

177-
If a command **foo** is decorated with `cmd2`'s `with_argparse` decorator, then **help_foo** will not be invoked when `help foo` is called. The [argparse](https://docs.python.org/3/library/argparse.html) module provides a rich API which can be used to tweak every aspect of the displayed help and we encourage `cmd2` developers to utilize that.
183+
If a command **foo** is decorated with `cmd2`'s `with_argparse` decorator, then **help_foo** will not be
184+
invoked when `help foo` is called. The [argparse](https://docs.python.org/3/library/argparse.html) module
185+
provides a rich API which can be used to tweak every aspect of the displayed help and we encourage `cmd2`
186+
developers to utilize that.
187+
188+
### Argparse HelpFormatter classes
189+
190+
`cmd2` has 5 different Argparse HelpFormatter classes, all of which are based on the
191+
`RichHelpFormatter` class from [rich-argparse](https://github.com/hamdanal/rich-argparse). The
192+
benefit is that your `cmd2` applications now have more aesthetically pleasing help which includes
193+
color to make it quicker and easier to visually parse help text. This works for all supported
194+
versions of Python.
195+
196+
- [Cmd2HelpFormatter][cmd2.argparse_custom.Cmd2HelpFormatter] - default help formatter class
197+
- [ArgumentDefaultsCmd2HelpFormatter][cmd2.argparse_custom.ArgumentDefaultsCmd2HelpFormatter] - adds
198+
default values to argument help
199+
- [MetavarTypeCmd2HelpFormatter][cmd2.argparse_custom.MetavarTypeCmd2HelpFormatter] - uses the
200+
argument 'type' as the default metavar value (instead of the argument 'dest')
201+
- [RawDescriptionCmd2HelpFormatter][cmd2.argparse_custom.RawDescriptionCmd2HelpFormatter] - retains
202+
any formatting in descriptions and epilogs
203+
- [RawTextCmd2HelpFormatter][cmd2.argparse_custom.RawTextCmd2HelpFormatter] - retains formatting of
204+
all help text
205+
206+
The default `Cmd2HelpFormatter` class inherits from `argparse.HelpFormatter`. If you want a
207+
different behavior, then pass the desired class to the `formatter_class` argument of your argparse
208+
parser, e.g. `formatter_class=ArgumentDefaultsCmd2HelpFormatter` to your parser.
178209

179210
## Argument List
180211

0 commit comments

Comments
 (0)