Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

162 lines · 4.2 KiB

Last changed by

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

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('Hello %s!' % name)2829And what it looks like:3031.. click:run::3233    invoke(hello, args=['--help'])3435Arguments cannot be documented this way.  This is to follow the general36convention of Unix tools of using arguments for only the most necessary37things and to document them in the introduction text by referring to them38by name.3940Preventing Rewrapping41---------------------4243The default behavior of click is to rewrap text based on the width of the44terminal.  In some circumstances, this can become a problem. The main issue45is when showing code examples, where newlines are significant.4647Rewrapping can be disabled on a per-paragraph basis by adding a line with48solely the ``\b`` escape marker in it.  This line will be removed from the49help text and rewrapping will be disabled.5051Example:5253.. click:example::5455    @click.command()56    def cli():57        """First paragraph.5859        This is a very long second paragraph and as you60        can see wrapped very early in the source text61        but will be rewrapped to the terminal width in62        the final output.6364        \b65        This is66        a paragraph67        without rewrapping.6869        And this is a paragraph70        that will be rewrapped again.71        """7273And what it looks like:7475.. click:run::7677    invoke(cli, args=['--help'])787980Meta Variables81--------------8283Options and parameters accept a ``metavar`` argument that can change the84meta variable in the help page.  The default version is the parameter name85in uppercase with underscores, but can be annotated differently if86desired.  This can be customized at all levels:8788.. click:example::8990    @click.command(options_metavar='<options>')91    @click.option('--count', default=1, help='number of greetings',92                  metavar='<int>')93    @click.argument('name', metavar='<name>')94    def hello(count, name):95        """This script prints hello <name> <int> times."""96        for x in range(count):97            click.echo('Hello %s!' % name)9899Example:100101.. click:run::102103    invoke(hello, args=['--help'])104105106Command Short Help107------------------108109For commands, a short help snippet is generated.  By default, it's the first110sentence of the help message of the command, unless it's too long.  This can111also be overridden:112113.. click:example::114115    @click.group()116    def cli():117        """A simple command line tool."""118119    @cli.command('init', short_help='init the repo')120    def init():121        """Initializes the repository."""122123    @cli.command('delete', short_help='delete the repo')124    def delete():125        """Deletes the repository."""126127And what it looks like:128129.. click:run::130131    invoke(cli, prog_name='repo.py')132133134Help Parameter Customization135----------------------------136137.. versionadded:: 2.0138139The help parameter is implemented in click in a very special manner.140Unlike regular parameters it's automatically added by click for any141command and it performs automatic conflict resolution.  By default it's142called ``--help``, but this can be changed.  If a command itself implements143a parameter with the same name, the default help parameter stops accepting144it.  There is a context setting that can be used to override the names of145the help parameters called :attr:`~Context.help_option_names`.146147This example changes the default parameters to ``-h`` and ``--help``148instead of just ``--help``:149150.. click:example::151152    CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])153154    @click.command(context_settings=CONTEXT_SETTINGS)155    def cli():156        pass157158And what it looks like:159160.. click:run::161162    invoke(cli, ['-h'])