Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

88 lines · 2.3 KiB

Last changed by

More automated docsArmin Ronacherbefore adoption8254b79

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–25
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):24 print('Hello %s!' % name)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–42
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.3637Meta Vars38---------3940Options and parameters accept a ``metavar`` argument that can change the41meta variable in the help page. The default version is the parameter name42in uppercase with underscores and sometimes annotated differently if

More automated docsArmin Ronacherbefore adoption8254b79

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

Initial commitArmin Ronacherbefore adoption4101de3

line 44
44

More automated docsArmin Ronacherbefore adoption8254b79

lines 45–46why, one step back
45.. click:example::
46

Initial commitArmin Ronacherbefore adoption4101de3

lines 47–55
47 @click.command(options_metavar='<options>')
48 @click.option('--count', default=1, help='number of greetings',49 metavar='<int>')50 @click.argument('name', metavar='<name>')51 def hello(count, name):52 """This script prints hello <name> <int> times."""53 for x in range(count):54 print('Hello %s!' % name)55

More automated docsArmin Ronacherbefore adoption8254b79

line 56why, one step back
56Example:

Initial commitArmin Ronacherbefore adoption4101de3

line 57
57

More automated docsArmin Ronacherbefore adoption8254b79

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

Initial commitArmin Ronacherbefore adoption4101de3

line 59
59

More automated docsArmin Ronacherbefore adoption8254b79

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 61–67
61
6263Command Short Help64------------------6566For commands a short help is generated. By default it's the first part67(until the first dot) of the help message of the command unless it's too

More automated docsArmin Ronacherbefore adoption8254b79

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

Initial commitArmin Ronacherbefore adoption4101de3

line 69
69

More automated docsArmin Ronacherbefore adoption8254b79

lines 70–71why, one step back
70.. click:example::
71

Initial commitArmin Ronacherbefore adoption4101de3

lines 72–83
72 @click.group()
73 def cli():74 """A simple command line tool."""7576 @cli.command('init', short_help='init the repo')77 def init():78 """Initializes the repository."""7980 @cli.command('delete', short_help='delete the repo')81 def delete():82 """Deletes the repository."""83

More automated docsArmin Ronacherbefore adoption8254b79

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

Initial commitArmin Ronacherbefore adoption4101de3

line 85
85

More automated docsArmin Ronacherbefore adoption8254b79

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

Initial commitArmin Ronacherbefore adoption4101de3

line 87
87

More automated docsArmin Ronacherbefore adoption8254b79

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