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 fileTake it
The walk stopped after 200 commits: lines older than a5a0471 point at it.
Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 1–9why, one step back1Quickstart2==========34.. currentmodule:: click56You can get the library directly from PyPI::78 pip install click9Linked examples from documentationArmin Ronacherbefore adoption2bab892
lines 10–11why, one step back10Screencast and Examples11-----------------------Added link to screencastArmin Ronacherbefore adoption9716618
line 12why, one step back12Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 13why, one step back13There is a screencast available which shows the basic API of Click andAdded link to screencastArmin Ronacherbefore adoption9716618
lines 14–18why, one step back14how 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>`_Linked examples from documentationArmin Ronacherbefore adoption2bab892
line 19why, one step back19Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 20why, one step back20Examples of Click applications can be found in the documentation as wellMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 21why, one step back21as in the GitHub repository together with readme files:Linked examples from documentationArmin Ronacherbefore adoption2bab892
lines 22–28why, one step back2223* ``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>`_Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 29why, one step back29* ``repo``: `Git-/Mercurial-like command line interfaceLinked examples from documentationArmin Ronacherbefore adoption2bab892
lines 30–32why, one step back30 <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>`_Added validation example to docs.Armin Ronacherbefore adoption438b945
lines 33–34why, one step back33* ``validation``: `Custom parameter validation example34 <https://github.com/mitsuhiko/click/tree/master/examples/validation>`_Added a colorama example.Armin Ronacherbefore adoption6e31b1c
lines 35–36why, one step back35* ``colors``: `Colorama ANSI color support36 <https://github.com/mitsuhiko/click/tree/master/examples/colors>`_Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 37–38why, one step back37* ``termui``: `Terminal UI functions demo38 <https://github.com/mitsuhiko/click/tree/master/examples/termui>`_Added imagepipe example to docsArmin Ronacherbefore adoption847fcbc
lines 39–40why, one step back39* ``imagepipe``: `Multi command chaining demo40 <https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`_Added link to screencastArmin Ronacherbefore adoption9716618
line 41why, one step back41Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 42–44why, one step back42Basic Concepts43--------------44Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 45–46why, one step back45Click is based on declaring commands through decorators. Internally, there46is a non-decorator interface for advanced use cases, but it's discouragedUpdated the exampleArmin Ronacherbefore adoptiona5a0471
lines 47–48why, one step back47for high-level usage.48Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 49why, one step back49A function becomes a Click command line tool by decorating it throughMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 50–51why, one step back50:func:`click.command`. At its simplest, just decorating a function51with this decorator will make it into a callable script:Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 52–82why, one step back5253.. 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 regularMerge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 83why, one step back83:func:`print` function? The answer to this question is that ClickUpdated the exampleArmin Ronacherbefore adoptiona5a0471
lines 84–92why, one step back84attempts 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`.92Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 93why, one step back93As an added benefit, starting with Click 2.0, the echo function alsoAdded style helpers and added deeper ansi color support. Colorama now optional.Armin Ronacherbefore adoptionf516eee
lines 94–96why, one step back94has 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.Cross link for ansi colors.Armin Ronacherbefore adoptionc5d53f8
line 97why, one step back97Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 98–124why, one step back98If 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)124Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 125–127why, one step back125As 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`.Updated the exampleArmin Ronacherbefore adoptiona5a0471
line 128why, one step back128Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 129why, one step back129For simple scripts, it's also possible to automatically attach and create aUpdated the exampleArmin Ronacherbefore adoptiona5a0471
line 130why, one step back130command by using the :meth:`Group.command` decorator instead. The aboveDocumentation fixesKevin Yapbefore adoption67523af
line 131why, one step back131script can instead be written like this:Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 132–149why, one step back132133.. 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-----------------149Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 150why, one step back150To add parameters, use the :func:`option` and :func:`argument` decorators:Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 151–165why, one step back151152.. 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'])