Skip to content
Myr

documentation.rst

This file

Path

docs/documentation.rst

98 lines · 2.7 KiB

Last changed by

Initial commitArmin Ronacherbefore adoption4101de3

History of this file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–98
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.1314Simple example::1516 @click.command()17 @click.option('--count', default=1, help='number of greetings')18 @click.argument('name')19 def hello(count, name):20 """This script prints hello NAME COUNT times."""21 for x in range(count):22 print('Hello %s!' % name)2324And what it looks like::2526 $ python hello.py --help27 Usage: hello.py [OPTIONS] NAME2829 This script prints hello NAME COUNT times.3031 Options:32 --count=COUNT number of greetings33 --help Show this message and exit.3435Arguments cannot be documented this way. This is to follow general36convention of Unix tools to use arguments only for the most necessary37things and to document them in the introduction text by referring to them38by name.3940Meta Vars41---------4243Options and parameters accept a ``metavar`` argument that can change the44meta variable in the help page. The default version is the parameter name45in uppercase with underscores and sometimes annotated differently if46optional. This can be customized at all levels::4748 @click.command(options_metavar='<options>')49 @click.option('--count', default=1, help='number of greetings',50 metavar='<int>')51 @click.argument('name', metavar='<name>')52 def hello(count, name):53 """This script prints hello <name> <int> times."""54 for x in range(count):55 print('Hello %s!' % name)5657Example::5859 Usage: hello.py <options> <name>6061 This script prints hello <name> <int> times.6263 Options:64 --count=<int> number of greetings65 --help Show this message and exit.66 6768Command Short Help69------------------7071For commands a short help is generated. By default it's the first part72(until the first dot) of the help message of the command unless it's too73long. This can also be overridden::7475 @click.group()76 def cli():77 """A simple command line tool."""7879 @cli.command('init', short_help='init the repo')80 def init():81 """Initializes the repository."""8283 @cli.command('delete', short_help='delete the repo')84 def delete():85 """Deletes the repository."""8687And what it looks like::8889 Usage: repo.py [OPTIONS] COMMAND [ARGS]...9091 A simple command line tool.9293 Commands:94 delete delete the repo95 init init the repo9697 Options:98 --help Show this message and exit.