Skip to content
Myr

complex.rst

This file

Path

docs/complex.rst

219 lines · 7.6 KiB

Last changed by

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

History of this file

Take it

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

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 1–7why, one step back
1.. _complex-guide:
23Complex Applications4====================56.. currentmodule:: click7

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

lines 8–9why, one step back
8Click is designed to assist with the creation of complex and simple CLI tools
9alike. However, the power of its design is the ability to arbitrarily nest

Documentation fixesKevin Yapbefore adoption67523af

lines 10–13why, one step back
10systems together. For instance, if you have ever used Django, you will
11have realized that it provides a command line utility, but so does Celery.12When using Celery with Django, there are two tools that need to interact with13each other and be cross-configured.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 14why, one step back
14

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

line 15why, one step back
15In a theoretical world of two separate Click command line utilities, they

Documentation fixesKevin Yapbefore adoption67523af

lines 16–17why, one step back
16could solve this problem by nesting one inside the other. For instance, the
17web framework could also load the commands for the message queue framework.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 18–21why, one step back
18
19Basic Concepts20--------------21

Documentation fixesKevin Yapbefore adoption67523af

line 22why, one step back
22To understand how this works, you need to understand two concepts: contexts

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 23–27why, one step back
23and the calling convention.
2425Contexts26````````27

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

line 28why, one step back
28Whenever a Click command is executed, a :class:`Context` object is created

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

lines 29–32why, one step back
29which holds state for this particular invocation. It remembers parsed
30parameters, what command created it, which resources need to be cleaned up31at the end of the function, and so forth. It can also optionally hold an32application-defined object.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 33why, one step back
33

Documentation fixesKevin Yapbefore adoption67523af

line 34why, one step back
34Context objects build a linked list until they hit the top one. Each context

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 35why, one step back
35is linked to a parent context. This allows a command to work below

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 36why, one step back
36another command and store its own information there without having to be

Documentation fixesKevin Yapbefore adoption67523af

line 37why, one step back
37afraid of altering up the state of the parent command.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 38why, one step back
38

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

line 39why, one step back
39Because the parent data is available, however, it is possible to navigate to

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 40–41why, one step back
40it if needed.
41

Documentation fixesKevin Yapbefore adoption67523af

line 42why, one step back
42Most of the time, you do not see the context object, but when writing more

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 43–47why, one step back
43complex applications it comes in handy. This brings us to the next point.
4445Calling Convention46``````````````````47

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

line 48why, one step back
48When a Click command callback is executed, it's passed all the non-hidden

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

line 49why, one step back
49parameters as keyword arguments. Notably absent is the context. However,

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 50why, one step back
50a callback can opt into being passed to the context object by marking itself

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 51–58why, one step back
51with :func:`pass_context`.
5253So how do you invoke a command callback if you don't know if it should54receive the context or not? The answer is that the context itself55provides a helper function (:meth:`Context.invoke`) which can do this for56you. It accepts the callback as first argument and then invokes the57function correctly.58

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

lines 59–60why, one step back
59Building a Git Clone
60--------------------

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 61why, one step back
61

Documentation fixesKevin Yapbefore adoption67523af

line 62why, one step back
62In this example, we want to build a command line tool that resembles a

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

line 63why, one step back
63version control system. Systems like Git usually provide one

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 64why, one step back
64over-arching command that already accepts some parameters and

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

line 65why, one step back
65configuration, and then have extra subcommands that do other things.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 66–69why, one step back
66
67The Root Command68````````````````69

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

lines 70–71why, one step back
70At the top level, we need a group that can hold all our commands. In this
71case, we use the basic :func:`click.group` which allows us to register

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

line 72why, one step back
72other Click commands below it.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 73why, one step back
73

Documentation fixesKevin Yapbefore adoption67523af

line 74why, one step back
74For this command, we also want to accept some parameters that configure the

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 75–97why, one step back
75state of our tool:
7677.. click:example::7879 import os80 import click818283 class Repo(object):84 def __init__(self, home=None, debug=False):85 self.home = os.path.abspath(home or '.')86 self.debug = debug878889 @click.group()90 @click.option('--repo-home', envvar='REPO_HOME', default='.repo')91 @click.option('--debug/--no-debug', default=False,92 envvar='REPO_DEBUG')93 @click.pass_context94 def cli(ctx, repo_home, debug):95 ctx.obj = Repo(repo_home, debug)9697

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 98why, one step back
98Let's understand what this does. We create a group command which can

Documentation fixesKevin Yapbefore adoption67523af

line 99why, one step back
99have subcommands. When it is invoked, it will create an instance of a

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 100why, one step back
100``Repo`` class. This holds the state for our command line tool. In this

Documentation fixesKevin Yapbefore adoption67523af

line 101why, one step back
101case, it just remembers some parameters, but at this point it could also

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

line 102why, one step back
102start loading configuration files and so on.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 103why, one step back
103

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

lines 104–106why, one step back
104This state object is then remembered by the context as :attr:`~Context.obj`.
105This is a special attribute where commands are supposed to remember what106they need to pass on to their children.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 107why, one step back
107

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

line 108why, one step back
108In order for this to work, we need to mark our function with

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 109why, one step back
109:func:`pass_context`, because otherwise, the context object would be

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 110–114why, one step back
110entirely hidden from us.
111112The First Child Command113```````````````````````114

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

line 115why, one step back
115Let's add our first child command to it, the clone command:

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 116–125why, one step back
116
117.. click:example::118119 @cli.command()120 @click.argument('src')121 @click.argument('dest', required=False)122 def clone(src, dest):123 pass124125So now we have a clone command, but how do we get access to the repo? As

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

line 126why, one step back
126you can imagine, one way is to use the :func:`pass_context` function which

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 127why, one step back
127again will make our callback also get the context passed on which we

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

lines 128–130why, one step back
128memorized the repo. However, there is a second version of this decorator
129called :func:`pass_obj` which will just pass the stored object, (in our case130the repo):

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 131–143why, one step back
131
132.. click:example::133134 @cli.command()135 @click.argument('src')136 @click.argument('dest', required=False)137 @click.pass_obj138 def clone(repo, src, dest):139 pass140141Interleaved Commands142````````````````````143

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

lines 144–145why, one step back
144While not relevant for the particular program we want to build, there is
145also quite good support for interleaving systems. Imagine for instance that

Documentation fixesKevin Yapbefore adoption67523af

lines 146–147why, one step back
146there was a super cool plugin for our version control system that needed a
147lot of configuration and wanted to store its own configuration as

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 148why, one step back
148:attr:`~Context.obj`. If we would then attach another command below that,

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

lines 149–150why, one step back
149we would all of a sudden get the plugin configuration instead of our repo
150object.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 151why, one step back
151

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

lines 152–154why, one step back
152One obvious way to remedy this is to store a reference to the repo in the
153plugin, but then a command needs to be aware that it's attached below such a154plugin.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 155why, one step back
155

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

lines 156–158why, one step back
156There is a much better system that can built by taking advantage of the linked
157nature of contexts. We know that the plugin context is linked to the context158that created our repo. Because of that, we can start a search for the last

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 159why, one step back
159level where the object stored by the context was a repo.

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 160–161why, one step back
160
161Built-in support for this is provided by the :func:`make_pass_decorator`

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

lines 162–164why, one step back
162factory, which will create decorators for us that find objects (it
163internally calls into :meth:`Context.find_object`). In our case, we164know that we want to find the closest ``Repo`` object, so let's make a

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 165–170why, one step back
165decorator for this:
166167.. click:example::168169 pass_repo = click.make_pass_decorator(Repo)170

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

line 171why, one step back
171If we now use ``pass_repo`` instead of ``pass_obj``, we will always get a

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 172–182why, one step back
172repo instead of something else:
173174.. click:example::175176 @cli.command()177 @click.argument('src')178 @click.argument('dest', required=False)179 @pass_repo180 def clone(repo, src, dest):181 pass182

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

lines 183–184why, one step back
183Ensuring Object Creation
184````````````````````````

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 185–186why, one step back
185
186The above example only works if there was an outer command that created a

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 187why, one step back
187``Repo`` object and stored it in the context. For some more advanced use

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

lines 188–190why, one step back
188cases, this might become a problem. The default behavior of
189:func:`make_pass_decorator` is to call :meth:`Context.find_object`190which will find the object. If it can't find the object, it will raise an

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 191why, one step back
191error. The alternative behavior is to use :meth:`Context.ensure_object`

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

line 192why, one step back
192which will find the object, and if it cannot find it, will create one and

Merge pull request #113 from iKevinY/doc-fixesArmin Ronacherbefore adoptione0ea94a

line 193why, one step back
193store it in the innermost context. This behavior can also be enabled for

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 194–199why, one step back
194:func:`make_pass_decorator` by passing ``ensure=True``:
195196.. click:example::197198 pass_repo = click.make_pass_decorator(Repo, ensure=True)199

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

line 200why, one step back
200In this case, the innermost context gets an object created if it is

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

line 201why, one step back
201missing. This might replace objects being placed there earlier. In this

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

lines 202–203why, one step back
202case, the command stays executable, even if the outer command does not run.
203For this to work, the object type needs to have a constructor that accepts

Implemented atomic file opening support.Armin Ronacherbefore adoption469ba25

lines 204–219why, one step back
204no arguments.
205206As such it runs standalone:207208.. click:example::209210 @click.command()211 @pass_repo212 def cp(repo):213 click.echo(repo)214215As you can see:216217.. click:run::218219 invoke(cp, [])