Skip to content
Myr

Moved the command invoke docs

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 506e4e7 · Browse files · 2 files+38 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+38 added−38 removed

  • other:+38 added−38 removed

2 files+38 added−38 removed

modified:docs/advanced.rst+38 added
56 unmodified lines
5758 @cli.command()59 def pop():60 pass6162Invoking Other Commands63-----------------------6465Sometimes it might be interesting to invoke one command from another66command. This is generally a pattern that is discouraged with click but67possible nonetheless. For this you can use the :func:`Context.invoke`68or :func:`Context.forward` methods.6970They work similar but the difference is that :func:`Context.invoke` merely71invokes another command with the arguments you provide as a caller,72whereas :func:`Context.forward` fills in the arguments from the current73command. Both accept the command as first argument and everything else is74passed onwards as you would expect.7576Example:7778.. click:example::7980 cli = click.Group()8182 @cli.command()83 @click.option('--count', default=1)84 def test(count):85 click.echo('Count: %d' % count)8687 @cli.command()88 @click.option('--count', default=1)89 @click.pass_context90 def dist(ctx, count):91 ctx.forward(test)92 ctx.invoke(test, count=42)9394And what it looks like:9596.. click:run::9798 invoke(cli, prog_name='cli', args=['dist'])
modified:docs/commands.rst-38 removed
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'])