Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

241 lines · 5.7 KiB

Last changed by

Not known.

History of this file

Take it

1Documenting Scripts2===================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.1011Help Texts12----------1314Commands and options accept help arguments.  In the case of commands, the15docstring of the function is automatically used if provided.1617Simple example:1819.. click:example::2021    @click.command()22    @click.option('--count', default=1, help='number of greetings')23    @click.argument('name')24    def hello(count, name):25        """This script prints hello NAME COUNT times."""26        for x in range(count):27            click.echo(f"Hello {name}!")2829And what it looks like:3031.. click:run::3233    invoke(hello, args=['--help'])343536.. _documenting-arguments:3738Documenting Arguments39~~~~~~~~~~~~~~~~~~~~~4041: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.4546You might prefer to reference the argument in the description:4748.. click:example::4950    @click.command()51    @click.argument('filename')52    def touch(filename):53        """Print FILENAME."""54        click.echo(filename)5556And what it looks like:5758.. click:run::5960    invoke(touch, args=['--help'])6162Or you might prefer to explicitly provide a description of the argument:6364.. click:example::6566    @click.command()67    @click.argument('filename')68    def touch(filename):69        """Print FILENAME.7071        FILENAME is the name of the file to check.72        """73        click.echo(filename)7475And what it looks like:7677.. click:run::7879    invoke(touch, args=['--help'])8081For more examples, see the examples in :doc:`/arguments`.828384Preventing Rewrapping85---------------------8687The default behavior of Click is to rewrap text based on the width of the88terminal.  In some circumstances, this can become a problem. The main issue89is when showing code examples, where newlines are significant.9091Rewrapping can be disabled on a per-paragraph basis by adding a line with92solely the ``\b`` escape marker in it.  This line will be removed from the93help text and rewrapping will be disabled.9495Example:9697.. click:example::9899    @click.command()100    def cli():101        """First paragraph.102103        This is a very long second paragraph and as you104        can see wrapped very early in the source text105        but will be rewrapped to the terminal width in106        the final output.107108        \b109        This is110        a paragraph111        without rewrapping.112113        And this is a paragraph114        that will be rewrapped again.115        """116117And what it looks like:118119.. click:run::120121    invoke(cli, args=['--help'])122123.. _doc-meta-variables:124125Truncating Help Texts126---------------------127128Click gets command help text from function docstrings.  However if you129already use docstrings to document function arguments you may not want130to see :param: and :return: lines in your help text.131132You can use the ``\f`` escape marker to have Click truncate the help text133after the marker.134135Example:136137.. click:example::138139    @click.command()140    @click.pass_context141    def cli(ctx):142        """First paragraph.143144        This is a very long second145        paragraph and not correctly146        wrapped but it will be rewrapped.147        \f148149        :param click.core.Context ctx: Click context.150        """151152And what it looks like:153154.. click:run::155156    invoke(cli, args=['--help'])157158159Meta Variables160--------------161162Options and parameters accept a ``metavar`` argument that can change the163meta variable in the help page.  The default version is the parameter name164in uppercase with underscores, but can be annotated differently if165desired.  This can be customized at all levels:166167.. click:example::168169    @click.command(options_metavar='<options>')170    @click.option('--count', default=1, help='number of greetings',171                  metavar='<int>')172    @click.argument('name', metavar='<name>')173    def hello(count, name):174        """This script prints hello <name> <int> times."""175        for x in range(count):176            click.echo(f"Hello {name}!")177178Example:179180.. click:run::181182    invoke(hello, args=['--help'])183184185Command Short Help186------------------187188For commands, a short help snippet is generated.  By default, it's the first189sentence of the help message of the command, unless it's too long.  This can190also be overridden:191192.. click:example::193194    @click.group()195    def cli():196        """A simple command line tool."""197198    @cli.command('init', short_help='init the repo')199    def init():200        """Initializes the repository."""201202    @cli.command('delete', short_help='delete the repo')203    def delete():204        """Deletes the repository."""205206And what it looks like:207208.. click:run::209210    invoke(cli, prog_name='repo.py')211212213Help Parameter Customization214----------------------------215216.. versionadded:: 2.0217218The help parameter is implemented in Click in a very special manner.219Unlike regular parameters it's automatically added by Click for any220command and it performs automatic conflict resolution.  By default it's221called ``--help``, but this can be changed.  If a command itself implements222a parameter with the same name, the default help parameter stops accepting223it.  There is a context setting that can be used to override the names of224the help parameters called :attr:`~Context.help_option_names`.225226This example changes the default parameters to ``-h`` and ``--help``227instead of just ``--help``:228229.. click:example::230231    CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])232233    @click.command(context_settings=CONTEXT_SETTINGS)234    def cli():235        pass236237And what it looks like:238239.. click:run::240241    invoke(cli, ['-h'])