Skip to content
Myr

Rewrite help page section. (#2821)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Rewrite docs for generating help pages. Make it more concise. Reorder sections.

authored by Rowlando13 · committed by GitHub · parents 45f89cd, accbb4e · Browse files · 2 files+89 added−125 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+89 added−125 removed

  • other:+89 added−125 removed

2 files+89 added−125 removed

modified:docs/documentation.rst+87 added-123 removed
1Documenting Scripts1Help Pages2===================34.. currentmodule:: click56Click makes it very easy to document your command line tools. First of7all, it automatically generates help pages for you. While these are8currently not customizable in terms of their layout, all of the text9can be changed.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.78Help Texts12----------9--------------1014Commands and options accept help arguments. In the case of commands, the15docstring of the function is automatically used if provided.11Commands and options accept help arguments. For commands, the docstring of the function is automatically used if provided.1213Simple example:1415.. click:example::1617 @click.command()22 @click.option('--count', default=1, help='number of greetings')18 @click.argument('name')19 @click.option('--count', default=1, help='number of greetings')24 def hello(count, name):25 """This script prints hello NAME COUNT times."""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:27 click.echo(f"Hello {name}!")24 click.echo(f"Hello {name}!")25 else:26 click.echo("Hello!")2729And what it looks like:3028.. click:run::2930 invoke(hello, args=['--help'])313233.. _documenting-arguments:3435Command Short Help36------------------3738For 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``:3940.. 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::6162 @click.command(63 epilog='See https://example.com for more details',64 )65 def init():66 """Initializes the repository."""676869.. click:run::7071 invoke(init, args=['--help'])7273Documenting Arguments74----------------------7541:func:`click.argument` does not take a ``help`` parameter. This is to42follow the general convention of Unix tools of using arguments for only43the most necessary things, and to document them in the command help text44by 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 text77by name. This should then be done via the docstring.7846You might prefer to reference the argument in the description:79A brief example:8081.. click:example::8283 @click.command()
1 unmodified line
85 def touch(filename):86 """Print FILENAME."""87 click.echo(filename)8856And what it looks like:8990.. click:run::9192 invoke(touch, args=['--help'])9362Or you might prefer to explicitly provide a description of the argument:94Or more explicitly:9596.. click:example::9798 @click.command()
4 unmodified lines
103 FILENAME is the name of the file to check.104 """105 click.echo(filename)10675And what it looks like:76107.. click:run::108109 invoke(touch, args=['--help'])11081For more examples, see the examples in :doc:`/arguments`.111Click's Wrapping Behavior112----------------------------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.114115.. click:example::11684Preventing Rewrapping85---------------------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.12487The default behavior of Click is to rewrap text based on the width of the88terminal, to a maximum 80 characters. In some circumstances, this can become89a problem. The main issue is when showing code examples, where newlines are90significant.125 This is126 a paragraph127 that is compacted.128 """12992Rewrapping can be disabled on a per-paragraph basis by adding a line with93solely the ``\b`` escape marker in it. This line will be removed from the94help text and rewrapping will be disabled.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.137138Example:139140.. click:example::141142 @click.command()143 def cli():144 """First paragraph.145104 This is a very long second paragraph and as you105 can see wrapped very early in the source text106 but will be rewrapped to the terminal width in107 the final output.108146 \b147 This is148 a paragraph149 without rewrapping.
1 unmodified line
151 And this is a paragraph152 that will be rewrapped again.153 """154118And what it looks like:155156.. click:run::157158 invoke(cli, args=['--help'])159124To change the maximum width, pass ``max_content_width`` when calling the command.160To change the rendering maximum width, pass ``max_content_width`` when calling the command.161162.. code-block:: python163164 cli(max_content_width=120)165130166.. _doc-meta-variables:167168Truncating Help Texts169---------------------170136Click gets command help text from function docstrings. However if you137already use docstrings to document function arguments you may not want138to see :param: and :return: lines in your help text.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.172140You can use the ``\f`` escape marker to have Click truncate the help text141after the marker.142173Example:174175.. click:example::176177 @click.command()148 @click.pass_context149 def cli(ctx):178 def cli():179 """First paragraph.151152 This is a very long second153 paragraph and not correctly154 wrapped but it will be rewrapped.180 \f181157 :param click.core.Context ctx: Click context.182 Words to not be included.183 """184160And what it looks like:161185.. click:run::186187 invoke(cli, args=['--help'])188189167Meta Variables168--------------190Placeholder / Meta Variable191-----------------------------192170Options and parameters accept a ``metavar`` argument that can change the171meta variable in the help page. The default version is the parameter name172in uppercase with underscores, but can be annotated differently if173desired. This can be customized at all levels: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.194195.. click:example::196197 # This controls entry on the usage line.177 @click.command(options_metavar='<options>')198 @click.command(options_metavar='[[options]]')199 @click.option('--count', default=1, help='number of greetings',200 metavar='<int>')201 @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."""204 for x in range(count):205 click.echo(f"Hello {name}!")206207Example:
1 unmodified line
209.. click:run::210211 invoke(hello, args=['--help'])212192193Command Short Help194------------------195196For commands, a short help snippet is generated. By default, it's the first197sentence of the help message of the command, unless it's too long. This can198also be overridden:199200.. click:example::201202 @click.group()203 def cli():204 """A simple command line tool."""205206 @cli.command('init', short_help='init the repo')207 def init():208 """Initializes the repository."""209210 @cli.command('delete', short_help='delete the repo')211 def delete():212 """Deletes the repository."""213214And what it looks like:215216.. click:run::217218 invoke(cli, prog_name='repo.py')219220Command Epilog Help221-------------------222223The help epilog is like the help string but it's printed at the end of the help224page after everything else. Useful for showing example command usages or225referencing additional help resources.226227.. click:example::228229 @click.command(epilog='Check out our docs at https://click.palletsprojects.com/ for more details')230 def init():231 """Initializes the repository."""232233And what it looks like:234235.. click:run::236237 invoke(init, prog_name='repo.py', args=['--help'])238213Help Parameter Customization214----------------------------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.216242.. versionadded:: 2.0243244The help parameter is implemented in Click in a very special manner.245Unlike regular parameters it's automatically added by Click for any246command and it performs automatic conflict resolution. By default it's247called ``--help``, but this can be changed. If a command itself implements248a parameter with the same name, the default help parameter stops accepting249it. There is a context setting that can be used to override the names of250the help parameters called :attr:`~Context.help_option_names`.251217This example changes the default parameters to ``-h`` and ``--help``218instead of just ``--help``:219220.. click:example::
3 unmodified lines
224 @click.command(context_settings=CONTEXT_SETTINGS)225 def cli():226 pass227263And what it looks like:228229.. click:run::230231 invoke(cli, ['-h'])
modified:docs/index.rst+2 added-2 removed
70 unmodified lines
71 entry-points72 virtualenv73 setuptools74 parameters75 options75 arguments76 options77 commands78 prompts78 documentation79 prompts80 complex81 advanced82 testing83 utils
25 unmodified lines