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
Provenance
- Commit
3ab47e9- Tree
b8db140- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
506e4e7
2 files changed+38 added−38 removed
modified:docs/advanced.rst+38 added
56 unmodified lines57 58 @cli.command()59 def pop():60 pass61+62+Invoking Other Commands63+-----------------------64+65+Sometimes it might be interesting to invoke one command from another66+command. This is generally a pattern that is discouraged with click but67+possible nonetheless. For this you can use the :func:`Context.invoke`68+or :func:`Context.forward` methods.69+70+They work similar but the difference is that :func:`Context.invoke` merely71+invokes another command with the arguments you provide as a caller,72+whereas :func:`Context.forward` fills in the arguments from the current73+command. Both accept the command as first argument and everything else is74+passed onwards as you would expect.75+76+Example:77+78+.. click:example::79+80+ cli = click.Group()81+82+ @cli.command()83+ @click.option('--count', default=1)84+ def test(count):85+ click.echo('Count: %d' % count)86+87+ @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)93+94+And what it looks like:95+96+.. click:run::97+98+ invoke(cli, prog_name='cli', args=['dist'])modified:docs/commands.rst-38 removed
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'])