@@ -7,24 +7,28 @@ https://user-images.githubusercontent.com/93259987/224482407-ea1de764-09f7-415e-
7
7
] ( https://github.com/hamdanal/rich-argparse/actions/workflows/tests.yml )
8
8
[ ![ pre-commit.ci status] ( https://results.pre-commit.ci/badge/github/hamdanal/rich-argparse/main.svg )
9
9
] ( 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 )
10
11
[ ![ 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 )
13
13
] ( https://pypi.org/project/rich-argparse/ )
14
14
15
15
Format argparse and optparse help using [ rich] ( https://pypi.org/project/rich ) .
16
16
17
+ * rich-argparse* improves the look and readability of argparse's help while requiring minimal
18
+ changes to the code.
19
+
17
20
## Table of contents
18
21
19
22
* [ Installation] ( #installation )
20
23
* [ Usage] ( #usage )
21
24
* [ Output styles] ( #output-styles )
22
25
* [ 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 )
26
30
* [ Subparsers] ( #working-with-subparsers )
27
- * [ Third party formatters] ( #working-with-third-party-formatters )
31
+ * [ Third party formatters] ( #working-with-third-party-formatters ) (ft. django)
28
32
* [ Optparse] ( #optparse-support ) (experimental)
29
33
30
34
## Installation
@@ -46,31 +50,26 @@ parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
46
50
...
47
51
```
48
52
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 ) :
51
55
52
- | ` rich_argparse ` formatter | ` argparse ` equivalent |
53
- | ---------------------------| -----------------------|
56
+ | ` rich_argparse ` formatter | equivalent in ` argparse ` |
57
+ | ---------------------------| -------------------------- |
54
58
| ` RichHelpFormatter ` | ` HelpFormatter ` |
55
59
| ` RawDescriptionRichHelpFormatter ` | ` RawDescriptionHelpFormatter ` |
56
60
| ` RawTextRichHelpFormatter ` | ` RawTextHelpFormatter ` |
57
61
| ` ArgumentDefaultsRichHelpFormatter ` | ` ArgumentDefaultsHelpFormatter ` |
58
62
| ` MetavarTypeRichHelpFormatter ` | ` MetavarTypeHelpFormatter ` |
59
63
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
-
63
64
## Output styles
64
65
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.
70
68
71
69
### Customize the colors
70
+
72
71
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:
74
73
75
74
``` python
76
75
{
@@ -80,7 +79,7 @@ class. By default, `RichHelpFormatter` defines the following styles:
80
79
' argparse.metavar' : ' dark_cyan' , # for metavariables (e.g. "FILE" in "--file FILE")
81
80
' argparse.prog' : ' grey50' , # for %(prog)s in the usage (e.g. "foo" in "Usage: foo [options]")
82
81
' 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")
84
83
}
85
84
```
86
85
@@ -90,27 +89,27 @@ For example, to make the description and epilog *italic*, change the `argparse.t
90
89
RichHelpFormatter.styles[" argparse.text" ] = " italic"
91
90
```
92
91
93
- ### Customize group name formatting
92
+ ### Customize the group name format
93
+
94
94
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:
98
97
99
98
``` python
100
- RichHelpFormatter.group_name_formatter = str .upper
99
+ RichHelpFormatter.group_name_formatter = str .upper # Make group names UPPERCASE
101
100
```
102
101
103
102
### Special text highlighting
104
103
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
108
107
and patterns of `` `back tick quoted text` `` using the ` argparse.syntax ` style. You can control
109
108
what patterns are highlighted by modifying the ` RichHelpFormatter.highlights ` list. To disable all
110
109
highlights, you can clear this list using ` RichHelpFormatter.highlights.clear() ` .
111
110
112
111
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:
114
113
115
114
``` python
116
115
# Add a style called `pyproject` which applies a green style (any rich style works)
@@ -124,45 +123,43 @@ parser = argparse.ArgumentParser(..., formatter_class=RichHelpFormatter)
124
123
125
124
### Colors in the ` usage `
126
125
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
131
130
` RichHelpFormatter.usage_markup = True ` . If you enable this option, make sure to [ escape] (
132
131
https://rich.readthedocs.io/en/stable/markup.html#escaping ) any square brackets in the usage text.
133
132
133
+ ### Colors in ` --version `
134
134
135
- ## Working with subparsers
135
+ If you use the ` "version" ` action from argparse, you can use console markup in the ` version ` string:
136
136
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
+ ```
140
142
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.
144
145
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
148
147
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:
158
150
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
+ ```
159
156
160
157
## Working with third party formatters
161
158
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
164
161
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
166
163
` manage.py ` file as follows:
167
164
168
165
``` diff
@@ -211,33 +208,34 @@ index 7fb6855..5e5d48a 100755
211
208
main()
212
209
```
213
210
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
215
212
ordering of the arguments applied by django is respected by the new help formatter.
216
213
217
214
## 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 ` :
220
220
221
221
``` python
222
222
import optparse
223
- from rich_argparse.optparse import IndentedRichHelpFormatter
223
+ from rich_argparse.optparse import IndentedRichHelpFormatter # or TitledRichHelpFormatter
224
224
225
225
parser = optparse.OptionParser(formatter = IndentedRichHelpFormatter())
226
226
...
227
227
```
228
228
229
229
Similar to ` argparse ` , you can customize the styles used by the formatter by modifying the
230
230
` 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 :
232
232
233
233
``` python
234
- RichHelpFormatter.styles[" optparse.metavar" ] = " italic "
234
+ RichHelpFormatter.styles[" optparse.metavar" ] = " bold magenta "
235
235
```
236
236
237
- Syntax highlighting works the same way as ` argparse ` .
237
+ Syntax highlighting works the same as ` argparse ` .
238
238
239
239
Colors in the ` usage ` are not supported yet.
240
240
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