advanced.rst
This file
Path
docs/advanced.rst
190 lines · 6.6 KiB
Last changed by
Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
History of this fileTake it
The walk stopped after 200 commits: lines older than ca1dff9 point at it.
Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 1–6why, one step back1Advanced Patterns2=================34.. currentmodule:: click56In addition to common functionality that is implemented in the libraryMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 7–8why, one step back7itself, there are countless patterns that can be implemented by extending8click. This page should give some insight into what can be accomplished.Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 9–14why, one step back910.. _aliases:1112Command Aliases13---------------14Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 15why, one step back15Many tools support aliases for commands. For instance, you can configureUpdated repo script a bitArmin Ronacherbefore adoptionca1dff9
line 16why, one step back16``git`` to accept ``git ci`` as alias for ``git commit``. Other toolsMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 17why, one step back17also support auto-discovery for aliases by automatically shortening them.Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
line 18why, one step back18Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 19why, one step back19Click does not support this out of the box, but it's very easy to customizeUpdated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 20–22why, one step back20the :class:`Group` or any other :class:`MultiCommand` to provide this21functionality.22Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 23why, one step back23As explained in :ref:`custom-multi-commands`, a multi command can provideUpdated repo script a bitArmin Ronacherbefore adoptionca1dff9
line 24why, one step back24two methods: :meth:`~MultiCommand.list_commands` andDocumentation fixesKevin Yapbefore adoption67523af
line 25why, one step back25:meth:`~MultiCommand.get_command`. In this particular case, you only needUpdated repo script a bitArmin Ronacherbefore adoptionca1dff9
line 26why, one step back26to override the latter as you generally don't want to enumerate theDocumentation fixesKevin Yapbefore adoption67523af
line 27why, one step back27aliases on the help page in order to avoid confusion.Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 28–29why, one step back2829This following example implements a subclass of :class:`Group` thatMerge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 30–31why, one step back30accepts 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):Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 32–48why, one step back3233.. 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)))48Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 49why, one step back49And it can then be used like this:Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 50–64why, one step back5051.. 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 pass64Added example for token normalization.Armin Ronacherbefore adoption8da75f9
lines 65–69why, one step back65Token Normalization66-------------------6768.. versionadded:: 2.069Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 70–72why, one step back70Starting with click 2.0, it's possible to provide a function that is used71for normalizing tokens. Tokens are option names, choice values, or command72values. This can be used to implement case insensitive options, forAdded example for token normalization.Armin Ronacherbefore adoption8da75f9
lines 73–74why, one step back73instance.74Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
lines 75–77why, one step back75In order to use this feature, the context needs to be passed a function that76performs 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 back7879.. click:example::80defaults -> settingsArmin Ronacherbefore adoption1bc0a19
line 81why, one step back81 CONTEXT_SETTINGS = dict(token_normalize_func=lambda x: x.lower())Added example for token normalization.Armin Ronacherbefore adoption8da75f9
line 82why, one step back82defaults -> settingsArmin Ronacherbefore adoption1bc0a19
line 83why, one step back83 @click.command(context_settings=CONTEXT_SETTINGS)Added example for token normalization.Armin Ronacherbefore adoption8da75f9
lines 84–87why, one step back84 @click.option('--name', default='Pete')85 def cli(name):86 click.echo('Name: %s' % name)87Merge pull request #125 from iKevinY/doc-fixesArmin Ronacherbefore adoption1d25afd
line 88why, one step back88And how it works on the command line:Added example for token normalization.Armin Ronacherbefore adoption8da75f9
lines 89–93why, one step back8990.. click:run::9192 invoke(cli, prog_name='cli', args=['--NAME=Pete'])93Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 94–96why, one step back94Invoking Other Commands95-----------------------96Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 97why, one step back97Sometimes, it might be interesting to invoke one command from anotherDocumentation fixesKevin Yapbefore adoption67523af
lines 98–99why, one step back98command. This is a pattern that is generally discouraged with click, but99possible nonetheless. For this, you can use the :func:`Context.invoke`Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 100–101why, one step back100or :func:`Context.forward` methods.101Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 102why, one step back102They work similarly, but the difference is that :func:`Context.invoke` merelyUpdated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 103–104why, one step back103invokes another command with the arguments you provide as a caller,104whereas :func:`Context.forward` fills in the arguments from the currentMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 105–106why, one step back105command. Both accept the command as the first argument and everything else106is passed onwards as you would expect.Updated repo script a bitArmin Ronacherbefore adoptionca1dff9
lines 107–131why, one step back107108Example: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'])131Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 132–137why, one step back132133.. _callback-evaluation-order:134135Callback Evaluation Order136-------------------------137Documentation fixesKevin Yapbefore adoption67523af
line 138why, one step back138Click works a bit differently than some other command line parsers in thatMerge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a
line 139why, one step back139it attempts to reconcile the order of arguments as defined by theAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 140–142why, one step back140programmer with the order of arguments as defined by the user before141invoking any callbacks.142Documentation fixesKevin Yapbefore adoption67523af
lines 143–145why, one step back143This is an important concept to understand when porting complex144patterns to click from optparse or other systems. A parameter145callback invocation in optparse happens as part of the parsing step,Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 146–147why, one step back146whereas a callback invocation in click happens after the parsing.147Documentation fixesKevin Yapbefore adoption67523af
line 148why, one step back148The main difference is that in optparse, callbacks are invoked with the rawMerge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a
lines 149–150why, one step back149value as it happens, whereas a callback in click is invoked after the150value has been fully converted.Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
line 151why, one step back151Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
lines 152–155why, one step back152Generally, the order of invocation is driven by the order in which the user153provides 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``.Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 156–164why, one step back156157There 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 back165 and ``--version``. Both are eager parameters, but whatever parameterAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 166–169why, one step back166 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 multipleDocumentation fixesKevin Yapbefore adoption67523af
lines 170–171why, one step back170 places because it is repeated -- for instance, ``--exclude foo --include171 baz --exclude bar`` -- the callback will fire based on the position ofMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 172why, one step back172 the first option. In this case, the callback will fire forAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 173–176why, one step back173 ``exclude`` and it will be passed both options (``foo`` and174 ``bar``), then the callback for ``include`` will fire with ``baz``175 only.176Merge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a
line 177why, one step back177 Note that even if a parameter does not allow multiple versions, clickAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
line 178why, one step back178 will still accept the position of the first, but it will ignore everyMerge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a
lines 179–180why, one step back179 value except the first. The reason for this is to allow composability180 through shell aliases that set defaults.Added strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 181–182why, one step back181182Missing parameters:Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 183why, one step back183 If a parameter is not defined on the command line, the callback willAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
line 184why, one step back184 still fire. This is different from how it works in optparse whereMerge pull request #74 from julen/doc-fixesArmin Ronacherbefore adoption814f50a
line 185why, one step back185 undefined values do not fire the callback. Missing parameters fireAdded strong support for a defined parameter callback evaluation order.Armin Ronacherbefore adoption5381d65
lines 186–188why, one step back186 their callbacks at the very end which makes it possible for them to187 default to values from a parameter that came before.188Documentation fixesKevin Yapbefore adoption67523af
lines 189–190why, one step back189Most 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.