Skip to content
Myr

quickstart.rst

This file

Path

docs/quickstart.rst

163 lines · 4.7 KiB

Last changed by

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

History of this file

Take it

The walk stopped after 200 commits: lines older than d227e5e point at it.

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 1–9why, one step back
1Quickstart
2==========34.. currentmodule:: click56You can get the library directly from PyPI::78 pip install click9

Linked examples from documentationArmin Ronacherbefore adoption2bab892

lines 10–11why, one step back
10Screencast and Examples
11-----------------------

Added link to screencastArmin Ronacherbefore adoption9716618

line 12why, one step back
12

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 13why, one step back
13There is a screencast available which shows the basic API of Click and

Added link to screencastArmin Ronacherbefore adoption9716618

lines 14–18why, one step back
14how to build simple applications with it. It also explores how to build
15commands with subcommands.1617* `Building Command Line Applications with Click18 <https://www.youtube.com/watch?v=kNke39OZ2k0>`_

Linked examples from documentationArmin Ronacherbefore adoption2bab892

line 19why, one step back
19

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 20why, one step back
20Examples of Click applications can be found in the documentation as well

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 21why, one step back
21as in the GitHub repository together with readme files:

Linked examples from documentationArmin Ronacherbefore adoption2bab892

lines 22–28why, one step back
22
23* ``inout``: `File input and output24 <https://github.com/mitsuhiko/click/tree/master/examples/inout>`_25* ``naval``: `Port of docopt naval example26 <https://github.com/mitsuhiko/click/tree/master/examples/naval>`_27* ``aliases``: `Command alias example28 <https://github.com/mitsuhiko/click/tree/master/examples/aliases>`_

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 29why, one step back
29* ``repo``: `Git-/Mercurial-like command line interface

Linked examples from documentationArmin Ronacherbefore adoption2bab892

lines 30–32why, one step back
30 <https://github.com/mitsuhiko/click/tree/master/examples/repo>`_
31* ``complex``: `Complex example with plugin loading32 <https://github.com/mitsuhiko/click/tree/master/examples/complex>`_

Added validation example to docs.Armin Ronacherbefore adoption438b945

lines 33–34why, one step back
33* ``validation``: `Custom parameter validation example
34 <https://github.com/mitsuhiko/click/tree/master/examples/validation>`_

Added a colorama example.Armin Ronacherbefore adoption6e31b1c

lines 35–36why, one step back
35* ``colors``: `Colorama ANSI color support
36 <https://github.com/mitsuhiko/click/tree/master/examples/colors>`_

Linked to termui exampleArmin Ronacherbefore adoptione539622

lines 37–38why, one step back
37* ``termui``: `Terminal UI functions demo
38 <https://github.com/mitsuhiko/click/tree/master/examples/termui>`_

Added link to screencastArmin Ronacherbefore adoption9716618

line 39why, one step back
39

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 40–42why, one step back
40Basic Concepts
41--------------42

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

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

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 45–46why, one step back
45for high-level usage.
46

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 47why, one step back
47A function becomes a Click command line tool by decorating it through

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 48–49why, one step back
48:func:`click.command`. At its simplest, just decorating a function
49with this decorator will make it into a callable script:

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 50–80why, one step back
50
51.. click:example::5253 import click5455 @click.command()56 def hello():57 click.echo('Hello World!')5859What's happening is that the decorator converts the function into a60:class:`Command` which then can be invoked::6162 if __name__ == '__main__':63 hello()6465And what it looks like:6667.. click:run::6869 invoke(hello, args=[])7071And the corresponding help page:7273.. click:run::7475 invoke(hello, args=['--help'])7677Echoing78-------7980Why does this example use :func:`echo` instead of the regular

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 81why, one step back
81:func:`print` function? The answer to this question is that Click

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 82–90why, one step back
82attempts to support both Python 2 and Python 3 the same way and to be very
83robust even when the environment is misconfigured. Click wants to be84functional at least on a basic level even if everything is completely85broken.8687What this means is that the :func:`echo` function applies some error88correction in case the terminal is misconfigured instead of dying with an89:exc:`UnicodeError`.90

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 91why, one step back
91As an added benefit, starting with Click 2.0, the echo function also

Added style helpers and added deeper ansi color support. Colorama now optional.Armin Ronacherbefore adoptionf516eee

lines 92–94why, one step back
92has good support for ANSI colors. It will automatically strip ANSI codes
93if the output stream is a file and if colorama is supported, ANSI colors94will also work on Windows. See :ref:`ansi-colors` for more information.

Cross link for ansi colors.Armin Ronacherbefore adoptionc5d53f8

line 95why, one step back
95

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 96–122why, one step back
96If you don't need this, you can also use the `print()` construct /
97function.9899Nesting Commands100----------------101102Commands can be attached to other commands of type :class:`Group`. This103allows arbitrary nesting of scripts. As an example here is a script that104implements two commands for managing databases:105106.. click:example::107108 @click.group()109 def cli():110 pass111112 @click.command()113 def initdb():114 click.echo('Initialized the database')115116 @click.command()117 def dropdb():118 click.echo('Dropped the database')119120 cli.add_command(initdb)121 cli.add_command(dropdb)122

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 123–125why, one step back
123As you can see, the :func:`group` decorator works like the :func:`command`
124decorator, but creates a :class:`Group` object instead which can be given125multiple subcommands that can be attached with :meth:`Group.add_command`.

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

line 126why, one step back
126

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 127why, one step back
127For simple scripts, it's also possible to automatically attach and create a

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

line 128why, one step back
128command by using the :meth:`Group.command` decorator instead. The above

Documentation fixesKevin Yapbefore adoption67523af

line 129why, one step back
129script can instead be written like this:

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 130–147why, one step back
130
131.. click:example::132133 @click.group()134 def cli():135 pass136137 @cli.command()138 def initdb():139 click.echo('Initialized the database')140141 @cli.command()142 def dropdb():143 click.echo('Dropped the database')144145Adding Parameters146-----------------147

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

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

Delay resolve versionArmin Ronacherbefore adoptiond227e5e

lines 149–163why, one step back
149
150.. click:example::151152 @click.command()153 @click.option('--count', default=1, help='number of greetings')154 @click.argument('name')155 def hello(count, name):156 for x in range(count):157 click.echo('Hello %s!' % name)158159What it looks like:160161.. click:run::162163 invoke(hello, args=['--help'])