Skip to content
Myr

quickstart.rst

This file

Path

docs/quickstart.rst

206 lines · 6.9 KiB

Last changed by

Merge stable into main (#2799)Rowlando13before adoptiond791537

History of this file

Take it

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

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 1–5why, one step back
1Quickstart
2==========34.. currentmodule:: click5

Merge stable into main (#2799)Rowlando13before adoptiond791537

lines 6–8why, one step back
6Install
7----------------------8Install from PyPI::

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 9–11why, one step back
9
10 pip install click11

Merge stable into main (#2799)Rowlando13before adoptiond791537

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

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

line 13why, one step back
13

Merge stable into main (#2799)Rowlando13before adoptiond791537

line 14why, one step back
14Examples

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 15–16why, one step back
15-----------------------
16

Merge stable into main (#2799)Rowlando13before adoptiond791537

line 17why, one step back
17Some standalone examples of Click applications are packaged with Click. They are available in the `examples folder <https://github.com/pallets/click/tree/main/examples>`_ of the repo.

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

line 18why, one step back
18

Merge stable into main (#2799)Rowlando13before adoptiond791537

lines 19–27why, one step back
19* `inout <https://github.com/pallets/click/tree/main/examples/inout>`_ : A very simple example of an application that can read from files and write to files and also accept input from stdin or write to stdout.
20* `validation <https://github.com/pallets/click/tree/main/examples/validation>`_ : A simple example of an application that performs custom validation of parameters in different ways.21* `naval <https://github.com/pallets/click/tree/main/examples/naval>`_ : Port of the `docopt <http://docopt.org/>`_ naval example.22* `colors <https://github.com/pallets/click/tree/main/examples/colors>`_ : A simple example that colorizes text. Uses colorama on Windows.23* `aliases <https://github.com/pallets/click/tree/main/examples/aliases>`_ : An advanced example that implements :ref:`aliases`.24* `imagepipe <https://github.com/pallets/click/tree/main/examples/imagepipe>`_ : A complex example that implements some :ref:`command-pipelines` . It chains together image processing instructions. Requires pillow.25* `repo <https://github.com/pallets/click/tree/main/examples/repo>`_ : An advanced example that implements a Git-/Mercurial-like command line interface.26* `complex <https://github.com/pallets/click/tree/main/examples/complex>`_ : A very advanced example that implements loading subcommands dynamically from a plugin folder.27* `termui <https://github.com/pallets/click/tree/main/examples/termui>`_ : A simple example that showcases terminal UI helpers provided by click.

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 28–76why, one step back
28
29Basic Concepts - Creating a Command30-----------------------------------3132Click is based on declaring commands through decorators. Internally, there33is a non-decorator interface for advanced use cases, but it's discouraged34for high-level usage.3536A function becomes a Click command line tool by decorating it through37:func:`click.command`. At its simplest, just decorating a function38with this decorator will make it into a callable script:3940.. click:example::4142 import click4344 @click.command()45 def hello():46 click.echo('Hello World!')4748What's happening is that the decorator converts the function into a49:class:`Command` which then can be invoked::5051 if __name__ == '__main__':52 hello()5354And what it looks like:5556.. click:run::5758 invoke(hello, args=[], prog_name='python hello.py')5960And the corresponding help page:6162.. click:run::6364 invoke(hello, args=['--help'], prog_name='python hello.py')6566Echoing67-------6869Why does this example use :func:`echo` instead of the regular70:func:`print` function? The answer to this question is that Click71attempts to support different environments consistently and to be very72robust even when the environment is misconfigured. Click wants to be73functional at least on a basic level even if everything is completely74broken.7576What this means is that the :func:`echo` function applies some error

Merge branch '8.1.x'David Lordbefore adoption6bd8e56

line 77why, one step back
77correction in case the terminal is misconfigured instead of dying with a

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 78–133why, one step back
78:exc:`UnicodeError`.
7980The echo function also supports color and other styles in output. It81will automatically remove styles if the output stream is a file. On82Windows, colorama is automatically installed and used. See83:ref:`ansi-colors`.8485If you don't need this, you can also use the `print()` construct /86function.8788Nesting Commands89----------------9091Commands can be attached to other commands of type :class:`Group`. This92allows arbitrary nesting of scripts. As an example here is a script that93implements two commands for managing databases:9495.. click:example::9697 @click.group()98 def cli():99 pass100101 @click.command()102 def initdb():103 click.echo('Initialized the database')104105 @click.command()106 def dropdb():107 click.echo('Dropped the database')108109 cli.add_command(initdb)110 cli.add_command(dropdb)111112As you can see, the :func:`group` decorator works like the :func:`command`113decorator, but creates a :class:`Group` object instead which can be given114multiple subcommands that can be attached with :meth:`Group.add_command`.115116For simple scripts, it's also possible to automatically attach and create a117command by using the :meth:`Group.command` decorator instead. The above118script can instead be written like this:119120.. click:example::121122 @click.group()123 def cli():124 pass125126 @cli.command()127 def initdb():128 click.echo('Initialized the database')129130 @cli.command()131 def dropdb():132 click.echo('Dropped the database')133

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

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

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 135–182why, one step back
135
136 if __name__ == '__main__':137 cli()138139140Registering Commands Later141--------------------------142143Instead of using the ``@group.command()`` decorator, commands can be144decorated with the plain ``@click.command()`` decorator and registered145with a group later with ``group.add_command()``. This could be used to146split commands into multiple Python modules.147148.. code-block:: python149150 @click.command()151 def greet():152 click.echo("Hello, World!")153154.. code-block:: python155156 @click.group()157 def group():158 pass159160 group.add_command(greet)161162163Adding Parameters164-----------------165166To add parameters, use the :func:`option` and :func:`argument` decorators:167168.. click:example::169170 @click.command()171 @click.option('--count', default=1, help='number of greetings')172 @click.argument('name')173 def hello(count, name):174 for x in range(count):175 click.echo(f"Hello {name}!")176177What it looks like:178179.. click:run::180181 invoke(hello, args=['--help'], prog_name='python hello.py')182

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

lines 183–184why, one step back
183Switching to Entry Points
184-------------------------

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

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

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

line 189why, one step back
189doing that, but a better way is to package your app with an entry point.

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 190–192why, one step back
190
191There are two main (and many more) reasons for this:192

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

line 193why, one step back
193The first one is that installers automatically generate executable

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 194–195why, one step back
194wrappers for Windows so your command line utilities work on Windows too.
195

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

line 196why, one step back
196The second reason is that entry point scripts work with virtualenv on Unix

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

lines 197–201why, one step back
197without the virtualenv having to be activated. This is a very useful
198concept which allows you to bundle your scripts with all requirements into199a virtualenv.200201Click is perfectly equipped to work with that and in fact the rest of the

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

lines 202–203why, one step back
202documentation will assume that you are writing applications as distributed
203packages.

Merge remote-tracking branch 'origin/8.0.x'David Lordbefore adoption051d57c

line 204why, one step back
204

switch to flit build backend (#2543)David Lordbefore adoption3fcf338

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