documentation.rst
This file
Path
docs/documentation.rst
163 lines · 4.2 KiB
Last changed by
Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
History of this fileTake it
The walk stopped after 200 commits: lines older than 8e31125 point at it.
Added Context.scopeArmin Ronacherbefore adoption8e31125
lines 1–78why, one step back1Documenting 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'])78Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737
line 79why, one step back79.. _doc-meta-variables:Added Context.scopeArmin Ronacherbefore adoption8e31125
lines 80–163why, one step back8081Meta Variables82--------------8384Options and parameters accept a ``metavar`` argument that can change the85meta variable in the help page. The default version is the parameter name86in uppercase with underscores, but can be annotated differently if87desired. This can be customized at all levels:8889.. click:example::9091 @click.command(options_metavar='<options>')92 @click.option('--count', default=1, help='number of greetings',93 metavar='<int>')94 @click.argument('name', metavar='<name>')95 def hello(count, name):96 """This script prints hello <name> <int> times."""97 for x in range(count):98 click.echo('Hello %s!' % name)99100Example:101102.. click:run::103104 invoke(hello, args=['--help'])105106107Command Short Help108------------------109110For commands, a short help snippet is generated. By default, it's the first111sentence of the help message of the command, unless it's too long. This can112also be overridden:113114.. click:example::115116 @click.group()117 def cli():118 """A simple command line tool."""119120 @cli.command('init', short_help='init the repo')121 def init():122 """Initializes the repository."""123124 @cli.command('delete', short_help='delete the repo')125 def delete():126 """Deletes the repository."""127128And what it looks like:129130.. click:run::131132 invoke(cli, prog_name='repo.py')133134135Help Parameter Customization136----------------------------137138.. versionadded:: 2.0139140The help parameter is implemented in Click in a very special manner.141Unlike regular parameters it's automatically added by Click for any142command and it performs automatic conflict resolution. By default it's143called ``--help``, but this can be changed. If a command itself implements144a parameter with the same name, the default help parameter stops accepting145it. There is a context setting that can be used to override the names of146the help parameters called :attr:`~Context.help_option_names`.147148This example changes the default parameters to ``-h`` and ``--help``149instead of just ``--help``:150151.. click:example::152153 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])154155 @click.command(context_settings=CONTEXT_SETTINGS)156 def cli():157 pass158159And what it looks like:160161.. click:run::162163 invoke(cli, ['-h'])