Skip to content
Myr

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

3 files+57 added−1 removed

modified:click/core.py+10 added-1 removed
925 unmodified lines
926 #: The result callback that is stored. This can be set or927 #: overridden with the :func:`resultcallback` decorator.928 self.result_callback = result_callback929930 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.')935936 def collect_usage_pieces(self, ctx):937 rv = Command.collect_usage_pieces(self, ctx)938 rv.append(self.subcommand_metavar)939 return rv
106 unmodified lines
1046 # 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)10551056 # Otherwise we make every single context and invoke them in a
5 unmodified lines
1062 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, []10671068 rv = []1069 for sub_ctx in contexts:1070 with sub_ctx:
653 unmodified lines
modified:tests/test_chain.py+43 added
1import sys2import click3import pytest456def test_basic_chaining(runner):7 @click.group(chain=True)
145 unmodified lines
153 assert result.output.splitlines() == [154 'FOO',155 'BAR',156 ]157158159def 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 ))165166 @click.group(chain=True)167 def cli():168 debug()169170 @cli.command()171 def a():172 debug()173174 @cli.command()175 def b():176 debug()177178 @cli.command()179 def c():180 debug()181182 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 ]190191192def test_multicommand_no_args(runner):193 with pytest.raises(RuntimeError):194 @click.group(chain=True)195 @click.argument('forbidden')196 def cli():197 pass
modified:CHANGES+4 added
34 unmodified lines
35 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.4344Version 5.145-----------46
251 unmodified lines