quickstart.rst
This file
Path
docs/quickstart.rst
274 lines · 8.7 KiB
Last changed by
Merge pull request #208 from willingc/masterArmin Ronacherbefore adoption4507fd5
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 click9Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa
lines 10–83why, one step back10The installation into a :ref:`virtualenv` is heavily recommended.1112.. _virtualenv:1314virtualenv15----------1617Virtualenv is probably what you want to use for developing Click18applications.1920What problem does virtualenv solve? Chances are that you want to use it21for other projects besides your Click script. But the more projects you22have, the more likely it is that you will be working with different23versions of Python itself, or at least different versions of Python24libraries. Let's face it: quite often libraries break backwards25compatibility, and it's unlikely that any serious application will have26zero dependencies. So what do you do if two or more of your projects have27conflicting dependencies?2829Virtualenv to the rescue! Virtualenv enables multiple side-by-side30installations of Python, one for each project. It doesn't actually31install separate copies of Python, but it does provide a clever way to32keep different project environments isolated. Let's see how virtualenv33works.3435If you are on Mac OS X or Linux, chances are that one of the following two36commands will work for you::3738 $ sudo easy_install virtualenv3940or even better::4142 $ sudo pip install virtualenv4344One of these will probably install virtualenv on your system. Maybe it's even45in your package manager. If you use Ubuntu, try::4647 $ sudo apt-get install python-virtualenv4849If you are on Windows (or none of the above methods worked) you must install50``pip`` first. For more information about this, see `installing pip`_.51Once you have it installed, run the ``pip`` command from above, but without52the `sudo` prefix.5354.. _installing pip: http://pip.readthedocs.org/en/latest/installing.html5556Once you have virtualenv installed, just fire up a shell and create57your own environment. I usually create a project folder and a `venv`58folder within::5960 $ mkdir myproject61 $ cd myproject62 $ virtualenv venv63 New python executable in venv/bin/python64 Installing setuptools, pip............done.6566Now, whenever you want to work on a project, you only have to activate the67corresponding environment. On OS X and Linux, do the following::6869 $ . venv/bin/activate7071If you are a Windows user, the following command is for you::7273 $ venv\scripts\activate7475Either way, you should now be using your virtualenv (notice how the prompt of76your shell has changed to show the active environment).7778And if you want to go back to the real world, use the following command::7980 $ deactivate8182After doing this, the prompt of your shell should be as familar as before.83Merge pull request #208 from willingc/masterArmin Ronacherbefore adoption4507fd5
line 84why, one step back84Now, let's move on. Enter the following command to get Click activated in yourRewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa
lines 85–90why, one step back85virtualenv::8687 $ pip install Click8889A few seconds later and you are good to go.90Linked examples from documentationArmin Ronacherbefore adoption2bab892
lines 91–92why, one step back91Screencast and Examples92-----------------------Added link to screencastArmin Ronacherbefore adoption9716618
line 93why, one step back93Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 94why, one step back94There is a screencast available which shows the basic API of Click andAdded link to screencastArmin Ronacherbefore adoption9716618
lines 95–99why, one step back95how to build simple applications with it. It also explores how to build96commands with subcommands.9798* `Building Command Line Applications with Click99 <https://www.youtube.com/watch?v=kNke39OZ2k0>`_Linked examples from documentationArmin Ronacherbefore adoption2bab892
line 100why, one step back100Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 101why, one step back101Examples of Click applications can be found in the documentation as wellMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 102why, one step back102as in the GitHub repository together with readme files:Linked examples from documentationArmin Ronacherbefore adoption2bab892
lines 103–109why, one step back103104* ``inout``: `File input and output105 <https://github.com/mitsuhiko/click/tree/master/examples/inout>`_106* ``naval``: `Port of docopt naval example107 <https://github.com/mitsuhiko/click/tree/master/examples/naval>`_108* ``aliases``: `Command alias example109 <https://github.com/mitsuhiko/click/tree/master/examples/aliases>`_Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 110why, one step back110* ``repo``: `Git-/Mercurial-like command line interfaceLinked examples from documentationArmin Ronacherbefore adoption2bab892
lines 111–113why, one step back111 <https://github.com/mitsuhiko/click/tree/master/examples/repo>`_112* ``complex``: `Complex example with plugin loading113 <https://github.com/mitsuhiko/click/tree/master/examples/complex>`_Added validation example to docs.Armin Ronacherbefore adoption438b945
lines 114–115why, one step back114* ``validation``: `Custom parameter validation example115 <https://github.com/mitsuhiko/click/tree/master/examples/validation>`_Added a colorama example.Armin Ronacherbefore adoption6e31b1c
lines 116–117why, one step back116* ``colors``: `Colorama ANSI color support117 <https://github.com/mitsuhiko/click/tree/master/examples/colors>`_Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 118–119why, one step back118* ``termui``: `Terminal UI functions demo119 <https://github.com/mitsuhiko/click/tree/master/examples/termui>`_Added imagepipe example to docsArmin Ronacherbefore adoption847fcbc
lines 120–121why, one step back120* ``imagepipe``: `Multi command chaining demo121 <https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`_Added link to screencastArmin Ronacherbefore adoption9716618
line 122why, one step back122Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 123–125why, one step back123Basic Concepts124--------------125Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 126–127why, one step back126Click is based on declaring commands through decorators. Internally, there127is a non-decorator interface for advanced use cases, but it's discouragedUpdated the exampleArmin Ronacherbefore adoptiona5a0471
lines 128–129why, one step back128for high-level usage.129Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 130why, one step back130A function becomes a Click command line tool by decorating it throughMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 131–132why, one step back131:func:`click.command`. At its simplest, just decorating a function132with this decorator will make it into a callable script:Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 133–151why, one step back133134.. click:example::135136 import click137138 @click.command()139 def hello():140 click.echo('Hello World!')141142What's happening is that the decorator converts the function into a143:class:`Command` which then can be invoked::144145 if __name__ == '__main__':146 hello()147148And what it looks like:149150.. click:run::151Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa
line 152why, one step back152 invoke(hello, args=[], prog_name='python hello.py')Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 153–157why, one step back153154And the corresponding help page:155156.. click:run::157Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa
line 158why, one step back158 invoke(hello, args=['--help'], prog_name='python hello.py')Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 159–163why, one step back159160Echoing161-------162163Why does this example use :func:`echo` instead of the regularMerge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 164why, one step back164:func:`print` function? The answer to this question is that ClickUpdated the exampleArmin Ronacherbefore adoptiona5a0471
lines 165–173why, one step back165attempts to support both Python 2 and Python 3 the same way and to be very166robust even when the environment is misconfigured. Click wants to be167functional at least on a basic level even if everything is completely168broken.169170What this means is that the :func:`echo` function applies some error171correction in case the terminal is misconfigured instead of dying with an172:exc:`UnicodeError`.173Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 174why, one step back174As 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 175–177why, one step back175has good support for ANSI colors. It will automatically strip ANSI codes176if the output stream is a file and if colorama is supported, ANSI colors177will also work on Windows. See :ref:`ansi-colors` for more information.Cross link for ansi colors.Armin Ronacherbefore adoptionc5d53f8
line 178why, one step back178Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 179–205why, one step back179If you don't need this, you can also use the `print()` construct /180function.181182Nesting Commands183----------------184185Commands can be attached to other commands of type :class:`Group`. This186allows arbitrary nesting of scripts. As an example here is a script that187implements two commands for managing databases:188189.. click:example::190191 @click.group()192 def cli():193 pass194195 @click.command()196 def initdb():197 click.echo('Initialized the database')198199 @click.command()200 def dropdb():201 click.echo('Dropped the database')202203 cli.add_command(initdb)204 cli.add_command(dropdb)205Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 206–208why, one step back206As you can see, the :func:`group` decorator works like the :func:`command`207decorator, but creates a :class:`Group` object instead which can be given208multiple subcommands that can be attached with :meth:`Group.add_command`.Updated the exampleArmin Ronacherbefore adoptiona5a0471
line 209why, one step back209Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 210why, one step back210For simple scripts, it's also possible to automatically attach and create aUpdated the exampleArmin Ronacherbefore adoptiona5a0471
line 211why, one step back211command by using the :meth:`Group.command` decorator instead. The aboveDocumentation fixesKevin Yapbefore adoption67523af
line 212why, one step back212script can instead be written like this:Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 213–230why, one step back213214.. click:example::215216 @click.group()217 def cli():218 pass219220 @cli.command()221 def initdb():222 click.echo('Initialized the database')223224 @cli.command()225 def dropdb():226 click.echo('Dropped the database')227228Adding Parameters229-----------------230Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 231why, one step back231To add parameters, use the :func:`option` and :func:`argument` decorators:Updated the exampleArmin Ronacherbefore adoptiona5a0471
lines 232–245why, one step back232233.. click:example::234235 @click.command()236 @click.option('--count', default=1, help='number of greetings')237 @click.argument('name')238 def hello(count, name):239 for x in range(count):240 click.echo('Hello %s!' % name)241242What it looks like:243244.. click:run::245Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa
lines 246–274why, one step back246 invoke(hello, args=['--help'], prog_name='python hello.py')247248.. _switching-to-setuptools:249250Switching to Setuptools251-----------------------252253In the code you wrote so far there is a block at the end of the file which254looks like this: ``if __name__ == '__main__':``. This is traditionally255how a standalone Python file looks like. With Click you can continue256doing that, but there are better ways through setuptools.257258There are two main (and many more) reasons for this:259260The first one is that setuptools automatically generates executable261wrappers for Windows so your command line utilities work on Windows too.262263The second reason is that setuptools scripts work with virtualenv on Unix264without the virtualenv having to be activated. This is a very useful265concept which allows you to bundle your scripts with all requirements into266a virtualenv.267268Click is perfectly equipped to work with that and in fact the rest of the269documentation will assume that you are writing applications through270setuptools.271272I strongly recommend to have a look at the :ref:`setuptools-integration`273chapter before reading the rest as the examples assume that you will274be using setuptools.