Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

268 lines · 7.0 KiB

Last changed by

Merge branch 'rst2myst' of https://github.com/denuoweb/click into rst2mystdenuowebbefore adoption7b4f1ce

History of this file

Take it

The walk stopped after 200 commits: lines older than 08ecfd0 point at it.

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 1why, one step back
1Help Pages

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 2–5why, one step back
2===================
34.. currentmodule:: click5

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 6why, one step back
6Click 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.

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 7–8why, one step back
7
8Help Texts

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 9why, one step back
9--------------

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 10why, one step back
10

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 11why, one step back
11Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 12–18why, one step back
12
13Simple example:1415.. click:example::1617 @click.command()18 @click.argument('name')

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 19–21why, one step back
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."""

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 22why, one step back
22 for x in range(count):

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 23–26why, one step back
23 if name:
24 click.echo(f"Hello {name}!")25 else:26 click.echo("Hello!")

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 27–32why, one step back
27
28.. click:run::2930 invoke(hello, args=['--help'])3132

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 33–69why, one step back
33Command Short Help
34------------------3536For 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``:3738.. click:example::3940 @click.group()41 def cli():42 """A simple command line tool."""4344 @cli.command('init', short_help='init the repo')45 def init():46 """Initializes the repository."""474849.. click:run::5051 invoke(cli, args=['--help'])5253Command Epilog Help54-------------------5556The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.5758.. click:example::5960 @click.command(61 epilog='See https://example.com for more details',62 )63 def init():64 """Initializes the repository."""656667.. click:run::6869 invoke(init, args=['--help'])

Fix documenting-arguments anchor in docs (#2921)Rowlando13before adoptiondce3f9e

lines 70–71why, one step back
70
71.. _documenting-arguments:

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 72why, one step back
72

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 73why, one step back
73Documenting Arguments

Merge stable into main (#2799)Rowlando13before adoptiond791537

line 74why, one step back
74----------------------

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 75why, one step back
75

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 76–77why, one step back
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
77by name. This should then be done via the docstring.

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 78why, one step back
78

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 79why, one step back
79A brief example:

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 80–93why, one step back
80
81.. click:example::8283 @click.command()84 @click.argument('filename')85 def touch(filename):86 """Print FILENAME."""87 click.echo(filename)888990.. click:run::9192 invoke(touch, args=['--help'])93

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 94why, one step back
94Or more explicitly:

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 95–110why, one step back
95
96.. click:example::9798 @click.command()99 @click.argument('filename')100 def touch(filename):101 """Print FILENAME.102103 FILENAME is the name of the file to check.104 """105 click.echo(filename)106107.. click:run::108109 invoke(touch, args=['--help'])110

Rewrite first half of options docs (#2825)Rowlando13before adoption82089af

lines 111–146why, one step back
111
112Showing Defaults113---------------------------114To control the appearance of defaults pass ``show_default``.115116.. click:example::117118 @click.command()119 @click.option('--n', default=1, show_default=False, help='number of dots')120 def dots(n):121 click.echo('.' * n)122123.. click:run::124125 invoke(dots, args=['--help'])126127For single option boolean flags, the default remains hidden if the default value is False even if show default is true.128129.. click:example::130131 @click.command()132 @click.option('--n', default=1, show_default=True)133 @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")134 @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")135 def dots(n, gr, br):136 if gr:137 click.echo('Hello world!')138 click.echo('.' * n)139 if br:140 click.echo('-' * n)141142.. click:run::143144 invoke(dots, args=['--help'])145146

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 147–149why, one step back
147Click's Wrapping Behavior
148----------------------------149Click'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.

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 150why, one step back
150

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 151why, one step back
151.. click:example::

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 152why, one step back
152

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 153–159why, one step back
153 @click.command()
154 def cli():155 """156 This is a very long paragraph and as you157 can see wrapped very early in the source text158 but will be rewrapped to the terminal width in159 the final output.

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 160why, one step back
160

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 161–164why, one step back
161 This is
162 a paragraph163 that is compacted.164 """

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 165why, one step back
165

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 166why, one step back
166.. click:run::

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 167why, one step back
167

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 168–173why, one step back
168 invoke(cli, args=['--help'])
169170Escaping Click's Wrapping171---------------------------172Sometimes 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.173

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 174–195why, one step back
174Example:
175176.. click:example::177178 @click.command()179 def cli():180 """First paragraph.181182 \b183 This is184 a paragraph185 without rewrapping.186187 And this is a paragraph188 that will be rewrapped again.189 """190191192.. click:run::193194 invoke(cli, args=['--help'])195

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 196why, one step back
196To change the rendering maximum width, pass ``max_content_width`` when calling the command.

Merge branch '8.1.x'David Lordbefore adoptiond9af5cf

lines 197–201why, one step back
197
198.. code-block:: python199200 cli(max_content_width=120)201

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 202why, one step back
202

Merge branch 'rst2myst' of https://github.com/denuoweb/click into rst2mystdenuowebbefore adoption7b4f1ce

line 203why, one step back
203

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 204–206why, one step back
204Truncating Help Texts
205---------------------206

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 207why, one step back
207Click 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.

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 208–213why, one step back
208
209Example:210211.. click:example::212213 @click.command()

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 214why, one step back
214 def cli():

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 215–217why, one step back
215 """First paragraph.
216 \f217

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 218why, one step back
218 Words to not be included.

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 219–224why, one step back
219 """
220221.. click:run::222223 invoke(cli, args=['--help'])224

Merge branch 'rst2myst' of https://github.com/denuoweb/click into rst2mystdenuowebbefore adoption7b4f1ce

line 225why, one step back
225.. _doc-meta-variables:

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 226why, one step back
226

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 227–228why, one step back
227Placeholder / Meta Variable
228-----------------------------

keep !r format for nowDavid Lordbefore adoption08ecfd0

line 229why, one step back
229

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 230why, one step back
230The 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.

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 231–233why, one step back
231
232.. click:example::233

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 234–235why, one step back
234 # This controls entry on the usage line.
235 @click.command(options_metavar='[[options]]')

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 236–238why, one step back
236 @click.option('--count', default=1, help='number of greetings',
237 metavar='<int>')238 @click.argument('name', metavar='<name>')

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

lines 239–240why, one step back
239 def hello(name: str, count: int) -> None:
240 """This script prints 'hello <name>' a total of <count> times."""

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 241–251why, one step back
241 for x in range(count):
242 click.echo(f"Hello {name}!")243244Example:245246.. click:run::247248 invoke(hello, args=['--help'])249250Help Parameter Customization251----------------------------

Rewrite help page section. (#2821)Rowlando13before adoption8a47580

line 252why, one step back
252Help 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.

keep !r format for nowDavid Lordbefore adoption08ecfd0

lines 253–268why, one step back
253
254This example changes the default parameters to ``-h`` and ``--help``255instead of just ``--help``:256257.. click:example::258259 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])260261 @click.command(context_settings=CONTEXT_SETTINGS)262 def cli():263 pass264265266.. click:run::267268 invoke(cli, ['-h'])