Skip to content
Myr

advanced.rst

This file

Path

docs/advanced.rst

119 lines · 3.5 KiB

Last changed by

Added notes on no auto correctionArmin Ronacherbefore adoption72d26f6

History of this file

Take it

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

lines 1–3why, one step back
1Advanced Patterns
2=================3

Added a missing currentmodule directiveArmin Ronacherbefore adoptiond5a6bd4

lines 4–5why, one step back
4.. currentmodule:: click
5

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

lines 6–10why, one step back
6In addition to common functionality that is implemented in the library
7itself, 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

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

lines 13–31why, one step back
13Command Aliases
14---------------1516Many tools support aliases for commands. For instance you can configure17``git`` to accept ``git ci`` as alias for ``git commit``. Other tools18also 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.2324As explained in :ref:`custom-multi-commands` a multi command can provide25two methods: :meth:`~MultiCommand.list_commands` and26: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` that31accepts a prefix for a command. So if there is a command called

More automated docsArmin Ronacherbefore adoption8254b79

line 32why, one step back
32``push`` it would accept ``pus`` as alias if it's unique:

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

line 33why, one step back
33

More automated docsArmin Ronacherbefore adoption8254b79

lines 34–35why, one step back
34.. click:example::
35

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

lines 36–41why, one step back
36 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 rv

Merge pull request #55 from fgimian/aliased-group-typoArmin Ronacherbefore adoption0327cab

line 42why, one step back
42 matches = [x for x in self.list_commands(ctx)

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

lines 43–49why, one step back
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

More automated docsArmin Ronacherbefore adoption8254b79

lines 50–52why, one step back
50And it can be used like this then:
5152.. click:example::

Added an example for aliasesArmin Ronacherbefore adoptione2d388e

lines 53–64why, one step back
53
54 @click.command(cls=AliasedGroup)55 def cli():56 pass5758 @cli.command()59 def push():60 pass6162 @cli.command()63 def pop():64 pass

Moved the command invoke docsArmin Ronacherbefore adoption3ab47e9

lines 65–102why, one step back
65
66Invoking Other Commands67-----------------------6869Sometimes it might be interesting to invoke one command from another70command. This is generally a pattern that is discouraged with click but71possible nonetheless. For this you can use the :func:`Context.invoke`72or :func:`Context.forward` methods.7374They work similar but the difference is that :func:`Context.invoke` merely75invokes another command with the arguments you provide as a caller,76whereas :func:`Context.forward` fills in the arguments from the current77command. Both accept the command as first argument and everything else is78passed onwards as you would expect.7980Example: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'])

Added example for pagerArmin Ronacherbefore adoptiondc25c8b

lines 103–119why, one step back
103
104Showing Long Texts105------------------106107In some situations you might want to show long texts on the terminal and108let a user scroll through it. This can be achieved by using the109:func:`echo_via_pager` function which works similar to the :func:`echo`110function but always writes to stdout and, if possible, through a pager.111112Example:113114.. click:example::115116 @click.command()117 def less():118 click.echo_via_pager('\n'.join('Line %d' % idx119 for idx in range(200)))