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

1Quickstart2==========34.. currentmodule:: click56You can get the library directly from PyPI::78    pip install click910Screencast and Examples11-----------------------1213There is a screencast available which shows the basic API of Click and14how to build simple applications with it.  It also explores how to build15commands with subcommands.1617*   `Building Command Line Applications with Click18    <https://www.youtube.com/watch?v=kNke39OZ2k0>`_1920Examples of Click applications can be found in the documentation as well21as in the GitHub repository together with readme files:2223*   ``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>`_29*   ``repo``: `Git-/Mercurial-like command line interface30    <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>`_33*   ``validation``: `Custom parameter validation example34    <https://github.com/mitsuhiko/click/tree/master/examples/validation>`_35*   ``colors``: `Colorama ANSI color support36    <https://github.com/mitsuhiko/click/tree/master/examples/colors>`_37*   ``termui``: `Terminal UI functions demo38    <https://github.com/mitsuhiko/click/tree/master/examples/termui>`_39*   ``imagepipe``: `Multi command chaining demo40    <https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`_4142Basic Concepts43--------------4445Click is based on declaring commands through decorators.  Internally, there46is a non-decorator interface for advanced use cases, but it's discouraged47for high-level usage.4849A function becomes a Click command line tool by decorating it through50:func:`click.command`.  At its simplest, just decorating a function51with this decorator will make it into a callable script:5253.. 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 regular83:func:`print` function?  The answer to this question is that Click84attempts to support both Python 2 and Python 3 the same way and to be very85robust 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`.9293As an added benefit, starting with Click 2.0, the echo function also94has good support for ANSI colors.  It will automatically strip ANSI codes95if the output stream is a file and if colorama is supported, ANSI colors96will also work on Windows.  See :ref:`ansi-colors` for more information.9798If 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)124125As 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`.128129For simple scripts, it's also possible to automatically attach and create a130command by using the :meth:`Group.command` decorator instead.  The above131script can instead be written like this:132133.. 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-----------------149150To add parameters, use the :func:`option` and :func:`argument` decorators:151152.. 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'])