quickstart.md
This file
Path
docs/quickstart.md
196 lines · 6.8 KiB · Markdown
Last changed by
remove redundant click. module linkJaron Rosenaubefore adoption4b695e5
History of this fileTake it
convert from reST to MySTdenuowebbefore adoption38d9d60
line 1why, one step back1# Quickstartchange ext from .rst to .mddenuowebbefore adoption11384eb
line 2why, one step back2convert from reST to MySTdenuowebbefore adoption38d9d60
lines 3–4why, one step back3```{currentmodule} click4```change ext from .rst to .mddenuowebbefore adoption11384eb
line 5why, one step back5convert from reST to MySTdenuowebbefore adoption38d9d60
line 6why, one step back6## Installchange ext from .rst to .mddenuowebbefore adoption11384eb
line 7why, one step back7convert from reST to MySTdenuowebbefore adoption38d9d60
line 8why, one step back8Install from PyPI:change ext from .rst to .mddenuowebbefore adoption11384eb
line 9why, one step back9convert from reST to MySTdenuowebbefore adoption38d9d60
lines 10–12why, one step back10```console11pip install click12```change ext from .rst to .mddenuowebbefore adoption11384eb
line 13why, one step back13convert from reST to MySTdenuowebbefore adoption38d9d60
line 14why, one step back14Installing into a virtual environment is highly recommended. We suggest {ref}`virtualenv-heading`.change ext from .rst to .mddenuowebbefore adoption11384eb
line 15why, one step back15convert from reST to MySTdenuowebbefore adoption38d9d60
line 16why, one step back16## Exampleschange ext from .rst to .mddenuowebbefore adoption11384eb
line 17why, one step back17convert from reST to MySTdenuowebbefore adoption38d9d60
lines 18–19why, one step back18Some standalone examples of Click applications are packaged with Click. They are available in the19[examples folder](https://github.com/pallets/click/tree/main/examples) of the repo.change ext from .rst to .mddenuowebbefore adoption11384eb
line 20why, one step back20convert from reST to MySTdenuowebbefore adoption38d9d60
lines 21–38why, one step back21- [inout](https://github.com/pallets/click/tree/main/examples/inout) : A very simple example of an application that can22 read from files and write to files and also accept input from stdin or write to stdout.23- [validation](https://github.com/pallets/click/tree/main/examples/validation) : A simple example of an application that24 performs custom validation of parameters in different ways.25- [naval](https://github.com/pallets/click/tree/main/examples/naval) : Port of the [docopt](http://docopt.org/) naval26 example.27- [colors](https://github.com/pallets/click/tree/main/examples/colors) : A simple example that colorizes text. Uses28 colorama on Windows.29- [aliases](https://github.com/pallets/click/tree/main/examples/aliases) : An advanced example that implements30 {ref}`aliases`.31- [imagepipe](https://github.com/pallets/click/tree/main/examples/imagepipe) : A complex example that implements some32 {ref}`command-pipelines` . It chains together image processing instructions. Requires pillow.33- [repo](https://github.com/pallets/click/tree/main/examples/repo) : An advanced example that implements a34 Git-/Mercurial-like command line interface.35- [complex](https://github.com/pallets/click/tree/main/examples/complex) : A very advanced example that implements36 loading subcommands dynamically from a plugin folder.37- [termui](https://github.com/pallets/click/tree/main/examples/termui) : A simple example that showcases terminal UI38 helpers provided by click.change ext from .rst to .mddenuowebbefore adoption11384eb
line 39why, one step back39convert from reST to MySTdenuowebbefore adoption38d9d60
line 40why, one step back40## Basic Concepts - Creating a Commandchange ext from .rst to .mddenuowebbefore adoption11384eb
line 41why, one step back41convert from reST to MySTdenuowebbefore adoption38d9d60
lines 42–43why, one step back42Click is based on declaring commands through decorators. Internally, there is a non-decorator interface for advanced use43cases, but it's discouraged for high-level usage.change ext from .rst to .mddenuowebbefore adoption11384eb
line 44why, one step back44remove redundant click. module linkJaron Rosenaubefore adoption4b695e5
line 45why, one step back45A function becomes a Click command line tool by decorating it through {func}`command`. At its simplest, justconvert from reST to MySTdenuowebbefore adoption38d9d60
line 46why, one step back46decorating a function with this decorator will make it into a callable script:change ext from .rst to .mddenuowebbefore adoption11384eb
line 47why, one step back47convert from reST to MySTdenuowebbefore adoption38d9d60
lines 48–50why, one step back4849```{eval-rst}50.. click:example::change ext from .rst to .mddenuowebbefore adoption11384eb
lines 51–56why, one step back51 import click5253 @click.command()54 def hello():55 click.echo('Hello World!')56convert from reST to MySTdenuowebbefore adoption38d9d60
line 57why, one step back57What's happening is that the decorator converts the function into a :class:`Command` which then can be invoked:change ext from .rst to .mddenuowebbefore adoption11384eb
line 58why, one step back58convert from reST to MySTdenuowebbefore adoption38d9d60
line 59why, one step back59.. click:example::change ext from .rst to .mddenuowebbefore adoption11384eb
lines 60–71why, one step back60 if __name__ == '__main__':61 hello()6263And what it looks like:6465.. click:run::66 invoke(hello, args=[], prog_name='python hello.py')6768And the corresponding help page:6970.. click:run::71 invoke(hello, args=['--help'], prog_name='python hello.py')convert from reST to MySTdenuowebbefore adoption38d9d60
line 72why, one step back72```change ext from .rst to .mddenuowebbefore adoption11384eb
line 73why, one step back73convert from reST to MySTdenuowebbefore adoption38d9d60
line 74why, one step back74## Echoingchange ext from .rst to .mddenuowebbefore adoption11384eb
line 75why, one step back75convert from reST to MySTdenuowebbefore adoption38d9d60
lines 76–78why, one step back76Why does this example use {func}`echo` instead of the regular {func}`print` function? The answer to this question is77that Click attempts to support different environments consistently and to be very robust even when the environment is78misconfigured. Click wants to be functional at least on a basic level even if everything is completely broken.change ext from .rst to .mddenuowebbefore adoption11384eb
line 79why, one step back79convert from reST to MySTdenuowebbefore adoption38d9d60
lines 80–81why, one step back80What this means is that the {func}`echo` function applies some error correction in case the terminal is misconfigured81instead of dying with a {exc}`UnicodeError`.change ext from .rst to .mddenuowebbefore adoption11384eb
line 82why, one step back82convert from reST to MySTdenuowebbefore adoption38d9d60
lines 83–84why, one step back83The echo function also supports color and other styles in output. It will automatically remove styles if the output84stream is a file. On Windows, colorama is automatically installed and used. See {ref}`ansi-colors`.change ext from .rst to .mddenuowebbefore adoption11384eb
line 85why, one step back85convert from reST to MySTdenuowebbefore adoption38d9d60
line 86why, one step back86If you don't need this, you can also use the `print()` construct / function.change ext from .rst to .mddenuowebbefore adoption11384eb
line 87why, one step back87convert from reST to MySTdenuowebbefore adoption38d9d60
line 88why, one step back88## Nesting Commandschange ext from .rst to .mddenuowebbefore adoption11384eb
line 89why, one step back89convert from reST to MySTdenuowebbefore adoption38d9d60
lines 90–91why, one step back90Commands can be attached to other commands of type {class}`Group`. This allows arbitrary nesting of scripts. As an91example here is a script that implements two commands for managing databases:change ext from .rst to .mddenuowebbefore adoption11384eb
line 92why, one step back92convert from reST to MySTdenuowebbefore adoption38d9d60
line 93why, one step back93```{eval-rst}change ext from .rst to .mddenuowebbefore adoption11384eb
lines 94–108why, one step back94.. click:example::95 @click.group()96 def cli():97 pass9899 @click.command()100 def initdb():101 click.echo('Initialized the database')102103 @click.command()104 def dropdb():105 click.echo('Dropped the database')106107 cli.add_command(initdb)108 cli.add_command(dropdb)convert from reST to MySTdenuowebbefore adoption38d9d60
line 109why, one step back109```change ext from .rst to .mddenuowebbefore adoption11384eb
line 110why, one step back110convert from reST to MySTdenuowebbefore adoption38d9d60
lines 111–112why, one step back111As you can see, the {func}`group` decorator works like the {func}`command` decorator, but creates a {class}`Group`112object instead which can be given multiple subcommands that can be attached with {meth}`Group.add_command`.change ext from .rst to .mddenuowebbefore adoption11384eb
line 113why, one step back113convert from reST to MySTdenuowebbefore adoption38d9d60
lines 114–115why, one step back114For simple scripts, it's also possible to automatically attach and create a command by using the {meth}`Group.command`115decorator instead. The above script can instead be written like this:change ext from .rst to .mddenuowebbefore adoption11384eb
line 116why, one step back116convert from reST to MySTdenuowebbefore adoption38d9d60
line 117why, one step back117```{eval-rst}change ext from .rst to .mddenuowebbefore adoption11384eb
lines 118–130why, one step back118.. click:example::119 @click.group()120 def cli():121 pass122123 @cli.command()124 def initdb():125 click.echo('Initialized the database')126127 @cli.command()128 def dropdb():129 click.echo('Dropped the database')130convert from reST to MySTdenuowebbefore adoption38d9d60
line 131why, one step back131You would then invoke the :class:`Group` in your entry points or other invocations:change ext from .rst to .mddenuowebbefore adoption11384eb
line 132why, one step back132convert from reST to MySTdenuowebbefore adoption38d9d60
line 133why, one step back133.. click:example::change ext from .rst to .mddenuowebbefore adoption11384eb
lines 134–135why, one step back134 if __name__ == '__main__':135 cli()convert from reST to MySTdenuowebbefore adoption38d9d60
line 136why, one step back136```change ext from .rst to .mddenuowebbefore adoption11384eb
line 137why, one step back137convert from reST to MySTdenuowebbefore adoption38d9d60
line 138why, one step back138## Registering Commands Laterchange ext from .rst to .mddenuowebbefore adoption11384eb
line 139why, one step back139convert from reST to MySTdenuowebbefore adoption38d9d60
lines 140–142why, one step back140Instead of using the `@group.command()` decorator, commands can be decorated with the plain `@command()` decorator141and registered with a group later with `group.add_command()`. This could be used to split commands into multiple Python142modules.change ext from .rst to .mddenuowebbefore adoption11384eb
line 143why, one step back143convert from reST to MySTdenuowebbefore adoption38d9d60
line 144why, one step back144```{code-block} pythonchange ext from .rst to .mddenuowebbefore adoption11384eb
lines 145–147why, one step back145 @click.command()146 def greet():147 click.echo("Hello, World!")convert from reST to MySTdenuowebbefore adoption38d9d60
line 148why, one step back148```change ext from .rst to .mddenuowebbefore adoption11384eb
line 149why, one step back149convert from reST to MySTdenuowebbefore adoption38d9d60
line 150why, one step back150```{code-block} pythonchange ext from .rst to .mddenuowebbefore adoption11384eb
lines 151–155why, one step back151 @click.group()152 def group():153 pass154155 group.add_command(greet)convert from reST to MySTdenuowebbefore adoption38d9d60
line 156why, one step back156```change ext from .rst to .mddenuowebbefore adoption11384eb
line 157why, one step back157convert from reST to MySTdenuowebbefore adoption38d9d60
line 158why, one step back158## Adding Parameterschange ext from .rst to .mddenuowebbefore adoption11384eb
line 159why, one step back159convert from reST to MySTdenuowebbefore adoption38d9d60
line 160why, one step back160To add parameters, use the {func}`option` and {func}`argument` decorators:change ext from .rst to .mddenuowebbefore adoption11384eb
line 161why, one step back161convert from reST to MySTdenuowebbefore adoption38d9d60
line 162why, one step back162```{eval-rst}change ext from .rst to .mddenuowebbefore adoption11384eb
lines 163–174why, one step back163.. click:example::164 @click.command()165 @click.option('--count', default=1, help='number of greetings')166 @click.argument('name')167 def hello(count, name):168 for x in range(count):169 click.echo(f"Hello {name}!")170171What it looks like:172173.. click:run::174 invoke(hello, args=['--help'], prog_name='python hello.py')convert from reST to MySTdenuowebbefore adoption38d9d60
line 175why, one step back175```change ext from .rst to .mddenuowebbefore adoption11384eb
line 176why, one step back176convert from reST to MySTdenuowebbefore adoption38d9d60
line 177why, one step back177## Switching to Entry Pointschange ext from .rst to .mddenuowebbefore adoption11384eb
line 178why, one step back178convert from reST to MySTdenuowebbefore adoption38d9d60
lines 179–180why, one step back179In the code you wrote so far there is a block at the end of the file which looks like this:180`if __name__ == '__main__':`. This is traditionally how a standalone Python file looks like. With Click you can continuechange ext from .rst to .mddenuowebbefore adoption11384eb
lines 181–184why, one step back181doing that, but a better way is to package your app with an entry point.182183There are two main (and many more) reasons for this:184convert from reST to MySTdenuowebbefore adoption38d9d60
lines 185–186why, one step back185The first one is that installers automatically generate executable wrappers for Windows so your command line utilities186work on Windows too.change ext from .rst to .mddenuowebbefore adoption11384eb
line 187why, one step back187convert from reST to MySTdenuowebbefore adoption38d9d60
lines 188–190why, one step back188The second reason is that entry point scripts work with virtualenv on Unix without the virtualenv having to be189activated. This is a very useful concept which allows you to bundle your scripts with all requirements into a190virtualenv.change ext from .rst to .mddenuowebbefore adoption11384eb
line 191why, one step back191convert from reST to MySTdenuowebbefore adoption38d9d60
lines 192–193why, one step back192Click is perfectly equipped to work with that and in fact the rest of the documentation will assume that you are writing193applications as distributed packages.change ext from .rst to .mddenuowebbefore adoption11384eb
line 194why, one step back194convert from reST to MySTdenuowebbefore adoption38d9d60
lines 195–196why, one step back195Look at the {doc}`entry-points` chapter before reading the rest as the examples assume that you will be using entry196points.