Skip to content
Myr

quickstart.rst

This file

Path

docs/quickstart.rst

277 lines · 8.8 KiB

Last changed by

Merge pull request #1161 from jdufresne/easyDavid Lordbefore adoption7d19964

History of this file

Take it

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

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 1–34why, one step back
1Quickstart
2==========34.. currentmodule:: click56You can get the library directly from PyPI::78 pip install click910The 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.34

Merge pull request #1161 from jdufresne/easyDavid Lordbefore adoption7d19964

line 35why, one step back
35If you are on Mac OS X or Linux::

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 36–48why, one step back
36
37 $ sudo pip install virtualenv3839One of these will probably install virtualenv on your system. Maybe it's even40in your package manager. If you use Ubuntu, try::4142 $ sudo apt-get install python-virtualenv4344If you are on Windows (or none of the above methods worked) you must install45``pip`` first. For more information about this, see `installing pip`_.46Once you have it installed, run the ``pip`` command from above, but without47the `sudo` prefix.48

Merge pull request #911 from jdufresne/readthedocsDavid Lordbefore adoption10a88a2

line 49why, one step back
49.. _installing pip: https://pip.readthedocs.io/en/latest/installing.html

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 50–76why, one step back
50
51Once you have virtualenv installed, just fire up a shell and create52your own environment. I usually create a project folder and a `venv`53folder within::5455 $ mkdir myproject56 $ cd myproject57 $ virtualenv venv58 New python executable in venv/bin/python59 Installing setuptools, pip............done.6061Now, whenever you want to work on a project, you only have to activate the62corresponding environment. On OS X and Linux, do the following::6364 $ . venv/bin/activate6566If you are a Windows user, the following command is for you::6768 $ venv\scripts\activate6970Either way, you should now be using your virtualenv (notice how the prompt of71your shell has changed to show the active environment).7273And if you want to go back to the real world, use the following command::7475 $ deactivate76

Merge pull request #557 from Nudies/masterMarkus Unterwaditzerbefore adoption42e2821

line 77why, one step back
77After doing this, the prompt of your shell should be as familiar as before.

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 78–99why, one step back
78
79Now, let's move on. Enter the following command to get Click activated in your80virtualenv::8182 $ pip install Click8384A few seconds later and you are good to go.8586Screencast and Examples87-----------------------8889There is a screencast available which shows the basic API of Click and90how to build simple applications with it. It also explores how to build91commands with subcommands.9293* `Building Command Line Applications with Click94 <https://www.youtube.com/watch?v=kNke39OZ2k0>`_9596Examples of Click applications can be found in the documentation as well97as in the GitHub repository together with readme files:9899* ``inout``: `File input and output

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 100why, one step back
100 <https://github.com/pallets/click/tree/master/examples/inout>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 101why, one step back
101* ``naval``: `Port of docopt naval example

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 102why, one step back
102 <https://github.com/pallets/click/tree/master/examples/naval>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 103why, one step back
103* ``aliases``: `Command alias example

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 104why, one step back
104 <https://github.com/pallets/click/tree/master/examples/aliases>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 105why, one step back
105* ``repo``: `Git-/Mercurial-like command line interface

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 106why, one step back
106 <https://github.com/pallets/click/tree/master/examples/repo>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 107why, one step back
107* ``complex``: `Complex example with plugin loading

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 108why, one step back
108 <https://github.com/pallets/click/tree/master/examples/complex>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 109why, one step back
109* ``validation``: `Custom parameter validation example

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 110why, one step back
110 <https://github.com/pallets/click/tree/master/examples/validation>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 111why, one step back
111* ``colors``: `Colorama ANSI color support

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 112why, one step back
112 <https://github.com/pallets/click/tree/master/examples/colors>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 113why, one step back
113* ``termui``: `Terminal UI functions demo

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 114why, one step back
114 <https://github.com/pallets/click/tree/master/examples/termui>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 115why, one step back
115* ``imagepipe``: `Multi command chaining demo

Merge pull request #550 from quobit/pallets-reallocationMarkus Unterwaditzerbefore adoption2489a7e

line 116why, one step back
116 <https://github.com/pallets/click/tree/master/examples/imagepipe>`_

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

line 117why, one step back
117

Merge pull request #1009 from dougharris/parameter-949Dan Sullybefore adoptionbf30737

lines 118–119why, one step back
118Basic Concepts - Creating a Command
119-----------------------------------

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 120–171why, one step back
120
121Click is based on declaring commands through decorators. Internally, there122is a non-decorator interface for advanced use cases, but it's discouraged123for high-level usage.124125A function becomes a Click command line tool by decorating it through126:func:`click.command`. At its simplest, just decorating a function127with this decorator will make it into a callable script:128129.. click:example::130131 import click132133 @click.command()134 def hello():135 click.echo('Hello World!')136137What's happening is that the decorator converts the function into a138:class:`Command` which then can be invoked::139140 if __name__ == '__main__':141 hello()142143And what it looks like:144145.. click:run::146147 invoke(hello, args=[], prog_name='python hello.py')148149And the corresponding help page:150151.. click:run::152153 invoke(hello, args=['--help'], prog_name='python hello.py')154155Echoing156-------157158Why does this example use :func:`echo` instead of the regular159:func:`print` function? The answer to this question is that Click160attempts to support both Python 2 and Python 3 the same way and to be very161robust even when the environment is misconfigured. Click wants to be162functional at least on a basic level even if everything is completely163broken.164165What this means is that the :func:`echo` function applies some error166correction in case the terminal is misconfigured instead of dying with an167:exc:`UnicodeError`.168169As an added benefit, starting with Click 2.0, the echo function also170has good support for ANSI colors. It will automatically strip ANSI codes171if the output stream is a file and if colorama is supported, ANSI colors

Merge pull request #1010 from rpgraham84/masterDan Sullybefore adoption692cb32

lines 172–174why, one step back
172will also work on Windows. Note that in Python 2, the :func:`echo` function
173does not parse color code information from bytearrays. See :ref:`ansi-colors`174for more information.

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 175–224why, one step back
175
176If you don't need this, you can also use the `print()` construct /177function.178179Nesting Commands180----------------181182Commands can be attached to other commands of type :class:`Group`. This183allows arbitrary nesting of scripts. As an example here is a script that184implements two commands for managing databases:185186.. click:example::187188 @click.group()189 def cli():190 pass191192 @click.command()193 def initdb():194 click.echo('Initialized the database')195196 @click.command()197 def dropdb():198 click.echo('Dropped the database')199200 cli.add_command(initdb)201 cli.add_command(dropdb)202203As you can see, the :func:`group` decorator works like the :func:`command`204decorator, but creates a :class:`Group` object instead which can be given205multiple subcommands that can be attached with :meth:`Group.add_command`.206207For simple scripts, it's also possible to automatically attach and create a208command by using the :meth:`Group.command` decorator instead. The above209script can instead be written like this:210211.. click:example::212213 @click.group()214 def cli():215 pass216217 @cli.command()218 def initdb():219 click.echo('Initialized the database')220221 @cli.command()222 def dropdb():223 click.echo('Dropped the database')224

Merge pull request #1117 from sirosen/docs/tweak-quickstartDavid Lordbefore adoption6e410dc

lines 225–230why, one step back
225You would then invoke the :class:`Group` in your setuptools entry points or
226other invocations::227228 if __name__ == '__main__':229 cli()230

Added BadArgumentUsage to docsArmin Ronacherbefore adoption50cf345

lines 231–277why, one step back
231Adding Parameters
232-----------------233234To add parameters, use the :func:`option` and :func:`argument` decorators:235236.. click:example::237238 @click.command()239 @click.option('--count', default=1, help='number of greetings')240 @click.argument('name')241 def hello(count, name):242 for x in range(count):243 click.echo('Hello %s!' % name)244245What it looks like:246247.. click:run::248249 invoke(hello, args=['--help'], prog_name='python hello.py')250251.. _switching-to-setuptools:252253Switching to Setuptools254-----------------------255256In the code you wrote so far there is a block at the end of the file which257looks like this: ``if __name__ == '__main__':``. This is traditionally258how a standalone Python file looks like. With Click you can continue259doing that, but there are better ways through setuptools.260261There are two main (and many more) reasons for this:262263The first one is that setuptools automatically generates executable264wrappers for Windows so your command line utilities work on Windows too.265266The second reason is that setuptools scripts work with virtualenv on Unix267without the virtualenv having to be activated. This is a very useful268concept which allows you to bundle your scripts with all requirements into269a virtualenv.270271Click is perfectly equipped to work with that and in fact the rest of the272documentation will assume that you are writing applications through273setuptools.274275I strongly recommend to have a look at the :ref:`setuptools-integration`276chapter before reading the rest as the examples assume that you will277be using setuptools.