Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

231 lines · 6.0 KiB

Last changed by

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

History of this file

Take it

The walk stopped after 200 commits: lines older than 68b3d4f point at it.

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

line 1why, one step back
1Help Pages

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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.

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix typos on help pages docs.Edward Gbefore adoption2a6b6bf

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 10why, one step back
10

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix typos on help pages docs.Edward Gbefore adoption2a6b6bf

line 18why, one step back
18 @click.argument('name')

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 19why, one step back
19 @click.option('--count', default=1, help='number of greetings')

Fix typos on help pages docs.Edward Gbefore adoption2a6b6bf

line 20why, one step back
20 def hello(name: str, count: int):

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

line 21why, one step back
21 """This script prints hello and a name one or more times."""

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 35–37why, one step back
35Command Short Help
36------------------37

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

line 38why, one step back
38For 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``:

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 39–61why, one step back
39
40.. click:example::4142 @click.group()43 def cli():44 """A simple command line tool."""4546 @cli.command('init', short_help='init the repo')47 def init():48 """Initializes the repository."""495051.. click:run::5253 invoke(cli, args=['--help'])5455Command Epilog Help56-------------------5758The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.5960.. click:example::61

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

lines 62–64why, one step back
62 @click.command(
63 epilog='See https://example.com for more details',64 )

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 65–72why, one step back
65 def init():
66 """Initializes the repository."""676869.. click:run::7071 invoke(init, args=['--help'])72

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 73why, one step back
73Documenting Arguments

Merge stable into main (#2799)Rowlando13before adoptiond791537

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 75why, one step back
75

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

line 76why, 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

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

line 77why, one step back
77by name. This should then be done via the docstring.

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 78why, one step back
78

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 111–113why, one step back
111Click's Wrapping Behavior
112----------------------------113Click'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.

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 114why, one step back
114

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 116why, one step back
116

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 117–123why, one step back
117 @click.command()
118 def cli():119 """120 This is a very long paragraph and as you121 can see wrapped very early in the source text122 but will be rewrapped to the terminal width in123 the final output.

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 124why, one step back
124

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 125–128why, one step back
125 This is
126 a paragraph127 that is compacted.128 """

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 129why, one step back
129

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

lines 130–137why, one step back
130.. click:run::
131132 invoke(cli, args=['--help'])133134Escaping Click's Wrapping135---------------------------136Sometimes 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.137

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 138–159why, one step back
138Example:
139140.. click:example::141142 @click.command()143 def cli():144 """First paragraph.145146 \b147 This is148 a paragraph149 without rewrapping.150151 And this is a paragraph152 that will be rewrapped again.153 """154155156.. click:run::157158 invoke(cli, args=['--help'])159

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge branch '8.1.x'David Lordbefore adoptiond9af5cf

lines 161–165why, one step back
161
162.. code-block:: python163164 cli(max_content_width=120)165

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 166–170why, one step back
166.. _doc-meta-variables:
167168Truncating Help Texts169---------------------170

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 172–177why, one step back
172
173Example:174175.. click:example::176177 @click.command()

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 179–181why, one step back
179 """First paragraph.
180 \f181

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 183–189why, one step back
183 """
184185.. click:run::186187 invoke(cli, args=['--help'])188189

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

line 190why, one step back
190Placeholder / Meta Variable

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

line 191why, one step back
191-----------------------

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

line 192why, one step back
192

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 194–196why, one step back
194
195.. click:example::196

Fix merge conflicts in toc tree.Edward Gbefore adoption76db878

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

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

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 204–214why, one step back
204 for x in range(count):
205 click.echo(f"Hello {name}!")206207Example:208209.. click:run::210211 invoke(hello, args=['--help'])212213Help Parameter Customization214----------------------------

Merge branch 'rewrite_help_pages_2' of https://github.com/Rowlando13/click into rewrite_help_pages_2Edward Gbefore adoptionedb471d

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

Merge pull request #1836 from MLH-Fellowship/fix-promptDavid Lordbefore adoption68b3d4f

lines 216–231why, one step back
216
217This example changes the default parameters to ``-h`` and ``--help``218instead of just ``--help``:219220.. click:example::221222 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])223224 @click.command(context_settings=CONTEXT_SETTINGS)225 def cli():226 pass227228229.. click:run::230231 invoke(cli, ['-h'])