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
Provenance
- Commit
e2d388e- Tree
a182d6d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
9eacc90
4 files changed+79 added−9 removed
added:docs/advanced.rst+56 added
1+Advanced Patterns2+=================3+4+In addition to common functionality that is implemented in the library5+itself, there are countless of patterns that can be implemented by extending6+click. This page should give some inspiration of what can be7+accomplished.8+9+Command Aliases10+---------------11+12+Many tools support aliases for commands. For instance you can configure13+``git`` to accept ``git ci`` as alias for ``git commit``. Other tools14+also support auto discovery for aliases by automatically shortening them.15+16+Click does not support this out of the box but it's very easy to customize17+the :class:`Group` or any other :class:`MultiCommand` to provide this18+functionality.19+20+As explained in :ref:`custom-multi-commands` a multi command can provide21+two methods: :meth:`~MultiCommand.list_commands` and22+:meth:`~MultiCommand.get_command`. In this particular case you only need23+to override the latter as you generally don't want to enumerate the24+aliases on the help page to avoid confusion.25+26+This following example implements a subclass of :class:`Group` that27+accepts a prefix for a command. So if there is a command called28+``push`` it would accept ``pus`` as alias if it's unique::29+30+ class AliasedGroup(click.Group):31+32+ 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)))43+44+And it can be used like this then::45+46+ @click.command(cls=AliasedGroup)47+ def cli():48+ pass49+50+ @cli.command()51+ def push():52+ pass53+54+ @cli.command()55+ def pop():56+ passmodified:docs/commands.rst+8 added
167 unmodified lines168 $ python tool.py sync169 I am about to invoke sync170 The subcommand171 172+.. _custom-multi-commands:173+174 Custom Multi Commands175 ---------------------176 177 In addition to using :func:`click.group` you can also build your own29 unmodified lines207 'plugin folder dynamically.')208 209 if __name__ == '__main__':210 cli()211+212+These custom classes can also be used with decorators::213+214+ @click.command(cls=MyCLI)215+ def cli():216+ passmodified:docs/index.rst+1 added
51 unmodified lines52 prompts53 documentation54 setuptools55 complex56+ advanced57 58 API Reference59 -------------60 7 unmodified linesmodified:click.py+14 added-9 removed
630 unmodified lines631 """632 allow_extra_args = True633 634 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_help723 unmodified lines1364 return cls(name=name or f.__name__.lower(),1365 callback=f, params=params, **attrs)1366 1367 1368-def command(name=None, **attrs):1368+def 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 lines1378 1379 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`.1382+1383+ :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 decorator1393 1394 1395 def 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)1401 1402 1403 def _param_memo(f, param):1404 if isinstance(f, Command):252 unmodified lines