Documentation.rst reST to MyST (#2980)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rowlando13 · committed by GitHub · parents c1a8939, 59acbe9 · Browse files · 2 files·+277 added−268 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+277 added−268 removed
- other:+277 added−268 removed
Provenance
- Commit
5c1239b- Tree
0501096- Author
Rowlando13 <67291205+Rowlando13@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent 1
c1a8939- Parent 2
59acbe9
2 files changed+277 added−268 removed
added:docs/documentation.md+277 added
1+# Help Pages2+3+```{currentmodule} click4+```5+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.7+8+## Help Texts9+10+Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.11+12+Simple example:13+14+```{eval-rst}15+.. click:example::16+17+ @click.command()18+ @click.argument('name')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."""22+ for x in range(count):23+ if name:24+ click.echo(f"Hello {name}!")25+ else:26+ click.echo("Hello!")27+28+.. click:run::29+ invoke(hello, args=['--help'])30+```31+32+## Command Short Help33+34+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`:35+36+```{eval-rst}37+.. click:example::38+39+ import click40+41+ @click.group()42+ def cli():43+ """A simple command line tool."""44+45+ @cli.command('init', short_help='init the repo')46+ def init():47+ """Initializes the repository."""48+49+.. click:run::50+ invoke(cli, args=['--help'])51+```52+53+## Command Epilog Help54+55+The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.56+57+```{eval-rst}58+.. click:example::59+60+ import click61+62+ @click.command(63+ epilog='See https://example.com for more details',64+ )65+ def init():66+ """Initializes the repository."""67+68+.. click:run::69+ invoke(init, args=['--help'])70+```71+72+(documenting-arguments)=73+74+## Documenting Arguments75+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 text77+by name. This should then be done via the docstring.78+79+A brief example:80+81+```{eval-rst}82+.. click:example::83+84+ @click.command()85+ @click.argument('filename')86+ def touch(filename):87+ """Print FILENAME."""88+ click.echo(filename)89+90+.. click:run::91+ invoke(touch, args=['--help'])92+```93+94+Or more explicitly:95+96+```{eval-rst}97+.. click:example::98+99+ @click.command()100+ @click.argument('filename')101+ def touch(filename):102+ """Print FILENAME.103+104+ FILENAME is the name of the file to check.105+ """106+ click.echo(filename)107+108+.. click:run::109+ invoke(touch, args=['--help'])110+```111+112+## Showing Defaults113+114+To control the appearance of defaults pass `show_default`.115+116+```{eval-rst}117+.. click:example::118+119+ @click.command()120+ @click.option('--n', default=1, show_default=False, help='number of dots')121+ def dots(n):122+ click.echo('.' * n)123+124+.. click:run::125+ invoke(dots, args=['--help'])126+```127+128+For single option boolean flags, the default remains hidden if the default value is False, even if show default is set to true.129+130+```{eval-rst}131+.. click:example::132+133+ @click.command()134+ @click.option('--n', default=1, show_default=True)135+ @click.option("--gr", is_flag=True, show_default=True, default=False, help="Greet the world.")136+ @click.option("--br", is_flag=True, show_default=True, default=True, help="Add a thematic break")137+ def dots(n, gr, br):138+ if gr:139+ click.echo('Hello world!')140+ click.echo('.' * n)141+ if br:142+ click.echo('-' * n)143+144+.. click:run::145+ invoke(dots, args=['--help'])146+```147+148+## Click's Wrapping Behavior149+150+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.151+152+```{eval-rst}153+.. click:example::154+155+ import click156+157+ @click.command()158+ def cli():159+ """160+ This is a very long paragraph and as you161+ can see wrapped very early in the source text162+ but will be rewrapped to the terminal width in163+ the final output.164+165+ This is166+ a paragraph167+ that is compacted.168+ """169+170+.. click:run::171+ invoke(cli, args=['--help'])172+```173+174+## Escaping Click's Wrapping175+176+Sometimes Click's wrapping can be a problem, such as when showing code examples where new lines 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.177+178+Example:179+180+```{eval-rst}181+.. click:example::182+183+ import click184+185+ @click.command()186+ def cli():187+ """First paragraph.188+189+ \b190+ This is191+ a paragraph192+ without rewrapping.193+194+ And this is a paragraph195+ that will be rewrapped again.196+ """197+198+.. click:run::199+ invoke(cli, args=['--help'])200+```201+202+To change the rendering maximum width, pass `max_content_width` when calling the command.203+204+```bash205+cli(max_content_width=120)206+```207+208+## Truncating Help Texts209+210+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.211+212+Example:213+214+```{eval-rst}215+.. click:example::216+217+ import click218+219+ @click.command()220+ def cli():221+ """First paragraph.222+ \f223+224+ Words to not be included.225+ """226+227+.. click:run::228+ invoke(cli, args=['--help'])229+```230+231+(doc-meta-variables)=232+233+## Placeholder / Meta Variable234+235+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.236+237+```{eval-rst}238+.. click:example::239+240+ # This controls entry on the usage line.241+ @click.command(options_metavar='[[options]]')242+ @click.option('--count', default=1, help='number of greetings',243+ metavar='<int>')244+ @click.argument('name', metavar='<name>')245+ def hello(name: str, count: int) -> None:246+ """This script prints 'hello <name>' a total of <count> times."""247+ for x in range(count):248+ click.echo(f"Hello {name}!")249+250+# Example usage:251+252+.. click:run::253+ invoke(hello, args=['--help'])254+255+```256+257+## Help Parameter Customization258+259+Help parameters are automatically added by Click for any command. The default is `--help` but can be overridden 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.260+261+This example changes the default parameters to `-h` and `--help`262+instead of just `--help`:263+264+```{eval-rst}265+.. click:example::266+267+ import click268+269+ CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])270+271+ @click.command(context_settings=CONTEXT_SETTINGS)272+ def cli():273+ pass274+275+.. click:run::276+ invoke(cli, ['-h'])277+```deleted:docs/documentation.rst-268 removed
1-Help Pages2-===================3-4-.. currentmodule:: click5-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.7-8-Help Texts9---------------10-11-Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.12-13-Simple example:14-15-.. click:example::16-17- @click.command()18- @click.argument('name')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."""22- for x in range(count):23- if name:24- click.echo(f"Hello {name}!")25- else:26- click.echo("Hello!")27-28-.. click:run::29-30- invoke(hello, args=['--help'])31-32-33-Command Short Help34-------------------35-36-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``:37-38-.. click:example::39-40- @click.group()41- def cli():42- """A simple command line tool."""43-44- @cli.command('init', short_help='init the repo')45- def init():46- """Initializes the repository."""47-48-49-.. click:run::50-51- invoke(cli, args=['--help'])52-53-Command Epilog Help54--------------------55-56-The help epilog is printed at the end of the help and is useful for showing example command usages or referencing additional help resources.57-58-.. click:example::59-60- @click.command(61- epilog='See https://example.com for more details',62- )63- def init():64- """Initializes the repository."""65-66-67-.. click:run::68-69- invoke(init, args=['--help'])70-71-.. _documenting-arguments:72-73-Documenting Arguments74-----------------------75-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 text77-by name. This should then be done via the docstring.78-79-A brief example:80-81-.. click:example::82-83- @click.command()84- @click.argument('filename')85- def touch(filename):86- """Print FILENAME."""87- click.echo(filename)88-89-90-.. click:run::91-92- invoke(touch, args=['--help'])93-94-Or more explicitly:95-96-.. click:example::97-98- @click.command()99- @click.argument('filename')100- def touch(filename):101- """Print FILENAME.102-103- FILENAME is the name of the file to check.104- """105- click.echo(filename)106-107-.. click:run::108-109- invoke(touch, args=['--help'])110-111-112-Showing Defaults113----------------------------114-To control the appearance of defaults pass ``show_default``.115-116-.. click:example::117-118- @click.command()119- @click.option('--n', default=1, show_default=False, help='number of dots')120- def dots(n):121- click.echo('.' * n)122-123-.. click:run::124-125- invoke(dots, args=['--help'])126-127-For single option boolean flags, the default remains hidden if the default value is False even if show default is true.128-129-.. click:example::130-131- @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)141-142-.. click:run::143-144- invoke(dots, args=['--help'])145-146-147-Click's Wrapping Behavior148-----------------------------149-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.150-151-.. click:example::152-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.160-161- This is162- a paragraph163- that is compacted.164- """165-166-.. click:run::167-168- invoke(cli, args=['--help'])169-170-Escaping Click's Wrapping171----------------------------172-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.173-174-Example:175-176-.. click:example::177-178- @click.command()179- def cli():180- """First paragraph.181-182- \b183- This is184- a paragraph185- without rewrapping.186-187- And this is a paragraph188- that will be rewrapped again.189- """190-191-192-.. click:run::193-194- invoke(cli, args=['--help'])195-196-To change the rendering maximum width, pass ``max_content_width`` when calling the command.197-198-.. code-block:: python199-200- cli(max_content_width=120)201-202-203-204-Truncating Help Texts205----------------------206-207-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.208-209-Example:210-211-.. click:example::212-213- @click.command()214- def cli():215- """First paragraph.216- \f217-218- Words to not be included.219- """220-221-.. click:run::222-223- invoke(cli, args=['--help'])224-225-.. _doc-meta-variables:226-227-Placeholder / Meta Variable228------------------------------229-230-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.231-232-.. click:example::233-234- # This controls entry on the usage line.235- @click.command(options_metavar='[[options]]')236- @click.option('--count', default=1, help='number of greetings',237- metavar='<int>')238- @click.argument('name', metavar='<name>')239- def hello(name: str, count: int) -> None:240- """This script prints 'hello <name>' a total of <count> times."""241- for x in range(count):242- click.echo(f"Hello {name}!")243-244-Example:245-246-.. click:run::247-248- invoke(hello, args=['--help'])249-250-Help Parameter Customization251-----------------------------252-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.253-254-This example changes the default parameters to ``-h`` and ``--help``255-instead of just ``--help``:256-257-.. click:example::258-259- CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])260-261- @click.command(context_settings=CONTEXT_SETTINGS)262- def cli():263- pass264-265-266-.. click:run::267-268- invoke(cli, ['-h'])