Skip to content
Myr

advanced.rst

This file

Path

docs/advanced.rst

162 lines · 5.7 KiB

Last changed by

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

History of this file

Take it

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

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 1–10why, one step back
1Advanced Patterns
2=================34.. currentmodule:: click56In addition to common functionality that is implemented in the library7itself, there are countless of patterns that can be implemented by extending8click. This page should give some inspiration of what can be9accomplished.10

Added notes on no auto correctionArmin Ronacherbefore adoption72d26f6

lines 11–12why, one step back
11.. _aliases:
12

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 13–15why, one step back
13Command Aliases
14---------------15

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

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

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 17–23why, one step back
17``git`` to accept ``git ci`` as alias for ``git commit``. Other tools
18also support auto discovery for aliases by automatically shortening them.1920Click does not support this out of the box but it's very easy to customize21the :class:`Group` or any other :class:`MultiCommand` to provide this22functionality.23

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

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

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 25–30why, one step back
25two methods: :meth:`~MultiCommand.list_commands` and
26:meth:`~MultiCommand.get_command`. In this particular case you only need27to override the latter as you generally don't want to enumerate the28aliases on the help page to avoid confusion.2930This following example implements a subclass of :class:`Group` that

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

lines 31–32why, one step back
31accepts a prefix for a command. If there is a command called
32``push`` it would accept ``pus`` as an alias if it's unique:

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 33–49why, one step back
33
34.. click:example::3536 class AliasedGroup(click.Group):3738 def get_command(self, ctx, cmd_name):39 rv = click.Group.get_command(self, ctx, cmd_name)40 if rv is not None:41 return rv42 matches = [x for x in self.list_commands(ctx)43 if x.startswith(cmd_name)]44 if not matches:45 return None46 elif len(matches) == 1:47 return click.Group.get_command(self, ctx, matches[0])48 ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))49

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

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

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 51–68why, one step back
51
52.. click:example::5354 @click.command(cls=AliasedGroup)55 def cli():56 pass5758 @cli.command()59 def push():60 pass6162 @cli.command()63 def pop():64 pass6566Invoking Other Commands67-----------------------68

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

lines 69–70why, one step back
69Sometimes, it might be interesting to invoke one command from another
70command. This is a pattern that is generally discouraged with click but

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 71–73why, one step back
71possible nonetheless. For this you can use the :func:`Context.invoke`
72or :func:`Context.forward` methods.73

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

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

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

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

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

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

Properly skip empty second parts in dlsArmin Ronacherbefore adoptionfd9c73c

lines 79–103why, one step back
79
80Example:8182.. click:example::8384 cli = click.Group()8586 @cli.command()87 @click.option('--count', default=1)88 def test(count):89 click.echo('Count: %d' % count)9091 @cli.command()92 @click.option('--count', default=1)93 @click.pass_context94 def dist(ctx, count):95 ctx.forward(test)96 ctx.invoke(test, count=42)9798And what it looks like:99100.. click:run::101102 invoke(cli, prog_name='cli', args=['dist'])103

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 104–110why, one step back
104
105.. _callback-evaluation-order:106107Callback Evaluation Order108-------------------------109110Click works a bit different than some other command line parsers in that

Merge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a

line 111why, one step back
111it attempts to reconcile the order of arguments as defined by the

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 112–120why, one step back
112programmer with the order of arguments as defined by the user before
113invoking any callbacks.114115This is an important concept to understand when implementing complex116patterns ported from optparse or other systems to click. A parameter117callback invocation in optparse happens as part of the parsing step118whereas a callback invocation in click happens after the parsing.119120The main difference is that in optparse callbacks are invoked with the raw

Merge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a

lines 121–122why, one step back
121value as it happens, whereas a callback in click is invoked after the
122value has been fully converted.

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

line 123why, one step back
123

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

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

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 128–136why, one step back
128
129There are two exceptions to this rule which are important to know:130131Eagerness:132 An option can be set to be "eager". All eager parameters are133 evaluated before all non-eager parameters, but again in the order as134 they were provided on the command line by the user.135136 This is important for parameters that execute and exit like ``--help``

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

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

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 138–142why, one step back
138 comes first on the command line will win and exit the program.
139140Repeated parameters:141 If an option or argument is split up on the command line into multiple142 places because it's repeated (for instance ``--exclude foo --include

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

lines 143–144why, one step back
143 baz --exclude bar``), the callback will fire based on the position of
144 the first option. In this case, the callback will fire for

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 145–148why, one step back
145 ``exclude`` and it will be passed both options (``foo`` and
146 ``bar``), then the callback for ``include`` will fire with ``baz``147 only.148

Merge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a

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

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

line 150why, one step back
150 will still accept the position of the first, but it will ignore every

Merge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a

lines 151–152why, one step back
151 value except the first. The reason for this is to allow composability
152 through shell aliases that set defaults.

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 153–154why, one step back
153
154Missing parameters:

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

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

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

line 156why, one step back
156 still fire. This is different from how it works in optparse where

Merge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a

line 157why, one step back
157 undefined values do not fire the callback. Missing parameters fire

Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65

lines 158–162why, one step back
158 their callbacks at the very end which makes it possible for them to
159 default to values from a parameter that came before.160161Most of the time you don't need to be concerned about any of this stuff,162but it's important to know how it works for some advanced cases.