Skip to content
Myr

quickstart.rst

This file

Path

docs/quickstart.rst

112 lines · 2.5 KiB

Last changed by

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

History of this file

Take it

Initial commitArmin Ronacherbefore adoption4101de3

lines 1–18
1Quickstart
2==========34.. currentmodule:: click56You can get the library directly from PyPI::78 pip install click910Basic Concepts11--------------1213Click is based on declaring commands through decorators. Internally there14is a non-decorator interface for advanced use cases but it's discouraged15for high-level usage.1617A function becomes a click command line tool by decorating it through18:func:`click.command`. In the most simple version just decorating a

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 19why, one step back
19function with this decorator will make it into a callable script:

Initial commitArmin Ronacherbefore adoption4101de3

line 20
20

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 21–22why, one step back
21.. click:example::
22

Initial commitArmin Ronacherbefore adoption4101de3

lines 23–34
23 import click
2425 @click.command()26 def hello():27 print('Hello World!')2829What's happening is that the decorator converts the function into a30:class:`Command` which then can be invoked::3132 if __name__ == '__main__':33 hello()34

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

lines 35–46why, one step back
35And what it looks like:
3637.. click:run::3839 invoke(hello, args=[])4041And the corresponding help page:4243.. click:run::4445 invoke(hello, args=['--help'])46

Initial commitArmin Ronacherbefore adoption4101de3

lines 47–51
47Nesting Commands
48----------------4950Commands can be attached to other commands of type :class:`Group`. This51allows arbitrary nesting of scripts. As an example here is a script that

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 52why, one step back
52implements two commands for managing databases:

Initial commitArmin Ronacherbefore adoption4101de3

line 53
53

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 54why, one step back
54.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 55–65
55
56 @click.group()57 def cli():58 pass5960 @click.command()61 def initdb():62 print('Initialized the database')6364 @click.command()65 def dropdb():

Merge pull request #2 from jodal/patch-1Armin Ronacherbefore adoption3eba021

line 66why, one step back
66 print('Dropped the database')

Initial commitArmin Ronacherbefore adoption4101de3

lines 67–77
67
68 cli.add_command(initdb)69 cli.add_command(dropdb)7071As you can see the :func:`group` decorator works like the :func:`command`72decorator but creates a :class:`Group` object instead which can be given73multiple subcommands that can be attached with74:meth:`Group.add_command`.7576For simple scripts it's also possible to automatically attach and create a77command by using the :meth:`Group.command` decorator instead. The above

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 78why, one step back
78script can be written like this then:

Initial commitArmin Ronacherbefore adoption4101de3

line 79
79

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 80why, one step back
80.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 81–91
81
82 @click.group()83 def cli():84 pass8586 @cli.command()87 def initdb():88 print('Initialized the database')8990 @cli.command()91 def dropdb():

Merge pull request #2 from jodal/patch-1Armin Ronacherbefore adoption3eba021

line 92why, one step back
92 print('Dropped the database')

Initial commitArmin Ronacherbefore adoption4101de3

lines 93–96
93
94Adding Parameters95-----------------96

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 97why, one step back
97To add parameters the :func:`option` and :func:`argument` decorators:

Initial commitArmin Ronacherbefore adoption4101de3

line 98
98

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 99why, one step back
99.. click:example::

Initial commitArmin Ronacherbefore adoption4101de3

lines 100–107
100
101 @click.command()102 @click.option('--count', default=1, help='number of greetings')103 @click.argument('name')104 def hello(count, name):105 for x in range(count):106 print('Hello %s!' % name)107

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

line 108why, one step back
108What it looks like:

Initial commitArmin Ronacherbefore adoption4101de3

line 109
109

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

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

Initial commitArmin Ronacherbefore adoption4101de3

line 111
111

Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670

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