diff options
-rw-r--r-- | CHANGES | 4 | ||||
-rw-r--r-- | click/core.py | 4 | ||||
-rw-r--r-- | tests/test_chain.py | 28 |
3 files changed, 29 insertions, 7 deletions
@@ -38,8 +38,8 @@ Version 6.0 `Path` type. - Multi commands in chain mode no longer propagate arguments left over from parsing to the callbacks. It's also now disallowed through an - exception that arguments are attached to multi commands if chain mode - is enabled. + exception when optional arguments are attached to multi commands if chain + mode is enabled. Version 5.1 ----------- diff --git a/click/core.py b/click/core.py index a580e42..3744c21 100644 --- a/click/core.py +++ b/click/core.py @@ -929,9 +929,9 @@ class MultiCommand(Command): if self.chain: for param in self.params: - if isinstance(param, Argument): + if isinstance(param, Argument) and not param.required: raise RuntimeError('Multi commands in chain mode cannot ' - 'have other arguments.') + 'have optional arguments.') def collect_usage_pieces(self, ctx): rv = Command.collect_usage_pieces(self, ctx) diff --git a/tests/test_chain.py b/tests/test_chain.py index aee6f2d..b76b114 100644 --- a/tests/test_chain.py +++ b/tests/test_chain.py @@ -189,9 +189,31 @@ def test_args_and_chain(runner): ] -def test_multicommand_no_args(runner): +def test_multicommand_arg_behavior(runner): with pytest.raises(RuntimeError): @click.group(chain=True) - @click.argument('forbidden') - def cli(): + @click.argument('forbidden', required=False) + def bad_cli(): pass + + with pytest.raises(RuntimeError): + @click.group(chain=True) + @click.argument('forbidden', nargs=-1) + def bad_cli2(): + pass + + @click.group(chain=True) + @click.argument('arg') + def cli(arg): + click.echo('cli:%s' % arg) + + @cli.command() + def a(): + click.echo('a') + + result = runner.invoke(cli, ['foo', 'a']) + assert not result.exception + assert result.output.splitlines() == [ + 'cli:foo', + 'a', + ] |