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
Provenance
- Commit
055a8e6- Tree
a3cc3a2- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
51ebc82
4 files changed+85 added−4 removed
modified:click/__init__.py+4 added-4 removed
13 unmodified lines14 :license: BSD, see LICENSE for more details.15 """16 17 # Core classes18-from .core import Context, Command, MultiCommand, Group, Parameter, \19- Option, Argument18+from .core import Context, Command, MultiCommand, Group, CommandCollection, \19+ Parameter, Option, Argument20 21 # Decorators22 from .decorators import pass_context, pass_obj, make_pass_decorator, \23 command, group, argument, option, confirmation_option, \10 unmodified lines34 35 36 __all__ = [37 # Core classes38- 'Context', 'Command', 'MultiCommand', 'Group', 'Parameter',39- 'Option', 'Argument',38+ 'Context', 'Command', 'MultiCommand', 'Group', 'CommandCollection',39+ 'Parameter', 'Option', 'Argument',40 41 # Decorators42 'pass_context', 'pass_obj', 'make_pass_decorator', 'command', 'group',43 'argument', 'option', 'confirmation_option', 'password_option',11 unmodified linesmodified:click/core.py+29 added
469 unmodified lines470 def list_commands(self, ctx):471 return sorted(self.commands)472 473 474+class 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+ """480+481+ 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 []485+486+ def add_source(self, multi_cmd):487+ """Adds a new multi command to the chain dispatcher."""488+ self.sources.append(multi_cmd)489+490+ 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 rv495+496+ 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)501+502+503 class 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 are397 unmodified linesmodified:docs/api.rst+3 added
50 unmodified lines51 52 .. autoclass:: Group53 :members:54 55+.. autoclass:: CommandCollection56+ :members:57+58 Parameters59 ----------60 61 .. autoclass:: Parameter35 unmodified linesmodified:docs/commands.rst+49 added
203 unmodified lines204 205 @click.command(cls=MyCLI)206 def cli():207 pass208+209+Merging Multi Commands210+----------------------211+212+In addition to implementing custom multi commands it can also be213+interesting to merge multiple together into one script. While this is214+generally not as recommended as nesting one below the other the merging215+approach can be useful in some circumstances for a nicer shell experience.216+217+A default implementation for such a merging system is the218+:class:`CommandCollection` class. It accepts a list of other multi219+commands and makes the commands available on the same class class. It220+accepts a list of other multi commands and makes the commands available on221+the same level.222+223+Example usage:224+225+.. click:example::226+227+ import click228+229+ @click.group()230+ def cli1():231+ pass232+233+ @cli1.command()234+ def cmd1():235+ """Command on cli1"""236+237+ @click.group()238+ def cli2():239+ pass240+241+ @cli1.command()242+ def cmd2():243+ """Command on cli2"""244+245+ cli = click.CommandCollection(sources=[cli1, cli2])246+247+ if __name__ == '__main__':248+ cli()249+250+And what it looks like:251+252+.. click:run::253+254+ invoke(cli, prog_name='cli', args=['--help'])255+256+In case a command exists on more than one source, the first source wins.