Added support for command forwarding.
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 5fb4c01 · Browse files · 3 files·+89 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+89 added−0 removed
- tests:+18 added−0 removed
- other:+71 added−0 removed
Provenance
- Commit
506e4e7- Tree
8bf40a1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5fb4c01
3 files changed+89 added−0 removed
modified:click/core.py+33 added
163 unmodified lines164 def invoke(*args, **kwargs):165 """Invokes a command callback in exactly the way it expects.166 """167 self, callback = args[:2]168+169+ # It's also possible to invoke another command which might or170+ # might not have a callback.171+ if isinstance(callback, Command):172+ callback = callback.callback173+ if callback is None:174+ raise TypeError('The given command does not have a '175+ 'callback that can be invoked.')176+177 args = args[2:]178 if getattr(callback, '__click_pass_context__', False):179 args = (self,) + args180 return callback(*args, **kwargs)181 182+ def forward(*args, **kwargs):183+ """Similar to :meth:`forward` but fills in default keyword184+ arguments from the current context if the other command expects185+ it. This cannot invoke callbacks directly, only other commands.186+ """187+ self, cmd = args[:2]188 189+ # It's also possible to invoke another command which might or190+ # might not have a callback.191+ if not isinstance(cmd, Command):192+ raise TypeError('Callback is not a command.')193+194+ for param_name in cmd.iter_param_names():195+ if param_name in self.params and \196+ param_name not in kwargs:197+ kwargs[param_name] = self.params[param_name]198+199+ return self.invoke(cmd, **kwargs)200+201+202 class Command(object):203 """Commands are the basic building block of command line interfaces in204 click. A basic command handles command line parsing and might dispatch205 more parsing to commands nested blow it.51 unmodified lines257 for param in self.params:258 if not param.is_eager:259 yield param260 261+ def iter_param_names(self):262+ """Iterates over all parameter names."""263+ for param in self.iter_params_for_processing():264+ yield param.name265+266 def _make_parser(self, ctx):267 parser = _optparse._SimplifiedOptionParser(268 ctx, description=self.help, epilog=self.epilog)269 for param in self.params:666 unmodified linesmodified:docs/commands.rst+38 added
252 unmodified lines253 254 invoke(cli, prog_name='cli', args=['--help'])255 256 In case a command exists on more than one source, the first source wins.257+258+Invoking Other Commands259+-----------------------260+261+Sometimes it might be interesting to invoke one command from another262+command. This is generally a pattern that is discouraged with click but263+possible nonetheless. For this you can use the :func:`Context.invoke`264+or :func:`Context.forward` methods.265+266+They work similar but the difference is that :func:`Context.invoke` merely267+invokes another command with the arguments you provide as a caller,268+whereas :func:`Context.forward` fills in the arguments from the current269+command. Both accept the command as first argument and everything else is270+passed onwards as you would expect.271+272+Example:273+274+.. click:example::275+276+ cli = click.Group()277+278+ @cli.command()279+ @click.option('--count', default=1)280+ def test(count):281+ click.echo('Count: %d' % count)282+283+ @cli.command()284+ @click.option('--count', default=1)285+ @click.pass_context286+ def dist(ctx, count):287+ ctx.forward(test)288+ ctx.invoke(test, count=42)289+290+And what it looks like:291+292+.. click:run::293+294+ invoke(cli, prog_name='cli', args=['dist'])added:tests/test_commands.py+18 added
1+# -*- coding: utf-8 -*-2+import click3+4+5+def test_other_command_invoke(runner):6+ @click.command()7+ @click.pass_context8+ def cli(ctx):9+ return ctx.invoke(other_cmd, 42)10+11+ @click.command()12+ @click.argument('arg', type=click.INT)13+ def other_cmd(arg):14+ click.echo(arg)15+16+ result = runner.invoke(cli, [])17+ assert not result.exception18+ assert result.output == '42\n'