Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

197 lines · 4.9 KiB

Last changed by

Merge pull request #1091 from sirosen/truncate-helpDavid Lordbefore adoption011fd62

History of this file

Take it

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

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoptionc55d7d2

lines 1–78why, one step back
1Documenting Scripts
2===================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'])78

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

line 79why, one step back
79.. _doc-meta-variables:

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoptionc55d7d2

line 80why, one step back
80

Merge pull request #1091 from sirosen/truncate-helpDavid Lordbefore adoption011fd62

lines 81–114why, one step back
81Truncating Help Texts
82---------------------8384Click gets command help text from function docstrings. However if you85already use docstrings to document function arguments you may not want86to see :param: and :return: lines in your help text.8788You can use the ``\f`` escape marker to have Click truncate the help text89after the marker.9091Example:9293.. click:example::9495 @click.command()96 @click.pass_context97 def cli(ctx):98 """First paragraph.99100 This is a very long second101 paragraph and not correctly102 wrapped but it will be rewrapped.103 \f104105 :param click.core.Context ctx: Click context.106 """107108And what it looks like:109110.. click:run::111112 invoke(cli, args=['--help'])113114

Merge branch 'master' of github.com:mitsuhiko/clickArmin Ronacherbefore adoptionc55d7d2

lines 115–197why, one step back
115Meta Variables
116--------------117118Options and parameters accept a ``metavar`` argument that can change the119meta variable in the help page. The default version is the parameter name120in uppercase with underscores, but can be annotated differently if121desired. This can be customized at all levels:122123.. click:example::124125 @click.command(options_metavar='<options>')126 @click.option('--count', default=1, help='number of greetings',127 metavar='<int>')128 @click.argument('name', metavar='<name>')129 def hello(count, name):130 """This script prints hello <name> <int> times."""131 for x in range(count):132 click.echo('Hello %s!' % name)133134Example:135136.. click:run::137138 invoke(hello, args=['--help'])139140141Command Short Help142------------------143144For commands, a short help snippet is generated. By default, it's the first145sentence of the help message of the command, unless it's too long. This can146also be overridden:147148.. click:example::149150 @click.group()151 def cli():152 """A simple command line tool."""153154 @cli.command('init', short_help='init the repo')155 def init():156 """Initializes the repository."""157158 @cli.command('delete', short_help='delete the repo')159 def delete():160 """Deletes the repository."""161162And what it looks like:163164.. click:run::165166 invoke(cli, prog_name='repo.py')167168169Help Parameter Customization170----------------------------171172.. versionadded:: 2.0173174The help parameter is implemented in Click in a very special manner.175Unlike regular parameters it's automatically added by Click for any176command and it performs automatic conflict resolution. By default it's177called ``--help``, but this can be changed. If a command itself implements178a parameter with the same name, the default help parameter stops accepting179it. There is a context setting that can be used to override the names of180the help parameters called :attr:`~Context.help_option_names`.181182This example changes the default parameters to ``-h`` and ``--help``183instead of just ``--help``:184185.. click:example::186187 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])188189 @click.command(context_settings=CONTEXT_SETTINGS)190 def cli():191 pass192193And what it looks like:194195.. click:run::196197 invoke(cli, ['-h'])