Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

128 lines · 3.3 KiB

Last changed by

Merge pull request #70 from julen/doc-fixesArmin Ronacherbefore adoption24b35b9

History of this file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–13
1Documenting Scripts
2===================34Click makes it very easy to document your command line tools. First of5all it automatically generates help pages for you. While these are6currently not customizable in layout, all the texts can be changed.78Help Texts9----------1011Commands and options accept help arguments. In case of commands the doc12string of the function is automatically used if provided.13

More automated docsArmin Ronacherbefore adoption8254b79

line 14why, one step back
14Simple example:

Initial commitArmin Ronacherbefore adoption4101de3

line 15
15

More automated docsArmin Ronacherbefore adoption8254b79

lines 16–17why, one step back
16.. click:example::
17

Initial commitArmin Ronacherbefore adoption4101de3

lines 18–23
18 @click.command()
19 @click.option('--count', default=1, help='number of greetings')20 @click.argument('name')21 def hello(count, name):22 """This script prints hello NAME COUNT times."""23 for x in range(count):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 24why, one step back
24 click.echo('Hello %s!' % name)

Initial commitArmin Ronacherbefore adoption4101de3

line 25
25

More automated docsArmin Ronacherbefore adoption8254b79

line 26why, one step back
26And what it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 27
27

More automated docsArmin Ronacherbefore adoption8254b79

line 28why, one step back
28.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 29
29

More automated docsArmin Ronacherbefore adoption8254b79

line 30why, one step back
30 invoke(hello, args=['--help'])

Initial commitArmin Ronacherbefore adoption4101de3

lines 31–36
31
32Arguments cannot be documented this way. This is to follow general33convention of Unix tools to use arguments only for the most necessary34things and to document them in the introduction text by referring to them35by name.36

Added support for explicit terminal widths and disabling of rewrappingArmin Ronacherbefore adoption24d400f

lines 37–44why, one step back
37Preventing Rewrapping
38---------------------3940The default behavior of click is to rewrap text to work correctly for the41width of the terminal. In some circumstances this can become a problem.42The main issue is showing code examples where newlines are significant.4344Rewrapping can be disabled on a per-paragraph case by adding a line with

Merge pull request #70 from julen/doc-fixesArmin Ronacherbefore adoption24b35b9

lines 45–46why, one step back
45just the ``\b`` escape marker in it. This line will be removed from the
46help text and rewrapping will be disabled.

Added support for explicit terminal widths and disabling of rewrappingArmin Ronacherbefore adoption24d400f

lines 47–76why, one step back
47
48Example:4950.. click:example::5152 @click.command()53 def cli():54 """First paragraph.5556 This is a very long second paragraph and as you57 can see wrapped very early in the source text58 but will be rewrapped to the terminal width in59 the final output.6061 \b62 This is63 a paragraph64 without rewrapping.6566 And this is a paragraph67 that will be rewrapped again.68 """6970And what it looks like:7172.. click:run::7374 invoke(cli, args=['--help'])7576

Initial commitArmin Ronacherbefore adoption4101de3

lines 77–82
77Meta Vars
78---------7980Options and parameters accept a ``metavar`` argument that can change the81meta variable in the help page. The default version is the parameter name82in uppercase with underscores and sometimes annotated differently if

More automated docsArmin Ronacherbefore adoption8254b79

line 83why, one step back
83optional. This can be customized at all levels:

Initial commitArmin Ronacherbefore adoption4101de3

line 84
84

More automated docsArmin Ronacherbefore adoption8254b79

lines 85–86why, one step back
85.. click:example::
86

Initial commitArmin Ronacherbefore adoption4101de3

lines 87–93
87 @click.command(options_metavar='<options>')
88 @click.option('--count', default=1, help='number of greetings',89 metavar='<int>')90 @click.argument('name', metavar='<name>')91 def hello(count, name):92 """This script prints hello <name> <int> times."""93 for x in range(count):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 94why, one step back
94 click.echo('Hello %s!' % name)

Initial commitArmin Ronacherbefore adoption4101de3

line 95
95

More automated docsArmin Ronacherbefore adoption8254b79

line 96why, one step back
96Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 97
97

More automated docsArmin Ronacherbefore adoption8254b79

line 98why, one step back
98.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 99
99

More automated docsArmin Ronacherbefore adoption8254b79

line 100why, one step back
100 invoke(hello, args=['--help'])

Initial commitArmin Ronacherbefore adoption4101de3

lines 101–107
101
102103Command Short Help104------------------105106For commands a short help is generated. By default it's the first part107(until the first dot) of the help message of the command unless it's too

More automated docsArmin Ronacherbefore adoption8254b79

line 108why, one step back
108long. This can also be overridden:

Initial commitArmin Ronacherbefore adoption4101de3

line 109
109

More automated docsArmin Ronacherbefore adoption8254b79

lines 110–111why, one step back
110.. click:example::
111

Initial commitArmin Ronacherbefore adoption4101de3

lines 112–123
112 @click.group()
113 def cli():114 """A simple command line tool."""115116 @cli.command('init', short_help='init the repo')117 def init():118 """Initializes the repository."""119120 @cli.command('delete', short_help='delete the repo')121 def delete():122 """Deletes the repository."""123

More automated docsArmin Ronacherbefore adoption8254b79

line 124why, one step back
124And what it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 125
125

More automated docsArmin Ronacherbefore adoption8254b79

line 126why, one step back
126.. click:run::

Initial commitArmin Ronacherbefore adoption4101de3

line 127
127

More automated docsArmin Ronacherbefore adoption8254b79

line 128why, one step back
128 invoke(cli, prog_name='repo.py')