Skip to content
Myr

quickstart.rst

This file

Path

docs/quickstart.rst

165 lines · 4.9 KiB

Last changed by

Added imagepipe example to docsArmin Ronacherbefore adoption847fcbc

History of this file

Take it

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

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 imagepipe example to docsArmin Ronacherbefore adoption847fcbc

lines 39–40why, one step back
39* ``imagepipe``: `Multi command chaining demo
40 <https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`_

Added link to screencastArmin Ronacherbefore adoption9716618

line 41why, one step back
41

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 42–44why, one step back
42Basic Concepts
43--------------44

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

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 47–48why, one step back
47for high-level usage.
48

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

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

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

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 52–82why, one step back
52
53.. click:example::5455 import click5657 @click.command()58 def hello():59 click.echo('Hello World!')6061What's happening is that the decorator converts the function into a62:class:`Command` which then can be invoked::6364 if __name__ == '__main__':65 hello()6667And what it looks like:6869.. click:run::7071 invoke(hello, args=[])7273And the corresponding help page:7475.. click:run::7677 invoke(hello, args=['--help'])7879Echoing80-------8182Why does this example use :func:`echo` instead of the regular

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

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 84–92why, one step back
84attempts to support both Python 2 and Python 3 the same way and to be very
85robust even when the environment is misconfigured. Click wants to be86functional at least on a basic level even if everything is completely87broken.8889What this means is that the :func:`echo` function applies some error90correction in case the terminal is misconfigured instead of dying with an91:exc:`UnicodeError`.92

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

line 93why, one step back
93As 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 94–96why, one step back
94has good support for ANSI colors. It will automatically strip ANSI codes
95if the output stream is a file and if colorama is supported, ANSI colors96will also work on Windows. See :ref:`ansi-colors` for more information.

Cross link for ansi colors.Armin Ronacherbefore adoptionc5d53f8

line 97why, one step back
97

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 98–124why, one step back
98If you don't need this, you can also use the `print()` construct /
99function.100101Nesting Commands102----------------103104Commands can be attached to other commands of type :class:`Group`. This105allows arbitrary nesting of scripts. As an example here is a script that106implements two commands for managing databases:107108.. click:example::109110 @click.group()111 def cli():112 pass113114 @click.command()115 def initdb():116 click.echo('Initialized the database')117118 @click.command()119 def dropdb():120 click.echo('Dropped the database')121122 cli.add_command(initdb)123 cli.add_command(dropdb)124

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

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

line 128why, one step back
128

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

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

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

Documentation fixesKevin Yapbefore adoption67523af

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 132–149why, one step back
132
133.. click:example::134135 @click.group()136 def cli():137 pass138139 @cli.command()140 def initdb():141 click.echo('Initialized the database')142143 @cli.command()144 def dropdb():145 click.echo('Dropped the database')146147Adding Parameters148-----------------149

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

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

Merge pull request #78 from julen/doc-fixesArmin Ronacherbefore adoption147bd4b

lines 151–165why, one step back
151
152.. click:example::153154 @click.command()155 @click.option('--count', default=1, help='number of greetings')156 @click.argument('name')157 def hello(count, name):158 for x in range(count):159 click.echo('Hello %s!' % name)160161What it looks like:162163.. click:run::164165 invoke(hello, args=['--help'])