1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
# rich-argparse

[
](https://github.com/hamdanal/rich-argparse/actions/workflows/tests.yml)
[
](https://results.pre-commit.ci/latest/github/hamdanal/rich-argparse/main)
[](https://pypistats.org/packages/rich-argparse)
[

](https://pypi.org/project/rich-argparse/)
Format argparse and optparse help using [rich](https://pypi.org/project/rich).
*rich-argparse* improves the look and readability of argparse's help while requiring minimal
changes to the code.
## Table of contents
* [Installation](#installation)
* [Usage](#usage)
* [Output styles](#output-styles)
* [Colors](#customize-the-colors)
* [Group names](#customize-the-group-name-format)
* [Highlighting patterns](#special-text-highlighting)
* ["usage"](#colors-in-the-usage)
* [Console markup](#disable-console-markup)
* [--version](#colors-in---version)
* [Rich renderables](#rich-descriptions-and-epilog)
* [Subparsers](#working-with-subparsers)
* [Documenting your CLI](#generate-help-preview)
* [Third party formatters](#working-with-third-party-formatters) (ft. django)
* [Optparse](#optparse-support) (experimental)
* [Legacy Windows](#legacy-windows-support)
## Installation
Install from PyPI with pip or your favorite tool.
```sh
pip install rich-argparse
```
## Usage
Simply pass `formatter_class` to the argument parser
```python
import argparse
from rich_argparse import RichHelpFormatter
parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
...
```
*rich-argparse* defines equivalents to all argparse's [built-in formatters](
https://docs.python.org/3/library/argparse.html#formatter-class):
| `rich_argparse` formatter | equivalent in `argparse` |
|---------------------------|--------------------------|
| `RichHelpFormatter` | `HelpFormatter` |
| `RawDescriptionRichHelpFormatter` | `RawDescriptionHelpFormatter` |
| `RawTextRichHelpFormatter` | `RawTextHelpFormatter` |
| `ArgumentDefaultsRichHelpFormatter` | `ArgumentDefaultsHelpFormatter` |
| `MetavarTypeRichHelpFormatter` | `MetavarTypeHelpFormatter` |
## Output styles
The default styles used by *rich-argparse* are carefully chosen to work in different light and dark
themes.
### Customize the colors
You can customize the colors of the output by modifying the `styles` dictionary on the formatter
class. You can use any rich style as defined [here](https://rich.readthedocs.io/en/latest/style.html).
*rich-argparse* defines and uses the following styles:
```python
{
'argparse.args': 'cyan', # for positional-arguments and --options (e.g "--help")
'argparse.groups': 'dark_orange', # for group names (e.g. "positional arguments")
'argparse.help': 'default', # for argument's help text (e.g. "show this help message and exit")
'argparse.metavar': 'dark_cyan', # for metavariables (e.g. "FILE" in "--file FILE")
'argparse.prog': 'grey50', # for %(prog)s in the usage (e.g. "foo" in "Usage: foo [options]")
'argparse.syntax': 'bold', # for highlights of back-tick quoted text (e.g. "`some text`")
'argparse.text': 'default', # for descriptions, epilog, and --version (e.g. "A program to foo")
'argparse.default': 'italic', # for %(default)s in the help (e.g. "Value" in "(default: Value)")
}
```
For example, to make the description and epilog *italic*, change the `argparse.text` style:
```python
RichHelpFormatter.styles["argparse.text"] = "italic"
```
### Customize the group name format
You can change how the names of the groups (like `'positional arguments'` and `'options'`) are
formatted by setting the `RichHelpFormatter.group_name_formatter` which is set to `str.title` by
default. Any callable that takes the group name as an input and returns a str works:
```python
RichHelpFormatter.group_name_formatter = str.upper # Make group names UPPERCASE
```
### Special text highlighting
You can [highlight patterns](https://rich.readthedocs.io/en/stable/highlighting.html) in the
arguments help and the description and epilog using regular expressions. By default,
*rich-argparse* highlights patterns of `--options-with-hyphens` using the `argparse.args` style
and patterns of `` `back tick quoted text` `` using the `argparse.syntax` style. You can control
what patterns are highlighted by modifying the `RichHelpFormatter.highlights` list. To disable all
highlights, you can clear this list using `RichHelpFormatter.highlights.clear()`.
You can also add custom highlight patterns and styles. The following example highlights all
occurrences of `pyproject.toml` in green:
```python
# Add a style called `pyproject` which applies a green style (any rich style works)
RichHelpFormatter.styles["argparse.pyproject"] = "green"
# Add the highlight regex (the regex group name must match an existing style name)
RichHelpFormatter.highlights.append(r"\b(?P<pyproject>pyproject\.toml)\b")
# Pass the formatter class to argparse
parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
...
```
### Colors in the `usage`
The usage **generated by the formatter** is colored using the `argparse.args` and `argparse.metavar`
styles. If you use a custom `usage` message in the parser, it will be treated as "plain text" and
will **not** be colored by default. You can enable colors in user defined usage message through
[console markup](https://rich.readthedocs.io/en/stable/markup.html) by setting
`RichHelpFormatter.usage_markup = True`. If you enable this option, make sure to [escape](
https://rich.readthedocs.io/en/stable/markup.html#escaping) any square brackets in the usage text.
### Disable console markup
The text of the descriptions and epilog is interpreted as
[console markup](https://rich.readthedocs.io/en/stable/markup.html) by default. If this conflicts
with your usage of square brackets, make sure to [escape](
https://rich.readthedocs.io/en/stable/markup.html#escaping) the square brackets or to disable
markup globally with `RichHelpFormatter.text_markup = False`.
Similarly the help text of arguments is interpreted as markup by default. It can be disabled using
`RichHelpFormatter.help_markup = False`.
### Colors in `--version`
If you use the `"version"` action from argparse, you can use console markup in the `version` string:
```python
parser.add_argument(
"--version", action="version", version="[argparse.prog]%(prog)s[/] version [i]1.0.0[/]"
)
```
Note that the `argparse.text` style is applied to the `version` string similar to the description
and epilog.
### Rich descriptions and epilog
You can use any rich renderable in the descriptions and epilog. This includes all built-in rich
renderables like `Table` and `Markdown` and any custom renderables defined using the
[Console Protocol](https://rich.readthedocs.io/en/stable/protocol.html#console-protocol).
```python
import argparse
from rich.markdown import Markdown
from rich_argparse import RichHelpFormatter
description = """
# My program
This is a markdown description of my program.
* It has a list
* And a table
| Column 1 | Column 2 |
| -------- | -------- |
| Value 1 | Value 2 |
"""
parser = argparse.ArgumentParser(
description=Markdown(description, style="argparse.text"),
formatter_class=RichHelpFormatter,
)
...
```
Certain features are **disabled** for arbitrary renderables other than strings, including:
* Syntax highlighting with `RichHelpFormatter.highlights`
* Styling with the `"argparse.text"` style defined in `RichHelpFormatter.styles`
* Replacement of `%(prog)s` with the program name
## Working with subparsers
Subparsers do not inherit the formatter class from the parent parser by default. You have to pass
the formatter class explicitly:
```python
subparsers = parser.add_subparsers(...)
p1 = subparsers.add_parser(..., formatter_class=parser.formatter_class)
p2 = subparsers.add_parser(..., formatter_class=parser.formatter_class)
```
## Generate help preview
You can generate a preview of the help message for your CLI in SVG, HTML, or TXT formats using the
`HelpPreviewAction` action. This is useful for including the help message in the documentation of
your app. The action uses the
[rich exporting API](https://rich.readthedocs.io/en/stable/console.html#exporting) internally.
```python
import argparse
from rich.terminal_theme import DIMMED_MONOKAI
from rich_argparse import HelpPreviewAction, RichHelpFormatter
parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
...
parser.add_argument(
"--generate-help-preview",
action=HelpPreviewAction,
path="help-preview.svg", # (optional) or "help-preview.html" or "help-preview.txt"
export_kwds={"theme": DIMMED_MONOKAI}, # (optional) keywords passed to console.save_... methods
)
```
This action is hidden, it won't show up in the help message or in the parsed arguments namespace.
Use it like this:
```sh
python my_cli.py --generate-help-preview # generates help-preview.svg (default path specified above)
# or
python my_cli.py --generate-help-preview my-help.svg # generates my-help.svg
# or
COLUMNS=120 python my_cli.py --generate-help-preview # force the width of the output to 120 columns
```
## Working with third party formatters
*rich-argparse* can be used with other custom formatters through multiple inheritance. For example,
[django](https://pypi.org/project/django) defines a custom help formatter for its built in commands
as well as extension libraries and user defined commands. To use *rich-argparse* in your django
project, change your `manage.py` file as follows:
```diff
diff --git a/my_project/manage.py b/my_project/manage.py
index 7fb6855..5e5d48a 100755
--- a/my_project/manage.py
+++ b/my_project/manage.py
@@ -1,22 +1,38 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
+
+ from django.core.management.base import BaseCommand, DjangoHelpFormatter
+ from rich_argparse import RichHelpFormatter
+
+ class DjangoRichHelpFormatter(DjangoHelpFormatter, RichHelpFormatter): # django first
+ """A rich-based help formatter for django commands."""
+
+ original_create_parser = BaseCommand.create_parser
+
+ def create_parser(*args, **kwargs):
+ parser = original_create_parser(*args, **kwargs)
+ parser.formatter_class = DjangoRichHelpFormatter # set the formatter_class
+ return parser
+
+ BaseCommand.create_parser = create_parser
+
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
```
Now the output of all `python manage.py <COMMAND> --help` will be colored.
## Optparse support
*rich-argparse* now ships with experimental support for [optparse](
https://docs.python.org/3/library/optparse.html).
Import optparse help formatters from `rich_argparse.optparse`:
```python
import optparse
from rich_argparse.optparse import IndentedRichHelpFormatter # or TitledRichHelpFormatter
parser = optparse.OptionParser(formatter=IndentedRichHelpFormatter())
...
```
You can also generate a more helpful usage message by passing `usage=GENERATE_USAGE` to the
parser. This is similar to the default behavior of `argparse`.
```python
from rich_argparse.optparse import GENERATE_USAGE, IndentedRichHelpFormatter
parser = optparse.OptionParser(usage=GENERATE_USAGE, formatter=IndentedRichHelpFormatter())
```
Similar to `argparse`, you can customize the styles used by the formatter by modifying the
`RichHelpFormatter.styles` dictionary. These are the same styles used by `argparse` but with
the `optparse.` prefix instead:
```python
RichHelpFormatter.styles["optparse.metavar"] = "bold magenta"
```
Syntax highlighting works the same as with `argparse`.
Colors in the `usage` are only supported when using `GENERATE_USAGE`.
## Legacy Windows support
When used on legacy Windows versions like *Windows 7*, colors are disabled unless
[colorama](https://pypi.org/project/colorama/) is used:
```python
import argparse
import colorama
from rich_argparse import RichHelpFormatter
colorama.init()
parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
...
```
|