quickstart.rst
This file
Path
docs/quickstart.rst
274 lines · 8.7 KiB
Last changed by
Merge pull request #557 from Nudies/masterMarkus Unterwaditzerbefore adoption42e2821
History of this fileTake it
The walk stopped after 200 commits: lines older than 1318a20 point at it.
Added test for open_fileArmin Ronacherbefore adoption1318a20
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–81why, 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 $ deactivate81Merge pull request #557 from Nudies/masterMarkus Unterwaditzerbefore adoption42e2821
line 82why, one step back82After doing this, the prompt of your shell should be as familiar as before.Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa
line 83why, one step back83Merge 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.90Added test for open_fileArmin Ronacherbefore adoption1318a20
lines 91–104why, one step back91Screencast and Examples92-----------------------9394There is a screencast available which shows the basic API of Click and95how 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>`_100101Examples of Click applications can be found in the documentation as well102as in the GitHub repository together with readme files:103104* ``inout``: `File input and outputMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 105why, one step back105 <https://github.com/pallets/click/tree/master/examples/inout>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 106why, one step back106* ``naval``: `Port of docopt naval exampleMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 107why, one step back107 <https://github.com/pallets/click/tree/master/examples/naval>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 108why, one step back108* ``aliases``: `Command alias exampleMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 109why, one step back109 <https://github.com/pallets/click/tree/master/examples/aliases>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 110why, one step back110* ``repo``: `Git-/Mercurial-like command line interfaceMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 111why, one step back111 <https://github.com/pallets/click/tree/master/examples/repo>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 112why, one step back112* ``complex``: `Complex example with plugin loadingMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 113why, one step back113 <https://github.com/pallets/click/tree/master/examples/complex>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 114why, one step back114* ``validation``: `Custom parameter validation exampleMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 115why, one step back115 <https://github.com/pallets/click/tree/master/examples/validation>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 116why, one step back116* ``colors``: `Colorama ANSI color supportMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 117why, one step back117 <https://github.com/pallets/click/tree/master/examples/colors>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 118why, one step back118* ``termui``: `Terminal UI functions demoMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 119why, one step back119 <https://github.com/pallets/click/tree/master/examples/termui>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
line 120why, one step back120* ``imagepipe``: `Multi command chaining demoMerge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e
line 121why, one step back121 <https://github.com/pallets/click/tree/master/examples/imagepipe>`_Added test for open_fileArmin Ronacherbefore adoption1318a20
lines 122–151why, one step back122123Basic Concepts124--------------125126Click is based on declaring commands through decorators. Internally, there127is a non-decorator interface for advanced use cases, but it's discouraged128for high-level usage.129130A function becomes a Click command line tool by decorating it through131:func:`click.command`. At its simplest, just decorating a function132with this decorator will make it into a callable script:133134.. 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')Added test for open_fileArmin Ronacherbefore adoption1318a20
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')Added test for open_fileArmin Ronacherbefore adoption1318a20
lines 159–245why, one step back159160Echoing161-------162163Why does this example use :func:`echo` instead of the regular164:func:`print` function? The answer to this question is that Click165attempts 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`.173174As an added benefit, starting with Click 2.0, the echo function also175has 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.178179If 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)205206As 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`.209210For simple scripts, it's also possible to automatically attach and create a211command by using the :meth:`Group.command` decorator instead. The above212script can instead be written like this:213214.. 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-----------------230231To add parameters, use the :func:`option` and :func:`argument` decorators:232233.. 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.