quickstart.rst
This file
Path
docs/quickstart.rst
129 lines · 3.1 KiB
Last changed by
Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
History of this fileTake it
1Quickstart2==========34.. currentmodule:: click56You can get the library directly from PyPI::78 pip install click910Basic Concepts11--------------1213Click is based on declaring commands through decorators. Internally there14is a non-decorator interface for advanced use cases but it's discouraged15for high-level usage.1617A function becomes a click command line tool by decorating it through18:func:`click.command`. In the most simple version just decorating a19function with this decorator will make it into a callable script:2021.. click:example::2223 import click2425 @click.command()26 def hello():27 click.echo('Hello World!')2829What's happening is that the decorator converts the function into a30:class:`Command` which then can be invoked::3132 if __name__ == '__main__':33 hello()3435And what it looks like:3637.. click:run::3839 invoke(hello, args=[])4041And the corresponding help page:4243.. click:run::4445 invoke(hello, args=['--help'])4647Echoing48-------4950Why does this example use :func:`echo` instead of the regular51:func:`print` function? The answer to this question is that click52attempts to support both Python 2 and Python 3 the same way and to be very53robust even when the environment is misconfigured. Click wants to be54functional at least on a basic level even if everything is completely55broken.5657What this means is that the :func:`echo` function applies some error58correction in case the terminal is misconfigured instead of dying with an59:exc:`UnicodeError`.6061If you don't need this, you can also use the `print()` construct /62function.6364Nesting Commands65----------------6667Commands can be attached to other commands of type :class:`Group`. This68allows arbitrary nesting of scripts. As an example here is a script that69implements two commands for managing databases:7071.. click:example::7273 @click.group()74 def cli():75 pass7677 @click.command()78 def initdb():79 click.echo('Initialized the database')8081 @click.command()82 def dropdb():83 click.echo('Dropped the database')8485 cli.add_command(initdb)86 cli.add_command(dropdb)8788As you can see the :func:`group` decorator works like the :func:`command`89decorator but creates a :class:`Group` object instead which can be given90multiple subcommands that can be attached with91:meth:`Group.add_command`.9293For simple scripts it's also possible to automatically attach and create a94command by using the :meth:`Group.command` decorator instead. The above95script can be written like this then:9697.. click:example::9899 @click.group()100 def cli():101 pass102103 @cli.command()104 def initdb():105 click.echo('Initialized the database')106107 @cli.command()108 def dropdb():109 click.echo('Dropped the database')110111Adding Parameters112-----------------113114To add parameters the :func:`option` and :func:`argument` decorators:115116.. click:example::117118 @click.command()119 @click.option('--count', default=1, help='number of greetings')120 @click.argument('name')121 def hello(count, name):122 for x in range(count):123 click.echo('Hello %s!' % name)124125What it looks like:126127.. click:run::128129 invoke(hello, args=['--help'])