Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than ce1a418 point at it.

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 1–9why, one step back
1Quickstart
2==========34.. currentmodule:: click56You can get the library directly from PyPI::78 pip install click9

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 10–83why, one step back
10The 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.83

Merge pull request #208 from willingc/masterArmin Ronacherbefore adoption4507fd5

line 84why, one step back
84Now, let's move on. Enter the following command to get Click activated in your

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 85–90why, one step back
85virtualenv::
8687 $ pip install Click8889A few seconds later and you are good to go.90

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 91–93why, one step back
91Screencast and Examples
92-----------------------93

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 94why, one step back
94There is a screencast available which shows the basic API of Click and

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 95–100why, one step back
95how to build simple applications with it. It also explores how to build
96commands with subcommands.9798* `Building Command Line Applications with Click99 <https://www.youtube.com/watch?v=kNke39OZ2k0>`_100

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 101why, one step back
101Examples of Click applications can be found in the documentation as well

Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd

line 102why, one step back
102as in the GitHub repository together with readme files:

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 103–109why, one step back
103
104* ``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 back
110* ``repo``: `Git-/Mercurial-like command line interface

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 111–119why, one step back
111 <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>`_114* ``validation``: `Custom parameter validation example115 <https://github.com/mitsuhiko/click/tree/master/examples/validation>`_116* ``colors``: `Colorama ANSI color support117 <https://github.com/mitsuhiko/click/tree/master/examples/colors>`_118* ``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 back
120* ``imagepipe``: `Multi command chaining demo
121 <https://github.com/mitsuhiko/click/tree/master/examples/imagepipe>`_

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 122–125why, one step back
122
123Basic Concepts124--------------125

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 126–127why, one step back
126Click is based on declaring commands through decorators. Internally, there
127is a non-decorator interface for advanced use cases, but it's discouraged

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 128–129why, one step back
128for high-level usage.
129

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 130why, one step back
130A function becomes a Click command line tool by decorating it through

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 131–132why, one step back
131:func:`click.command`. At its simplest, just decorating a function
132with this decorator will make it into a callable script:

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 133–151why, one step back
133
134.. 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::151

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 152why, one step back
152 invoke(hello, args=[], prog_name='python hello.py')

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 153–157why, one step back
153
154And the corresponding help page:155156.. click:run::157

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

line 158why, one step back
158 invoke(hello, args=['--help'], prog_name='python hello.py')

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 159–163why, one step back
159
160Echoing161-------162163Why does this example use :func:`echo` instead of the regular

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 164why, one step back
164:func:`print` function? The answer to this question is that Click

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 165–173why, one step back
165attempts to support both Python 2 and Python 3 the same way and to be very
166robust 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`.173

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

line 174why, one step back
174As an added benefit, starting with Click 2.0, the echo function also

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 175–205why, one step back
175has good support for ANSI colors. It will automatically strip ANSI codes
176if 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)205

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

lines 206–208why, one step back
206As 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`.

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

line 209why, one step back
209

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 210why, one step back
210For simple scripts, it's also possible to automatically attach and create a

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

line 211why, one step back
211command by using the :meth:`Group.command` decorator instead. The above

Documentation fixesKevin Yapbefore adoption67523af

line 212why, one step back
212script can instead be written like this:

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 213–230why, one step back
213
214.. 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-----------------230

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 231why, one step back
231To add parameters, use the :func:`option` and :func:`argument` decorators:

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 232–245why, one step back
232
233.. 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::245

Rewrote the setuptools documentation.Armin Ronacherbefore adoption61b6ffa

lines 246–274why, one step back
246 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.