Skip to content

Commit b000bde

Browse files
authored
Improve the README (#83)
Closes #77
1 parent 7c65c02 commit b000bde

File tree

2 files changed

+66
-68
lines changed

2 files changed

+66
-68
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ repos:
1717
- id: mixed-line-ending
1818
- id: trailing-whitespace
1919
- repo: https://github.com/astral-sh/ruff-pre-commit
20-
rev: v0.0.276
20+
rev: v0.0.282
2121
hooks:
2222
- id: ruff
2323
args: [--fix, --exit-non-zero-on-fix]
24-
- repo: https://github.com/hauntsaninja/black-pre-commit-mirror
25-
rev: 23.3.0
24+
- repo: https://github.com/psf/black-pre-commit-mirror
25+
rev: 23.7.0
2626
hooks:
2727
- id: black
2828
- repo: https://github.com/pre-commit/mirrors-mypy

README.md

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,28 @@ https://user-images.githubusercontent.com/93259987/224482407-ea1de764-09f7-415e-
77
](https://github.com/hamdanal/rich-argparse/actions/workflows/tests.yml)
88
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/hamdanal/rich-argparse/main.svg)
99
](https://results.pre-commit.ci/latest/github/hamdanal/rich-argparse/main)
10+
[![Downloads](https://img.shields.io/pypi/dm/rich-argparse)](https://pypistats.org/packages/rich-argparse)
1011
[![Python Version](https://img.shields.io/pypi/pyversions/rich-argparse)
11-
![Release](https://img.shields.io/github/v/release/hamdanal/rich-argparse?sort=semver)
12-
![Downloads](https://pepy.tech/badge/rich-argparse/month)
12+
![Release](https://img.shields.io/pypi/v/rich-argparse)
1313
](https://pypi.org/project/rich-argparse/)
1414

1515
Format argparse and optparse help using [rich](https://pypi.org/project/rich).
1616

17+
*rich-argparse* improves the look and readability of argparse's help while requiring minimal
18+
changes to the code.
19+
1720
## Table of contents
1821

1922
* [Installation](#installation)
2023
* [Usage](#usage)
2124
* [Output styles](#output-styles)
2225
* [Colors](#customize-the-colors)
23-
* [Group names](#customize-group-name-formatting)
24-
* [Syntax highlighting](#special-text-highlighting)
25-
* [Usage colors](#colors-in-the-usage)
26+
* [Group names](#customize-the-group-name-format)
27+
* [Highlighting patterns](#special-text-highlighting)
28+
* ["usage"](#colors-in-the-usage)
29+
* [--version](#colors-in---version)
2630
* [Subparsers](#working-with-subparsers)
27-
* [Third party formatters](#working-with-third-party-formatters)
31+
* [Third party formatters](#working-with-third-party-formatters) (ft. django)
2832
* [Optparse](#optparse-support) (experimental)
2933

3034
## Installation
@@ -46,31 +50,26 @@ parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
4650
...
4751
```
4852

49-
rich-argparse defines help formatter classes that produce colorful and easy to read help text. The
50-
formatter classes are equivalent to argparse's built-in formatters:
53+
*rich-argparse* defines equivalents to argparse's [built-in formatters](
54+
https://docs.python.org/3/library/argparse.html#formatter-class):
5155

52-
| `rich_argparse` formatter | `argparse` equivalent |
53-
|---------------------------|-----------------------|
56+
| `rich_argparse` formatter | equivalent in `argparse` |
57+
|---------------------------|--------------------------|
5458
| `RichHelpFormatter` | `HelpFormatter` |
5559
| `RawDescriptionRichHelpFormatter` | `RawDescriptionHelpFormatter` |
5660
| `RawTextRichHelpFormatter` | `RawTextHelpFormatter` |
5761
| `ArgumentDefaultsRichHelpFormatter` | `ArgumentDefaultsHelpFormatter` |
5862
| `MetavarTypeRichHelpFormatter` | `MetavarTypeHelpFormatter` |
5963

60-
For more information on how these formatters work, check the [argparse documentation](
61-
https://docs.python.org/3/library/argparse.html#formatter-class).
62-
6364
## Output styles
6465

65-
The default styles used by rich-argparse formatters are carefully chosen to work in different light
66-
and dark themes. If these styles don't suit your taste, read below to learn how to change them.
67-
68-
> **Note**
69-
> The examples below only mention `RichHelpFormatter` but apply to all other formatter classes.
66+
The default styles used by *rich-argparse* are carefully chosen to work in different light and dark
67+
themes.
7068

7169
### Customize the colors
70+
7271
You can customize the colors in the output by modifying the `styles` dictionary on the formatter
73-
class. By default, `RichHelpFormatter` defines the following styles:
72+
class. *rich-argparse* defines the following styles:
7473

7574
```python
7675
{
@@ -80,7 +79,7 @@ class. By default, `RichHelpFormatter` defines the following styles:
8079
'argparse.metavar': 'dark_cyan', # for metavariables (e.g. "FILE" in "--file FILE")
8180
'argparse.prog': 'grey50', # for %(prog)s in the usage (e.g. "foo" in "Usage: foo [options]")
8281
'argparse.syntax': 'bold', # for highlights of back-tick quoted text (e.g. "`some text`")
83-
'argparse.text': 'default', # for the descriptions and epilog (e.g. "A program to foo")
82+
'argparse.text': 'default', # for descriptions, epilog, and --version (e.g. "A program to foo")
8483
}
8584
```
8685

@@ -90,27 +89,27 @@ For example, to make the description and epilog *italic*, change the `argparse.t
9089
RichHelpFormatter.styles["argparse.text"] = "italic"
9190
```
9291

93-
### Customize group name formatting
92+
### Customize the group name format
93+
9494
You can change how the names of the groups (like `'positional arguments'` and `'options'`) are
95-
formatted by setting the `RichHelpFormatter.group_name_formatter` function. By default,
96-
`RichHelpFormatter` sets the function to `str.title` but any function that takes the group name
97-
as an input and returns a str works. For example, to apply the *UPPER CASE* format do this:
95+
formatted by setting the `RichHelpFormatter.group_name_formatter` which is set to `str.title` by
96+
default. Any callable that takes the group name as an input and returns a str works:
9897

9998
```python
100-
RichHelpFormatter.group_name_formatter = str.upper
99+
RichHelpFormatter.group_name_formatter = str.upper # Make group names UPPERCASE
101100
```
102101

103102
### Special text highlighting
104103

105-
You can [highlight patterns](https://rich.readthedocs.io/en/stable/highlighting.html) in the help
106-
text and the description text of your parser's help output using regular expressions. By default,
107-
`RichHelpFormatter` highlights patterns of `--options-with-hyphens` using the `argparse.args` style
104+
You can [highlight patterns](https://rich.readthedocs.io/en/stable/highlighting.html) in the
105+
arguments help and the description and epilog using regular expressions. By default,
106+
*rich-argparse* highlights patterns of `--options-with-hyphens` using the `argparse.args` style
108107
and patterns of `` `back tick quoted text` `` using the `argparse.syntax` style. You can control
109108
what patterns are highlighted by modifying the `RichHelpFormatter.highlights` list. To disable all
110109
highlights, you can clear this list using `RichHelpFormatter.highlights.clear()`.
111110

112111
You can also add custom highlight patterns and styles. The following example highlights all
113-
occurrences of `pyproject.toml` in green.
112+
occurrences of `pyproject.toml` in green:
114113

115114
```python
116115
# Add a style called `pyproject` which applies a green style (any rich style works)
@@ -124,45 +123,43 @@ parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
124123

125124
### Colors in the `usage`
126125

127-
`RichHelpFormatter` colors the usage generated by the formatter using the same styles used to color
128-
the arguments and their metavars. If you use a custom `usage` message in the parser, this text will
129-
treated as "plain text" and will not be colored by default. You can enable colors in user defined
130-
usage message with [console markup](https://rich.readthedocs.io/en/stable/markup.html) by setting
126+
The usage **generated by the formatter** is colored using the `argparse.args` and `argparse.metavar`
127+
styles. If you use a custom `usage` message in the parser, it will treated as "plain text" and will
128+
**not** be colored by default. You can enable colors in user defined usage message through
129+
[console markup](https://rich.readthedocs.io/en/stable/markup.html) by setting
131130
`RichHelpFormatter.usage_markup = True`. If you enable this option, make sure to [escape](
132131
https://rich.readthedocs.io/en/stable/markup.html#escaping) any square brackets in the usage text.
133132

133+
### Colors in `--version`
134134

135-
## Working with subparsers
135+
If you use the `"version"` action from argparse, you can use console markup in the `version` string:
136136

137-
If your code uses argparse's subparsers and you want to format the subparsers' help output with
138-
rich-argparse, you have to explicitly pass `formatter_class` to the subparsers since subparsers
139-
do not inherit the formatter class from the parent parser by default. You have two options:
137+
```python
138+
parser.add_argument(
139+
"--version", action="version", version="[argparse.prog]%(prog)s[/] version [i]1.0.0[/]"
140+
)
141+
```
140142

141-
1. Create a helper function to set `formatter_class` automatically:
142-
```python
143-
subparsers = parser.add_subparsers(...)
143+
Note that the `argparse.text` style is applied to the `version` string similar to the description
144+
and epilog.
144145

145-
def add_parser(*args, **kwds):
146-
kwds.setdefault("formatter_class", parser.formatter_class)
147-
return subparsers.add_parser(*args, **kwds)
146+
## Working with subparsers
148147

149-
p1 = add_parser(...)
150-
p2 = add_parser(...)
151-
```
152-
1. Set `formatter_class` on each subparser individually:
153-
```python
154-
subparsers = parser.add_subparsers(...)
155-
p1 = subparsers.add_parser(..., formatter_class=parser.formatter_class)
156-
p2 = subparsers.add_parser(..., formatter_class=parser.formatter_class)
157-
```
148+
Subparsers do not inherit the formatter class from the parent parser by default. You have to pass
149+
the formatter class explicitly:
158150

151+
```python
152+
subparsers = parser.add_subparsers(...)
153+
p1 = subparsers.add_parser(..., formatter_class=parser.formatter_class)
154+
p2 = subparsers.add_parser(..., formatter_class=parser.formatter_class)
155+
```
159156

160157
## Working with third party formatters
161158

162-
`RichHelpFormatter` can be used with third party formatters that do not rely on the **private**
163-
internals of `argparse.HelpFormatter`. For example, [django](https://pypi.org/project/django)
159+
*rich-argparse* can be used with other formatters that **do not rely on the private internals**
160+
of `argparse.HelpFormatter`. A popular example is [django](https://pypi.org/project/django) that
164161
defines a custom help formatter that is used with its built in commands as well as with extension
165-
libraries and user defined commands. To use rich-argparse in your django project, change your
162+
libraries and user defined commands. To use *rich-argparse* in your django project, change your
166163
`manage.py` file as follows:
167164

168165
```diff
@@ -211,33 +208,34 @@ index 7fb6855..5e5d48a 100755
211208
main()
212209
```
213210

214-
Now try out some command like: `python manage.py runserver --help`. Notice how the special
211+
Now try out some command like `python manage.py runserver --help` and notice how the special
215212
ordering of the arguments applied by django is respected by the new help formatter.
216213

217214
## Optparse support
218-
rich-argparse now ships with experimental support for [optparse]. Import optparse help formatters
219-
from `rich_argparse.optparse`:
215+
216+
*rich-argparse* now ships with experimental support for [optparse](
217+
https://docs.python.org/3/library/optparse.html).
218+
219+
Import optparse help formatters from `rich_argparse.optparse`:
220220

221221
```python
222222
import optparse
223-
from rich_argparse.optparse import IndentedRichHelpFormatter
223+
from rich_argparse.optparse import IndentedRichHelpFormatter # or TitledRichHelpFormatter
224224

225225
parser = optparse.OptionParser(formatter=IndentedRichHelpFormatter())
226226
...
227227
```
228228

229229
Similar to `argparse`, you can customize the styles used by the formatter by modifying the
230230
`RichHelpFormatter.styles` dictionary. These are the same styles used by `argparse` but with
231-
the `optparse.` prefix. For example, to change the style used for the metavar of an option:
231+
the `optparse.` prefix instead:
232232

233233
```python
234-
RichHelpFormatter.styles["optparse.metavar"] = "italic"
234+
RichHelpFormatter.styles["optparse.metavar"] = "bold magenta"
235235
```
236236

237-
Syntax highlighting works the same way as `argparse`.
237+
Syntax highlighting works the same as `argparse`.
238238

239239
Colors in the `usage` are not supported yet.
240240

241-
Customizing the group name format is not supported. optparse uses title case by default.
242-
243-
[optparse]: https://docs.python.org/3/library/optparse.html
241+
Customizing the group name format is not supported. optparse uses Title Case format by default.

0 commit comments

Comments
 (0)