documentation.rst
This file
Path
docs/documentation.rst
162 lines · 4.2 KiB
Last changed by
Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
History of this fileTake it
The walk stopped after 200 commits: lines older than 5cae326 point at it.
Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326
lines 1–3why, one step back1Documenting Scripts2===================3Added an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
lines 4–5why, one step back4.. currentmodule:: click5Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326
lines 6–7why, one step back6Click makes it very easy to document your command line tools. First of7all, it automatically generates help pages for you. While these areMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 8–9why, one step back8currently not customizable in terms of their layout, all of the text9can be changed.Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326
lines 10–13why, one step back1011Help Texts12----------13Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 14–15why, one step back14Commands and options accept help arguments. In the case of commands, the15docstring of the function is automatically used if provided.Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326
lines 16–34why, one step back1617Simple 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'])34Documentation fixesKevin Yapbefore adoption67523af
lines 35–36why, one step back35Arguments cannot be documented this way. This is to follow the general36convention of Unix tools of using arguments for only the most necessaryMerge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326
lines 37–42why, one step back37things and to document them in the introduction text by referring to them38by name.3940Preventing Rewrapping41---------------------42Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 43why, one step back43The default behavior of Click is to rewrap text based on the width of theMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 44–45why, one step back44terminal. In some circumstances, this can become a problem. The main issue45is when showing code examples, where newlines are significant.Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326
lines 46–131why, one step back4647Rewrapping 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')Added an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
lines 132–138why, one step back132133134Help Parameter Customization135----------------------------136137.. versionadded:: 2.0138Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
lines 139–140why, one step back139The help parameter is implemented in Click in a very special manner.140Unlike regular parameters it's automatically added by Click for anyAdded an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
line 141why, one step back141command and it performs automatic conflict resolution. By default it'sMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 142–143why, one step back142called ``--help``, but this can be changed. If a command itself implements143a parameter with the same name, the default help parameter stops acceptingAdded an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
lines 144–147why, one step back144it. 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``Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 148why, one step back148instead of just ``--help``:Added an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
lines 149–151why, one step back149150.. click:example::151defaults -> settingsArmin Ronacherbefore adoption1bc0a19
line 152why, one step back152 CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])Added an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
line 153why, one step back153defaults -> settingsArmin Ronacherbefore adoption1bc0a19
line 154why, one step back154 @click.command(context_settings=CONTEXT_SETTINGS)Added an example for help parameter customization.Armin Ronacherbefore adoption6e98e9b
lines 155–162why, one step back155 def cli():156 pass157158And what it looks like:159160.. click:run::161162 invoke(cli, ['-h'])