Skip to content
Myr

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

3 files+89 added−0 removed

modified:click/core.py+33 added
163 unmodified lines
164 def invoke(*args, **kwargs):165 """Invokes a command callback in exactly the way it expects.166 """167 self, callback = args[:2]168169 # 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.')176177 args = args[2:]178 if getattr(callback, '__click_pass_context__', False):179 args = (self,) + args180 return callback(*args, **kwargs)181182 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]188189 # 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.')193194 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]198199 return self.invoke(cmd, **kwargs)200201202class 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 lines
257 for param in self.params:258 if not param.is_eager:259 yield param260261 def iter_param_names(self):262 """Iterates over all parameter names."""263 for param in self.iter_params_for_processing():264 yield param.name265266 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 lines
modified:docs/commands.rst+38 added
252 unmodified lines
253254 invoke(cli, prog_name='cli', args=['--help'])255256In case a command exists on more than one source, the first source wins.257258Invoking Other Commands259-----------------------260261Sometimes it might be interesting to invoke one command from another262command. This is generally a pattern that is discouraged with click but263possible nonetheless. For this you can use the :func:`Context.invoke`264or :func:`Context.forward` methods.265266They work similar but the difference is that :func:`Context.invoke` merely267invokes another command with the arguments you provide as a caller,268whereas :func:`Context.forward` fills in the arguments from the current269command. Both accept the command as first argument and everything else is270passed onwards as you would expect.271272Example:273274.. click:example::275276 cli = click.Group()277278 @cli.command()279 @click.option('--count', default=1)280 def test(count):281 click.echo('Count: %d' % count)282283 @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)289290And what it looks like:291292.. click:run::293294 invoke(cli, prog_name='cli', args=['dist'])
added:tests/test_commands.py+18 added
1# -*- coding: utf-8 -*-2import click345def test_other_command_invoke(runner):6 @click.command()7 @click.pass_context8 def cli(ctx):9 return ctx.invoke(other_cmd, 42)1011 @click.command()12 @click.argument('arg', type=click.INT)13 def other_cmd(arg):14 click.echo(arg)1516 result = runner.invoke(cli, [])17 assert not result.exception18 assert result.output == '42\n'