Skip to content
Myr

Added an example for aliases

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 9eacc90 · Browse files · 4 files+79 added−9 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+79 added−9 removed

  • other:+79 added−9 removed

4 files+79 added−9 removed

added:docs/advanced.rst+56 added
1Advanced Patterns2=================34In addition to common functionality that is implemented in the library5itself, there are countless of patterns that can be implemented by extending6click. This page should give some inspiration of what can be7accomplished.89Command Aliases10---------------1112Many tools support aliases for commands. For instance you can configure13``git`` to accept ``git ci`` as alias for ``git commit``. Other tools14also support auto discovery for aliases by automatically shortening them.1516Click does not support this out of the box but it's very easy to customize17the :class:`Group` or any other :class:`MultiCommand` to provide this18functionality.1920As explained in :ref:`custom-multi-commands` a multi command can provide21two methods: :meth:`~MultiCommand.list_commands` and22:meth:`~MultiCommand.get_command`. In this particular case you only need23to override the latter as you generally don't want to enumerate the24aliases on the help page to avoid confusion.2526This following example implements a subclass of :class:`Group` that27accepts a prefix for a command. So if there is a command called28``push`` it would accept ``pus`` as alias if it's unique::2930 class AliasedGroup(click.Group):3132 def get_command(self, ctx, cmd_name):33 rv = click.Group.get_command(self, ctx, cmd_name)34 if rv is not None:35 return rv36 matches = [x for x in self.list_commands()37 if x.startswith(cmd_name)]38 if not matches:39 return None40 elif len(matches) == 1:41 return click.Group.get_command(self, ctx, matches[0])42 ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))4344And it can be used like this then::4546 @click.command(cls=AliasedGroup)47 def cli():48 pass4950 @cli.command()51 def push():52 pass5354 @cli.command()55 def pop():56 pass
modified:docs/commands.rst+8 added
167 unmodified lines
168 $ python tool.py sync169 I am about to invoke sync170 The subcommand171172.. _custom-multi-commands:173174Custom Multi Commands175---------------------176177In addition to using :func:`click.group` you can also build your own
29 unmodified lines
207 'plugin folder dynamically.')208209 if __name__ == '__main__':210 cli()211212These custom classes can also be used with decorators::213214 @click.command(cls=MyCLI)215 def cli():216 pass
modified:docs/index.rst+1 added
51 unmodified lines
52 prompts53 documentation54 setuptools55 complex56 advanced5758API Reference59-------------60
7 unmodified lines
modified:click.py+14 added-9 removed
630 unmodified lines
631 """632 allow_extra_args = True633634 def __init__(self, name=None, invoke_without_command=False,635 no_args_is_help=None,636 subcommand_metavar='COMMAND [ARGS]...', **attrs):635 no_args_is_help=None, subcommand_metavar='COMMAND [ARGS]...',636 **attrs):637 Command.__init__(self, name, **attrs)638 if no_args_is_help is None:639 no_args_is_help = not invoke_without_command640 self.no_args_is_help = no_args_is_help
723 unmodified lines
1364 return cls(name=name or f.__name__.lower(),1365 callback=f, params=params, **attrs)136613671368def command(name=None, **attrs):1368def command(name=None, cls=None, **attrs):1369 """Creates a new :class:`Command` and uses the decorated function as1370 callback. This will also automatically attach all decorated1371 :func:`option`\s and :func:`argument`\s as paramters to the command.1372
5 unmodified lines
13781379 Once decorated the function turns into a :class:`Command` instance1380 that can be invoked as a command line utility or be attached to a1381 command :class:`Group`.13821383 :param name: the name of the command. This defaults to the function1384 name.1385 :param cls: the command class to instanciate. This defaults to1386 :class:`Command`.1387 """1388 if cls is None:1389 cls = Command1390 def decorator(f):1384 return _make_command(f, name, attrs, Command)1391 return _make_command(f, name, attrs, cls)1392 return decorator139313941395def group(name=None, **attrs):1396 """Creates a new :class:`Group` with a function as callback. This1390 works otherwise the same as :func:`command` just that the generated1391 class is different.1397 works otherwise the same as :func:`command` just that the `cls`1398 parameter is set to :class:`Group`.1399 """1393 def decorator(f):1394 return _make_command(f, name, attrs, Group)1395 return decorator1400 return command(name, cls=Group, **attrs)140114021403def _param_memo(f, param):1404 if isinstance(f, Command):
252 unmodified lines