Skip to content
Myr

Implemented basic command collections

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 51ebc82 · Browse files · 4 files+85 added−4 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+85 added−4 removed

  • other:+85 added−4 removed

4 files+85 added−4 removed

modified:click/__init__.py+4 added-4 removed
13 unmodified lines
14 :license: BSD, see LICENSE for more details.15"""1617# Core classes18from .core import Context, Command, MultiCommand, Group, Parameter, \19 Option, Argument18from .core import Context, Command, MultiCommand, Group, CommandCollection, \19 Parameter, Option, Argument2021# Decorators22from .decorators import pass_context, pass_obj, make_pass_decorator, \23 command, group, argument, option, confirmation_option, \
10 unmodified lines
343536__all__ = [37 # Core classes38 'Context', 'Command', 'MultiCommand', 'Group', 'Parameter',39 'Option', 'Argument',38 'Context', 'Command', 'MultiCommand', 'Group', 'CommandCollection',39 'Parameter', 'Option', 'Argument',4041 # Decorators42 'pass_context', 'pass_obj', 'make_pass_decorator', 'command', 'group',43 'argument', 'option', 'confirmation_option', 'password_option',
11 unmodified lines
modified:click/core.py+29 added
469 unmodified lines
470 def list_commands(self, ctx):471 return sorted(self.commands)472473474class CommandCollection(MultiCommand):475 """A command collection is a multi command that merges multiple multi476 commands together into one. This is a straightforward implementation477 that accepts a list of different multi commands as sources and478 provides all the commands for each of them.479 """480481 def __init__(self, name=None, sources=None, **attrs):482 MultiCommand.__init__(self, name, **attrs)483 #: The list of registered multi commands.484 self.sources = sources or []485486 def add_source(self, multi_cmd):487 """Adds a new multi command to the chain dispatcher."""488 self.sources.append(multi_cmd)489490 def get_command(self, ctx, cmd_name):491 for source in self.sources:492 rv = source.get_command(ctx, cmd_name)493 if rv is not None:494 return rv495496 def list_commands(self, ctx):497 rv = set()498 for source in self.sources:499 rv.update(source.list_commands(ctx))500 return sorted(rv)501502503class Parameter(object):504 """A parameter to a command comes in two versions: they are either505 :class:`Option`\s or :class:`Argument`\s. Other subclasses are currently506 not supported by design as some of the internals for parsing are
397 unmodified lines
modified:docs/api.rst+3 added
50 unmodified lines
5152.. autoclass:: Group53 :members:5455.. autoclass:: CommandCollection56 :members:5758Parameters59----------6061.. autoclass:: Parameter
35 unmodified lines
modified:docs/commands.rst+49 added
203 unmodified lines
204205 @click.command(cls=MyCLI)206 def cli():207 pass208209Merging Multi Commands210----------------------211212In addition to implementing custom multi commands it can also be213interesting to merge multiple together into one script. While this is214generally not as recommended as nesting one below the other the merging215approach can be useful in some circumstances for a nicer shell experience.216217A default implementation for such a merging system is the218:class:`CommandCollection` class. It accepts a list of other multi219commands and makes the commands available on the same class class. It220accepts a list of other multi commands and makes the commands available on221the same level.222223Example usage:224225.. click:example::226227 import click228229 @click.group()230 def cli1():231 pass232233 @cli1.command()234 def cmd1():235 """Command on cli1"""236237 @click.group()238 def cli2():239 pass240241 @cli1.command()242 def cmd2():243 """Command on cli2"""244245 cli = click.CommandCollection(sources=[cli1, cli2])246247 if __name__ == '__main__':248 cli()249250And what it looks like:251252.. click:run::253254 invoke(cli, prog_name='cli', args=['--help'])255256In case a command exists on more than one source, the first source wins.