Skip to content
Myr

quickstart.md

This file

Path

docs/quickstart.md

196 lines · 6.8 KiB · Markdown

Last changed by

remove redundant click. module linkJaron Rosenaubefore adoption4b695e5

History of this file

Take it

convert from reST to MySTdenuowebbefore adoption38d9d60

line 1why, one step back
1# Quickstart

change ext from .rst to .mddenuowebbefore adoption11384eb

line 2why, one step back
2

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 3–4why, one step back
3```{currentmodule} click
4```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 5why, one step back
5

convert from reST to MySTdenuowebbefore adoption38d9d60

line 6why, one step back
6## Install

change ext from .rst to .mddenuowebbefore adoption11384eb

line 7why, one step back
7

convert from reST to MySTdenuowebbefore adoption38d9d60

line 8why, one step back
8Install from PyPI:

change ext from .rst to .mddenuowebbefore adoption11384eb

line 9why, one step back
9

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 10–12why, one step back
10```console
11pip install click12```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 13why, one step back
13

convert from reST to MySTdenuowebbefore adoption38d9d60

line 14why, one step back
14Installing into a virtual environment is highly recommended. We suggest {ref}`virtualenv-heading`.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 15why, one step back
15

convert from reST to MySTdenuowebbefore adoption38d9d60

line 16why, one step back
16## Examples

change ext from .rst to .mddenuowebbefore adoption11384eb

line 17why, one step back
17

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 18–19why, one step back
18Some standalone examples of Click applications are packaged with Click. They are available in the
19[examples folder](https://github.com/pallets/click/tree/main/examples) of the repo.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 20why, one step back
20

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 21–38why, one step back
21- [inout](https://github.com/pallets/click/tree/main/examples/inout) : A very simple example of an application that can
22 read from files and write to files and also accept input from stdin or write to stdout.23- [validation](https://github.com/pallets/click/tree/main/examples/validation) : A simple example of an application that24 performs custom validation of parameters in different ways.25- [naval](https://github.com/pallets/click/tree/main/examples/naval) : Port of the [docopt](http://docopt.org/) naval26 example.27- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text. Uses28 colorama on Windows.29- [aliases](https://github.com/pallets/click/tree/main/examples/aliases) : An advanced example that implements30 {ref}`aliases`.31- [imagepipe](https://github.com/pallets/click/tree/main/examples/imagepipe) : A complex example that implements some32 {ref}`command-pipelines` . It chains together image processing instructions. Requires pillow.33- [repo](https://github.com/pallets/click/tree/main/examples/repo) : An advanced example that implements a34 Git-/Mercurial-like command line interface.35- [complex](https://github.com/pallets/click/tree/main/examples/complex) : A very advanced example that implements36 loading subcommands dynamically from a plugin folder.37- [termui](https://github.com/pallets/click/tree/main/examples/termui) : A simple example that showcases terminal UI38 helpers provided by click.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 39why, one step back
39

convert from reST to MySTdenuowebbefore adoption38d9d60

line 40why, one step back
40## Basic Concepts - Creating a Command

change ext from .rst to .mddenuowebbefore adoption11384eb

line 41why, one step back
41

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 42–43why, one step back
42Click is based on declaring commands through decorators. Internally, there is a non-decorator interface for advanced use
43cases, but it's discouraged for high-level usage.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 44why, one step back
44

remove redundant click. module linkJaron Rosenaubefore adoption4b695e5

line 45why, one step back
45A function becomes a Click command line tool by decorating it through {func}`command`. At its simplest, just

convert from reST to MySTdenuowebbefore adoption38d9d60

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

change ext from .rst to .mddenuowebbefore adoption11384eb

line 47why, one step back
47

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 48–50why, one step back
48
49```{eval-rst}50.. click:example::

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 51–56why, one step back
51 import click
5253 @click.command()54 def hello():55 click.echo('Hello World!')56

convert from reST to MySTdenuowebbefore adoption38d9d60

line 57why, one step back
57What's happening is that the decorator converts the function into a :class:`Command` which then can be invoked:

change ext from .rst to .mddenuowebbefore adoption11384eb

line 58why, one step back
58

convert from reST to MySTdenuowebbefore adoption38d9d60

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

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 60–71why, one step back
60 if __name__ == '__main__':
61 hello()6263And what it looks like:6465.. click:run::66 invoke(hello, args=[], prog_name='python hello.py')6768And the corresponding help page:6970.. click:run::71 invoke(hello, args=['--help'], prog_name='python hello.py')

convert from reST to MySTdenuowebbefore adoption38d9d60

line 72why, one step back
72```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 73why, one step back
73

convert from reST to MySTdenuowebbefore adoption38d9d60

line 74why, one step back
74## Echoing

change ext from .rst to .mddenuowebbefore adoption11384eb

line 75why, one step back
75

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 76–78why, one step back
76Why does this example use {func}`echo` instead of the regular {func}`print` function? The answer to this question is
77that Click attempts to support different environments consistently and to be very robust even when the environment is78misconfigured. Click wants to be functional at least on a basic level even if everything is completely broken.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 79why, one step back
79

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 80–81why, one step back
80What this means is that the {func}`echo` function applies some error correction in case the terminal is misconfigured
81instead of dying with a {exc}`UnicodeError`.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 82why, one step back
82

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 83–84why, one step back
83The echo function also supports color and other styles in output. It will automatically remove styles if the output
84stream is a file. On Windows, colorama is automatically installed and used. See {ref}`ansi-colors`.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 85why, one step back
85

convert from reST to MySTdenuowebbefore adoption38d9d60

line 86why, one step back
86If you don't need this, you can also use the `print()` construct / function.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 87why, one step back
87

convert from reST to MySTdenuowebbefore adoption38d9d60

line 88why, one step back
88## Nesting Commands

change ext from .rst to .mddenuowebbefore adoption11384eb

line 89why, one step back
89

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 90–91why, one step back
90Commands can be attached to other commands of type {class}`Group`. This allows arbitrary nesting of scripts. As an
91example here is a script that implements two commands for managing databases:

change ext from .rst to .mddenuowebbefore adoption11384eb

line 92why, one step back
92

convert from reST to MySTdenuowebbefore adoption38d9d60

line 93why, one step back
93```{eval-rst}

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 94–108why, one step back
94.. click:example::
95 @click.group()96 def cli():97 pass9899 @click.command()100 def initdb():101 click.echo('Initialized the database')102103 @click.command()104 def dropdb():105 click.echo('Dropped the database')106107 cli.add_command(initdb)108 cli.add_command(dropdb)

convert from reST to MySTdenuowebbefore adoption38d9d60

line 109why, one step back
109```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 110why, one step back
110

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 111–112why, one step back
111As you can see, the {func}`group` decorator works like the {func}`command` decorator, but creates a {class}`Group`
112object instead which can be given multiple subcommands that can be attached with {meth}`Group.add_command`.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 113why, one step back
113

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 114–115why, one step back
114For simple scripts, it's also possible to automatically attach and create a command by using the {meth}`Group.command`
115decorator instead. The above script can instead be written like this:

change ext from .rst to .mddenuowebbefore adoption11384eb

line 116why, one step back
116

convert from reST to MySTdenuowebbefore adoption38d9d60

line 117why, one step back
117```{eval-rst}

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 118–130why, one step back
118.. click:example::
119 @click.group()120 def cli():121 pass122123 @cli.command()124 def initdb():125 click.echo('Initialized the database')126127 @cli.command()128 def dropdb():129 click.echo('Dropped the database')130

convert from reST to MySTdenuowebbefore adoption38d9d60

line 131why, one step back
131You would then invoke the :class:`Group` in your entry points or other invocations:

change ext from .rst to .mddenuowebbefore adoption11384eb

line 132why, one step back
132

convert from reST to MySTdenuowebbefore adoption38d9d60

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

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 134–135why, one step back
134 if __name__ == '__main__':
135 cli()

convert from reST to MySTdenuowebbefore adoption38d9d60

line 136why, one step back
136```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 137why, one step back
137

convert from reST to MySTdenuowebbefore adoption38d9d60

line 138why, one step back
138## Registering Commands Later

change ext from .rst to .mddenuowebbefore adoption11384eb

line 139why, one step back
139

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 140–142why, one step back
140Instead of using the `@group.command()` decorator, commands can be decorated with the plain `@command()` decorator
141and registered with a group later with `group.add_command()`. This could be used to split commands into multiple Python142modules.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 143why, one step back
143

convert from reST to MySTdenuowebbefore adoption38d9d60

line 144why, one step back
144```{code-block} python

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 145–147why, one step back
145 @click.command()
146 def greet():147 click.echo("Hello, World!")

convert from reST to MySTdenuowebbefore adoption38d9d60

line 148why, one step back
148```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 149why, one step back
149

convert from reST to MySTdenuowebbefore adoption38d9d60

line 150why, one step back
150```{code-block} python

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 151–155why, one step back
151 @click.group()
152 def group():153 pass154155 group.add_command(greet)

convert from reST to MySTdenuowebbefore adoption38d9d60

line 156why, one step back
156```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 157why, one step back
157

convert from reST to MySTdenuowebbefore adoption38d9d60

line 158why, one step back
158## Adding Parameters

change ext from .rst to .mddenuowebbefore adoption11384eb

line 159why, one step back
159

convert from reST to MySTdenuowebbefore adoption38d9d60

line 160why, one step back
160To add parameters, use the {func}`option` and {func}`argument` decorators:

change ext from .rst to .mddenuowebbefore adoption11384eb

line 161why, one step back
161

convert from reST to MySTdenuowebbefore adoption38d9d60

line 162why, one step back
162```{eval-rst}

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 163–174why, one step back
163.. click:example::
164 @click.command()165 @click.option('--count', default=1, help='number of greetings')166 @click.argument('name')167 def hello(count, name):168 for x in range(count):169 click.echo(f"Hello {name}!")170171What it looks like:172173.. click:run::174 invoke(hello, args=['--help'], prog_name='python hello.py')

convert from reST to MySTdenuowebbefore adoption38d9d60

line 175why, one step back
175```

change ext from .rst to .mddenuowebbefore adoption11384eb

line 176why, one step back
176

convert from reST to MySTdenuowebbefore adoption38d9d60

line 177why, one step back
177## Switching to Entry Points

change ext from .rst to .mddenuowebbefore adoption11384eb

line 178why, one step back
178

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 179–180why, one step back
179In the code you wrote so far there is a block at the end of the file which looks like this:
180`if __name__ == '__main__':`. This is traditionally how a standalone Python file looks like. With Click you can continue

change ext from .rst to .mddenuowebbefore adoption11384eb

lines 181–184why, one step back
181doing that, but a better way is to package your app with an entry point.
182183There are two main (and many more) reasons for this:184

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 185–186why, one step back
185The first one is that installers automatically generate executable wrappers for Windows so your command line utilities
186work on Windows too.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 187why, one step back
187

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 188–190why, one step back
188The second reason is that entry point scripts work with virtualenv on Unix without the virtualenv having to be
189activated. This is a very useful concept which allows you to bundle your scripts with all requirements into a190virtualenv.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 191why, one step back
191

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 192–193why, one step back
192Click is perfectly equipped to work with that and in fact the rest of the documentation will assume that you are writing
193applications as distributed packages.

change ext from .rst to .mddenuowebbefore adoption11384eb

line 194why, one step back
194

convert from reST to MySTdenuowebbefore adoption38d9d60

lines 195–196why, one step back
195Look at the {doc}`entry-points` chapter before reading the rest as the examples assume that you will be using entry
196points.