Skip to content
Myr

quickstart.rst

This file

Path

docs/quickstart.rst

129 lines · 3.1 KiB

Last changed by

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

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–26
23 import click
2425 @click.command()26 def hello():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 27why, one step back
27 click.echo('Hello World!')

Initial commitArmin Ronacherbefore adoption4101de3

lines 28–34
28
29What'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

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

lines 47–63why, one step back
47Echoing
48-------4950Why does this example use :func:`echo` instead of the regular51:func:`print` function? The answer to this question is that click52attempts to support both Python 2 and Python 3 the same way and to be very53robust even when the environment is misconfigured. Click wants to be54functional at least on a basic level even if everything is completely55broken.5657What this means is that the :func:`echo` function applies some error58correction in case the terminal is misconfigured instead of dying with an59:exc:`UnicodeError`.6061If you don't need this, you can also use the `print()` construct /62function.63

Initial commitArmin Ronacherbefore adoption4101de3

lines 64–68
64Nesting Commands
65----------------6667Commands can be attached to other commands of type :class:`Group`. This68allows 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 69why, one step back
69implements two commands for managing databases:

Initial commitArmin Ronacherbefore adoption4101de3

line 70
70

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 72–78
72
73 @click.group()74 def cli():75 pass7677 @click.command()78 def initdb():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 79why, one step back
79 click.echo('Initialized the database')

Initial commitArmin Ronacherbefore adoption4101de3

lines 80–82
80
81 @click.command()82 def dropdb():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 83why, one step back
83 click.echo('Dropped the database')

Initial commitArmin Ronacherbefore adoption4101de3

lines 84–94
84
85 cli.add_command(initdb)86 cli.add_command(dropdb)8788As you can see the :func:`group` decorator works like the :func:`command`89decorator but creates a :class:`Group` object instead which can be given90multiple subcommands that can be attached with91:meth:`Group.add_command`.9293For simple scripts it's also possible to automatically attach and create a94command by using the :meth:`Group.command` decorator instead. The above

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 96
96

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 98–104
98
99 @click.group()100 def cli():101 pass102103 @cli.command()104 def initdb():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 105why, one step back
105 click.echo('Initialized the database')

Initial commitArmin Ronacherbefore adoption4101de3

lines 106–108
106
107 @cli.command()108 def dropdb():

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

line 109why, one step back
109 click.echo('Dropped the database')

Initial commitArmin Ronacherbefore adoption4101de3

lines 110–113
110
111Adding Parameters112-----------------113

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 115
115

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

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

Initial commitArmin Ronacherbefore adoption4101de3

lines 117–122
117
118 @click.command()119 @click.option('--count', default=1, help='number of greetings')120 @click.argument('name')121 def hello(count, name):122 for x in range(count):

Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3

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

Initial commitArmin Ronacherbefore adoption4101de3

line 124
124

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 126
126

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

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

Initial commitArmin Ronacherbefore adoption4101de3

line 128
128

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

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