Skip to content

Commit 8a47580

Browse files
authored
Rewrite help page section. (pallets#2821)
Rewrite docs for generating help pages. Make it more concise. Reorder sections.
2 parents 45f89cd + accbb4e commit 8a47580

File tree

2 files changed

+89
-125
lines changed

2 files changed

+89
-125
lines changed

docs/documentation.rst

+87-123
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
1-
Documenting Scripts
1+
Help Pages
22
===================
33

44
.. currentmodule:: click
55

6-
Click makes it very easy to document your command line tools. First of
7-
all, it automatically generates help pages for you. While these are
8-
currently not customizable in terms of their layout, all of the text
9-
can be changed.
6+
Click makes it very easy to document your command line tools. For most things Click automatically generates help pages for you. By design the text is customizable, but the layout is not.
107

118
Help Texts
12-
----------
9+
--------------
1310

14-
Commands and options accept help arguments. In the case of commands, the
15-
docstring of the function is automatically used if provided.
11+
Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.
1612

1713
Simple example:
1814

1915
.. click:example::
2016
2117
@click.command()
22-
@click.option('--count', default=1, help='number of greetings')
2318
@click.argument('name')
24-
def hello(count, name):
25-
"""This script prints hello NAME COUNT times."""
19+
@click.option('--count', default=1, help='number of greetings')
20+
def hello(name: str, count: int):
21+
"""This script prints hello and a name one or more times."""
2622
for x in range(count):
27-
click.echo(f"Hello {name}!")
28-
29-
And what it looks like:
23+
if name:
24+
click.echo(f"Hello {name}!")
25+
else:
26+
click.echo("Hello!")
3027

3128
.. click:run::
3229
@@ -35,15 +32,51 @@ And what it looks like:
3532

3633
.. _documenting-arguments:
3734

35+
Command Short Help
36+
------------------
37+
38+
For subcommands, a short help snippet is generated. By default, it's the first sentence of the docstring. If too long, then it will ellipsize what cannot be fit on a single line with ``...``. The short help snippet can also be overridden with ``short_help``:
39+
40+
.. click:example::
41+
42+
@click.group()
43+
def cli():
44+
"""A simple command line tool."""
45+
46+
@cli.command('init', short_help='init the repo')
47+
def init():
48+
"""Initializes the repository."""
49+
50+
51+
.. click:run::
52+
53+
invoke(cli, args=['--help'])
54+
55+
Command Epilog Help
56+
-------------------
57+
58+
The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.
59+
60+
.. click:example::
61+
62+
@click.command(
63+
epilog='See https://example.com for more details',
64+
)
65+
def init():
66+
"""Initializes the repository."""
67+
68+
69+
.. click:run::
70+
71+
invoke(init, args=['--help'])
72+
3873
Documenting Arguments
3974
----------------------
4075

41-
:func:`click.argument` does not take a ``help`` parameter. This is to
42-
follow the general convention of Unix tools of using arguments for only
43-
the most necessary things, and to document them in the command help text
44-
by referring to them by name.
76+
:class:`click.argument` does not take a ``help`` parameter. This follows the Unix Command Line Tools convention of using arguments only for necessary things and documenting them in the command help text
77+
by name. This should then be done via the docstring.
4578

46-
You might prefer to reference the argument in the description:
79+
A brief example:
4780

4881
.. click:example::
4982
@@ -53,13 +86,12 @@ You might prefer to reference the argument in the description:
5386
"""Print FILENAME."""
5487
click.echo(filename)
5588

56-
And what it looks like:
5789

5890
.. click:run::
5991
6092
invoke(touch, args=['--help'])
6193

62-
Or you might prefer to explicitly provide a description of the argument:
94+
Or more explicitly:
6395

6496
.. click:example::
6597
@@ -72,26 +104,36 @@ Or you might prefer to explicitly provide a description of the argument:
72104
"""
73105
click.echo(filename)
74106

75-
And what it looks like:
76-
77107
.. click:run::
78108
79109
invoke(touch, args=['--help'])
80110

81-
For more examples, see the examples in :doc:`/arguments`.
111+
Click's Wrapping Behavior
112+
----------------------------
113+
Click's default wrapping ignores single new lines and rewraps the text based on the width of the terminal, to a maximum of 80 characters. In the example notice how the second grouping of three lines is rewrapped into a single paragraph.
82114

115+
.. click:example::
83116
84-
Preventing Rewrapping
85-
---------------------
117+
@click.command()
118+
def cli():
119+
"""
120+
This is a very long paragraph and as you
121+
can see wrapped very early in the source text
122+
but will be rewrapped to the terminal width in
123+
the final output.
86124

87-
The default behavior of Click is to rewrap text based on the width of the
88-
terminal, to a maximum 80 characters. In some circumstances, this can become
89-
a problem. The main issue is when showing code examples, where newlines are
90-
significant.
125+
This is
126+
a paragraph
127+
that is compacted.
128+
"""
91129

92-
Rewrapping can be disabled on a per-paragraph basis by adding a line with
93-
solely the ``\b`` escape marker in it. This line will be removed from the
94-
help text and rewrapping will be disabled.
130+
.. click:run::
131+
132+
invoke(cli, args=['--help'])
133+
134+
Escaping Click's Wrapping
135+
---------------------------
136+
Sometimes Click's wrapping can be a problem, such as when showing code examples where newlines are significant. This behavior can be escaped on a per-paragraph basis by adding a line with only ``\b`` . The ``\b`` is removed from the rendered help text.
95137

96138
Example:
97139

@@ -101,11 +143,6 @@ Example:
101143
def cli():
102144
"""First paragraph.
103145

104-
This is a very long second paragraph and as you
105-
can see wrapped very early in the source text
106-
but will be rewrapped to the terminal width in
107-
the final output.
108-
109146
\b
110147
This is
111148
a paragraph
@@ -115,71 +152,55 @@ Example:
115152
that will be rewrapped again.
116153
"""
117154

118-
And what it looks like:
119155

120156
.. click:run::
121157
122158
invoke(cli, args=['--help'])
123159

124-
To change the maximum width, pass ``max_content_width`` when calling the command.
160+
To change the rendering maximum width, pass ``max_content_width`` when calling the command.
125161

126162
.. code-block:: python
127163
128164
cli(max_content_width=120)
129165
130-
131166
.. _doc-meta-variables:
132167

133168
Truncating Help Texts
134169
---------------------
135170

136-
Click gets command help text from function docstrings. However if you
137-
already use docstrings to document function arguments you may not want
138-
to see :param: and :return: lines in your help text.
139-
140-
You can use the ``\f`` escape marker to have Click truncate the help text
141-
after the marker.
171+
Click gets :class:`Command` help text from the docstring. If you do not want to include part of the docstring, add the ``\f`` escape marker to have Click truncate the help text after the marker.
142172

143173
Example:
144174

145175
.. click:example::
146176
147177
@click.command()
148-
@click.pass_context
149-
def cli(ctx):
178+
def cli():
150179
"""First paragraph.
151-
152-
This is a very long second
153-
paragraph and not correctly
154-
wrapped but it will be rewrapped.
155180
\f
156181

157-
:param click.core.Context ctx: Click context.
182+
Words to not be included.
158183
"""
159184

160-
And what it looks like:
161-
162185
.. click:run::
163186
164187
invoke(cli, args=['--help'])
165188

166189

167-
Meta Variables
168-
--------------
190+
Placeholder / Meta Variable
191+
-----------------------------
169192

170-
Options and parameters accept a ``metavar`` argument that can change the
171-
meta variable in the help page. The default version is the parameter name
172-
in uppercase with underscores, but can be annotated differently if
173-
desired. This can be customized at all levels:
193+
The default placeholder variable (`meta variable <https://en.wikipedia.org/wiki/Metasyntactic_variable#IETF_Requests_for_Comments>`_) in the help pages is the parameter name in uppercase with underscores. This can be changed for Commands and Parameters with the ``options_metavar`` and ``metavar`` kwargs.
174194

175195
.. click:example::
176196
177-
@click.command(options_metavar='<options>')
197+
# This controls entry on the usage line.
198+
@click.command(options_metavar='[[options]]')
178199
@click.option('--count', default=1, help='number of greetings',
179200
metavar='<int>')
180201
@click.argument('name', metavar='<name>')
181-
def hello(count, name):
182-
"""This script prints hello <name> <int> times."""
202+
def hello(name: str, count: int) -> None:
203+
"""This script prints 'hello <name>' a total of <count> times."""
183204
for x in range(count):
184205
click.echo(f"Hello {name}!")
185206

@@ -189,65 +210,9 @@ Example:
189210
190211
invoke(hello, args=['--help'])
191212

192-
193-
Command Short Help
194-
------------------
195-
196-
For commands, a short help snippet is generated. By default, it's the first
197-
sentence of the help message of the command, unless it's too long. This can
198-
also be overridden:
199-
200-
.. click:example::
201-
202-
@click.group()
203-
def cli():
204-
"""A simple command line tool."""
205-
206-
@cli.command('init', short_help='init the repo')
207-
def init():
208-
"""Initializes the repository."""
209-
210-
@cli.command('delete', short_help='delete the repo')
211-
def delete():
212-
"""Deletes the repository."""
213-
214-
And what it looks like:
215-
216-
.. click:run::
217-
218-
invoke(cli, prog_name='repo.py')
219-
220-
Command Epilog Help
221-
-------------------
222-
223-
The help epilog is like the help string but it's printed at the end of the help
224-
page after everything else. Useful for showing example command usages or
225-
referencing additional help resources.
226-
227-
.. click:example::
228-
229-
@click.command(epilog='Check out our docs at https://click.palletsprojects.com/ for more details')
230-
def init():
231-
"""Initializes the repository."""
232-
233-
And what it looks like:
234-
235-
.. click:run::
236-
237-
invoke(init, prog_name='repo.py', args=['--help'])
238-
239213
Help Parameter Customization
240214
----------------------------
241-
242-
.. versionadded:: 2.0
243-
244-
The help parameter is implemented in Click in a very special manner.
245-
Unlike regular parameters it's automatically added by Click for any
246-
command and it performs automatic conflict resolution. By default it's
247-
called ``--help``, but this can be changed. If a command itself implements
248-
a parameter with the same name, the default help parameter stops accepting
249-
it. There is a context setting that can be used to override the names of
250-
the help parameters called :attr:`~Context.help_option_names`.
215+
Help parameters are automatically added by Click for any command. The default is ``--help`` but can be override by the context setting :attr:`~Context.help_option_names`. Click also performs automatic conflict resolution on the default help parameter so if a command itself implements a parameter named ``help`` then the default help will not be run.
251216

252217
This example changes the default parameters to ``-h`` and ``--help``
253218
instead of just ``--help``:
@@ -260,7 +225,6 @@ instead of just ``--help``:
260225
def cli():
261226
pass
262227

263-
And what it looks like:
264228

265229
.. click:run::
266230

docs/index.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ usage patterns.
7272
virtualenv
7373
setuptools
7474
parameters
75-
options
7675
arguments
76+
options
7777
commands
78-
prompts
7978
documentation
79+
prompts
8080
complex
8181
advanced
8282
testing

0 commit comments

Comments
 (0)