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
Initial commitArmin Ronacherbefore adoption4101de3
lines 1–181Quickstart2==========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 aMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 19why, one step back19function with this decorator will make it into a callable script:20Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 21–22why, one step back21.. click:example::22Initial commitArmin Ronacherbefore adoption4101de3
lines 23–2623 import click2425 @click.command()26 def hello():Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 27why, one step back27 click.echo('Hello World!')Initial commitArmin Ronacherbefore adoption4101de3
lines 28–342829What's happening is that the decorator converts the function into a30:class:`Command` which then can be invoked::3132 if __name__ == '__main__':33 hello()34Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
lines 35–46why, one step back35And what it looks like:3637.. click:run::3839 invoke(hello, args=[])4041And the corresponding help page:4243.. click:run::4445 invoke(hello, args=['--help'])46Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
lines 47–63why, one step back47Echoing48-------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.63Initial commitArmin Ronacherbefore adoption4101de3
lines 64–6864Nesting Commands65----------------6667Commands can be attached to other commands of type :class:`Group`. This68allows arbitrary nesting of scripts. As an example here is a script thatMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 69why, one step back69implements two commands for managing databases:70Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 71why, one step back71.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 72–787273 @click.group()74 def cli():75 pass7677 @click.command()78 def initdb():Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 79why, one step back79 click.echo('Initialized the database')Initial commitArmin Ronacherbefore adoption4101de3
lines 80–828081 @click.command()82 def dropdb():Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 83why, one step back83 click.echo('Dropped the database')Initial commitArmin Ronacherbefore adoption4101de3
lines 84–948485 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 aboveMake documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 95why, one step back95script can be written like this then:96Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 97why, one step back97.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 98–1049899 @click.group()100 def cli():101 pass102103 @cli.command()104 def initdb():Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 105why, one step back105 click.echo('Initialized the database')Initial commitArmin Ronacherbefore adoption4101de3
lines 106–108106107 @cli.command()108 def dropdb():Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 109why, one step back109 click.echo('Dropped the database')Initial commitArmin Ronacherbefore adoption4101de3
lines 110–113110111Adding Parameters112-----------------113Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 114why, one step back114To add parameters the :func:`option` and :func:`argument` decorators:115Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 116why, one step back116.. click:example::Initial commitArmin Ronacherbefore adoption4101de3
lines 117–122117118 @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):Implemented and exposed click.echoArmin Ronacherbefore adoption95c24b3
line 123why, one step back123 click.echo('Hello %s!' % name)124Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 125why, one step back125What it looks like:126Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 127why, one step back127.. click:run::128Make documentation invoke click and fix a bunch of issuesArmin Ronacherbefore adoption0a44670
line 129why, one step back129 invoke(hello, args=['--help'])