Disallow arguments in chain mode and no longer expose leftover args.
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 07a4f18 · Browse files · 3 files·+57 added−1 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+57 added−1 removed
- tests:+43 added−0 removed
- other:+14 added−1 removed
Provenance
- Commit
1c7e1d3- Tree
09f1e26- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
07a4f18
3 files changed+57 added−1 removed
modified:click/core.py+10 added-1 removed
925 unmodified lines926 #: The result callback that is stored. This can be set or927 #: overridden with the :func:`resultcallback` decorator.928 self.result_callback = result_callback929 930+ if self.chain:931+ for param in self.params:932+ if isinstance(param, Argument):933+ raise RuntimeError('Multi commands in chain mode cannot '934+ 'have other arguments.')935+936 def collect_usage_pieces(self, ctx):937 rv = Command.collect_usage_pieces(self, ctx)938 rv.append(self.subcommand_metavar)939 return rv106 unmodified lines1046 # know the subcommands yet, the invoked subcommand attribute is1047 # set to ``*`` to inform the command that subcommands are executed1048 # but nothing else.1049 with ctx:1050+ # The master command does not receive any arguments. That1051+ # would be silly.1052+ ctx.args = []1053 ctx.invoked_subcommand = args and '*' or None1054 Command.invoke(self, ctx)1055 1056 # Otherwise we make every single context and invoke them in a5 unmodified lines1062 sub_ctx = cmd.make_context(cmd_name, args, parent=ctx,1063 allow_extra_args=True,1064 allow_interspersed_args=False)1065 contexts.append(sub_ctx)1057- args = sub_ctx.args1066+ args, sub_ctx.args = sub_ctx.args, []1067 1068 rv = []1069 for sub_ctx in contexts:1070 with sub_ctx:653 unmodified linesmodified:tests/test_chain.py+43 added
1+import sys2 import click3+import pytest4 5 6 def test_basic_chaining(runner):7 @click.group(chain=True)145 unmodified lines153 assert result.output.splitlines() == [154 'FOO',155 'BAR',156 ]157+158+159+def test_args_and_chain(runner):160+ def debug():161+ click.echo('%s=%s' % (162+ sys._getframe(1).f_code.co_name,163+ '|'.join(click.get_current_context().args),164+ ))165+166+ @click.group(chain=True)167+ def cli():168+ debug()169+170+ @cli.command()171+ def a():172+ debug()173+174+ @cli.command()175+ def b():176+ debug()177+178+ @cli.command()179+ def c():180+ debug()181+182+ result = runner.invoke(cli, ['a', 'b', 'c'])183+ assert not result.exception184+ assert result.output.splitlines() == [185+ 'cli=',186+ 'a=',187+ 'b=',188+ 'c=',189+ ]190+191+192+def test_multicommand_no_args(runner):193+ with pytest.raises(RuntimeError):194+ @click.group(chain=True)195+ @click.argument('forbidden')196+ def cli():197+ passmodified:CHANGES+4 added
34 unmodified lines35 virtual output and input streams via `click.get_text_stream(...)`.36 - Unittests now always force a certain virtual terminal width.37 - Added support for allowing dashes to indicate standard streams to the38 `Path` type.39+- Multi commands in chain mode no longer propagate arguments left over40+ from parsing to the callbacks. It's also now disallowed through an41+ exception that arguments are attached to multi commands if chain mode42+ is enabled.43 44 Version 5.145 -----------46 251 unmodified lines