Skip to content
Myr

advanced.rst

This file

Path

docs/advanced.rst

190 lines · 6.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 002c38d point at it.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 1–6why, one step back
1Advanced Patterns
2=================34.. currentmodule:: click56In addition to common functionality that is implemented in the library

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

line 7why, one step back
7itself, there are countless patterns that can be implemented by extending

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

line 8why, one step back
8Click. This page should give some insight into what can be accomplished.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 9–14why, one step back
9
10.. _aliases:1112Command Aliases13---------------14

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

line 15why, one step back
15Many tools support aliases for commands. For instance, you can configure

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

line 16why, one step back
16``git`` to accept ``git ci`` as alias for ``git commit``. Other tools

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

line 17why, one step back
17also support auto-discovery for aliases by automatically shortening them.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

line 18why, one step back
18

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

line 19why, one step back
19Click does not support this out of the box, but it's very easy to customize

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 20–22why, one step back
20the :class:`Group` or any other :class:`MultiCommand` to provide this
21functionality.22

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

line 23why, one step back
23As explained in :ref:`custom-multi-commands`, a multi command can provide

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

line 24why, one step back
24two methods: :meth:`~MultiCommand.list_commands` and

Documentation fixesKevin Yapbefore adoption67523af

line 25why, one step back
25:meth:`~MultiCommand.get_command`. In this particular case, you only need

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

line 26why, one step back
26to override the latter as you generally don't want to enumerate the

Documentation fixesKevin Yapbefore adoption67523af

line 27why, one step back
27aliases on the help page in order to avoid confusion.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 28–29why, one step back
28
29This following example implements a subclass of :class:`Group` that

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

lines 30–31why, one step back
30accepts a prefix for a command. If there were a command called ``push``,
31it would accept ``pus`` as an alias (so long as it was unique):

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 32–48why, one step back
32
33.. click:example::3435 class AliasedGroup(click.Group):3637 def get_command(self, ctx, cmd_name):38 rv = click.Group.get_command(self, ctx, cmd_name)39 if rv is not None:40 return rv41 matches = [x for x in self.list_commands(ctx)42 if x.startswith(cmd_name)]43 if not matches:44 return None45 elif len(matches) == 1:46 return click.Group.get_command(self, ctx, matches[0])47 ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))48

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

line 49why, one step back
49And it can then be used like this:

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 50–64why, one step back
50
51.. click:example::5253 @click.command(cls=AliasedGroup)54 def cli():55 pass5657 @cli.command()58 def push():59 pass6061 @cli.command()62 def pop():63 pass64

Added example for token normalization.Armin Ronacherbefore adoption8da75f9

lines 65–69why, one step back
65Token Normalization
66-------------------6768.. versionadded:: 2.069

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

line 70why, one step back
70Starting with Click 2.0, it's possible to provide a function that is used

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

lines 71–72why, one step back
71for normalizing tokens. Tokens are option names, choice values, or command
72values. This can be used to implement case insensitive options, for

Added example for token normalization.Armin Ronacherbefore adoption8da75f9

lines 73–74why, one step back
73instance.
74

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

lines 75–77why, one step back
75In order to use this feature, the context needs to be passed a function that
76performs the normalization of the token. For instance, you could have a77function that converts the token to lowercase:

Added example for token normalization.Armin Ronacherbefore adoption8da75f9

lines 78–80why, one step back
78
79.. click:example::80

defaults -> settingsArmin Ronacherbefore adoption1bc0a19

line 81why, one step back
81 CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())

Added example for token normalization.Armin Ronacherbefore adoption8da75f9

line 82why, one step back
82

defaults -> settingsArmin Ronacherbefore adoption1bc0a19

line 83why, one step back
83 @click.command(context_settings=CONTEXT_SETTINGS)

Added example for token normalization.Armin Ronacherbefore adoption8da75f9

lines 84–87why, one step back
84 @click.option('--name', default='Pete')
85 def cli(name):86 click.echo('Name: %s' % name)87

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

line 88why, one step back
88And how it works on the command line:

Added example for token normalization.Armin Ronacherbefore adoption8da75f9

lines 89–93why, one step back
89
90.. click:run::9192 invoke(cli, prog_name='cli', args=['--NAME=Pete'])93

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 94–96why, one step back
94Invoking Other Commands
95-----------------------96

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

line 97why, one step back
97Sometimes, it might be interesting to invoke one command from another

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

line 98why, one step back
98command. This is a pattern that is generally discouraged with Click, but

Documentation fixesKevin Yapbefore adoption67523af

line 99why, one step back
99possible nonetheless. For this, you can use the :func:`Context.invoke`

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 100–101why, one step back
100or :func:`Context.forward` methods.
101

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

line 102why, one step back
102They work similarly, but the difference is that :func:`Context.invoke` merely

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 103–104why, one step back
103invokes another command with the arguments you provide as a caller,
104whereas :func:`Context.forward` fills in the arguments from the current

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

lines 105–106why, one step back
105command. Both accept the command as the first argument and everything else
106is passed onwards as you would expect.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 107–137why, one step back
107
108Example:109110.. click:example::111112 cli = click.Group()113114 @cli.command()115 @click.option('--count', default=1)116 def test(count):117 click.echo('Count: %d' % count)118119 @cli.command()120 @click.option('--count', default=1)121 @click.pass_context122 def dist(ctx, count):123 ctx.forward(test)124 ctx.invoke(test, count=42)125126And what it looks like:127128.. click:run::129130 invoke(cli, prog_name='cli', args=['dist'])131132133.. _callback-evaluation-order:134135Callback Evaluation Order136-------------------------137

Documentation fixesKevin Yapbefore adoption67523af

line 138why, one step back
138Click works a bit differently than some other command line parsers in that

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 139–142why, one step back
139it attempts to reconcile the order of arguments as defined by the
140programmer with the order of arguments as defined by the user before141invoking any callbacks.142

Documentation fixesKevin Yapbefore adoption67523af

line 143why, one step back
143This is an important concept to understand when porting complex

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

line 144why, one step back
144patterns to Click from optparse or other systems. A parameter

Documentation fixesKevin Yapbefore adoption67523af

line 145why, one step back
145callback invocation in optparse happens as part of the parsing step,

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

line 146why, one step back
146whereas a callback invocation in Click happens after the parsing.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

line 147why, one step back
147

Documentation fixesKevin Yapbefore adoption67523af

line 148why, one step back
148The main difference is that in optparse, callbacks are invoked with the raw

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

line 149why, one step back
149value as it happens, whereas a callback in Click is invoked after the

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 150–151why, one step back
150value has been fully converted.
151

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

lines 152–155why, one step back
152Generally, the order of invocation is driven by the order in which the user
153provides the arguments to the script; if there is an option called ``--foo``154and an option called ``--bar`` and the user calls it as ``--bar155--foo``, then the callback for ``bar`` will fire before the one for ``foo``.

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 156–164why, one step back
156
157There are two exceptions to this rule which are important to know:158159Eagerness:160 An option can be set to be "eager". All eager parameters are161 evaluated before all non-eager parameters, but again in the order as162 they were provided on the command line by the user.163164 This is important for parameters that execute and exit like ``--help``

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

line 165why, one step back
165 and ``--version``. Both are eager parameters, but whatever parameter

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 166–169why, one step back
166 comes first on the command line will win and exit the program.
167168Repeated parameters:169 If an option or argument is split up on the command line into multiple

Documentation fixesKevin Yapbefore adoption67523af

lines 170–171why, one step back
170 places because it is repeated -- for instance, ``--exclude foo --include
171 baz --exclude bar`` -- the callback will fire based on the position of

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

line 172why, one step back
172 the first option. In this case, the callback will fire for

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 173–176why, one step back
173 ``exclude`` and it will be passed both options (``foo`` and
174 ``bar``), then the callback for ``include`` will fire with ``baz``175 only.176

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

line 177why, one step back
177 Note that even if a parameter does not allow multiple versions, Click

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 178–182why, one step back
178 will still accept the position of the first, but it will ignore every
179 value except the first. The reason for this is to allow composability180 through shell aliases that set defaults.181182Missing parameters:

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

line 183why, one step back
183 If a parameter is not defined on the command line, the callback will

Improved stream support and added it to public API.Armin Ronacherbefore adoption002c38d

lines 184–188why, one step back
184 still fire. This is different from how it works in optparse where
185 undefined values do not fire the callback. Missing parameters fire186 their callbacks at the very end which makes it possible for them to187 default to values from a parameter that came before.188

Documentation fixesKevin Yapbefore adoption67523af

lines 189–190why, one step back
189Most of the time you do not need to be concerned about any of this,
190but it is important to know how it works for some advanced cases.