Skip to content
Myr

Merge pull request #1604 from MLH-Fellowship/envvar-nargs-bug

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

nargs=-1 works with envvar

authored by David Lord · committed by GitHub · parents 6271cee, 3d3ea9c · Browse files · 4 files+62 added−17 removed
This commit

State

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

Diff stats

4 files+62 added−17 removed

  • tests:+42 added−17 removed
  • other:+20 added−0 removed

4 files+62 added−17 removed

modified:src/click/core.py+14 added
1622 unmodified lines
1623 value = self.process_value(ctx, value)16241625 if value is None and not ctx.resilient_parsing:1626 value = self.get_default(ctx)16271628 if value is not None:1629 ctx.set_parameter_source(self.name, ParameterSource.DEFAULT)16301631 if self.required and self.value_is_missing(value):1632 raise MissingParameter(ctx=ctx, param=self)16331634 # For bounded nargs (!= -1), validate the number of values.1635 if (1636 not ctx.resilient_parsing1637 and self.nargs > 11638 and self.nargs != len(value)1639 and isinstance(value, (tuple, list))1640 ):1641 were = "was" if len(value) == 1 else "were"1642 ctx.fail(1643 f"Argument {self.name!r} takes {self.nargs} values but"1644 f" {len(value)} {were} given."1645 )16461647 return value16481649 def resolve_envvar_value(self, ctx):1650 if self.envvar is None:
438 unmodified lines
modified:src/click/parser.py+4 added
181 unmodified lines
182 elif holes != 0:183 raise BadArgumentUsage(184 f"argument {self.dest} takes {self.nargs} values"185 )186187 if self.nargs == -1 and self.obj.envvar is not None:188 value = None189190 state.opts[self.dest] = value191 state.order.append(self.obj)192193
242 unmodified lines
modified:tests/test_arguments.py+42 added-17 removed
16 unmodified lines
17 assert not result.exception18 assert result.output.splitlines() == ["src=foo.txt|bar.txt", "dst=dir"]192021def test_nargs_default(runner):21def test_argument_unbounded_nargs_cant_have_default(runner):22 with pytest.raises(TypeError, match="nargs=-1"):2324 @click.command()25 @click.argument("src", nargs=-1, default=42)
136 unmodified lines
162 assert not result.exception163 assert result.output == "Foo bar baz\n"164165166def test_nargs_envvar(runner):167 @click.command()168 @click.option("--arg", nargs=2)169 def cmd(arg):170 click.echo("|".join(arg))166@pytest.mark.parametrize(167 ("nargs", "value", "code", "output"),168 [169 (2, "", 2, "Argument 'arg' takes 2 values but 0 were given."),170 (2, "a", 2, "Argument 'arg' takes 2 values but 1 was given."),171 (2, "a b", 0, "len 2"),172 (2, "a b c", 2, "Argument 'arg' takes 2 values but 3 were given."),173 (-1, "a b c", 0, "len 3"),174 (-1, "", 0, "len 0"),175 ],176)177def test_nargs_envvar(runner, nargs, value, code, output):178 if nargs == -1:179 param = click.argument("arg", envvar="X", nargs=nargs)180 else:181 param = click.option("--arg", envvar="X", nargs=nargs)182172 result = runner.invoke(173 cmd, [], auto_envvar_prefix="TEST", env={"TEST_ARG": "foo bar"}174 )175 assert not result.exception176 assert result.output == "foo|bar\n"177183 @click.command()179 @click.option("--arg", envvar="X", nargs=2)184 @param185 def cmd(arg):181 click.echo("|".join(arg))186 click.echo(f"len {len(arg)}")187183 result = runner.invoke(cmd, [], env={"X": "foo bar"})184 assert not result.exception185 assert result.output == "foo|bar\n"188 result = runner.invoke(cmd, env={"X": value})189 assert result.exit_code == code190 assert output in result.output191192193def test_empty_nargs(runner):194 @click.command()
112 unmodified lines
307 @click.command()308 @click.argument("x", click.Choice(["a", "b"]))309 def copy(x):310 click.echo(x)311312313@pytest.mark.parametrize(314 ("value", "code", "output"),315 [316 ((), 2, "Argument 'arg' takes 2 values but 0 were given."),317 (("a",), 2, "Argument 'arg' takes 2 values but 1 was given."),318 (("a", "b"), 0, "len 2"),319 (("a", "b", "c"), 2, "Argument 'arg' takes 2 values but 3 were given."),320 ],321)322def test_nargs_default(runner, value, code, output):323 @click.command()324 @click.argument("arg", nargs=2, default=value)325 def cmd(arg):326 click.echo(f"len {len(arg)}")327328 result = runner.invoke(cmd)329 assert result.exit_code == code330 assert output in result.output
modified:CHANGES.rst+2 added
29 unmodified lines
30 ``AliasedGroup``. :issue:`1422`31- The ``BOOL`` type accepts the values "on" and "off". :issue:`1629`32- A ``Group`` with ``invoke_without_command=True`` will always invoke33 its result callback. :issue:`1178`34- ``nargs == -1`` and ``nargs > 1`` is parsed and validated for35 values from environment variables and defaults. :issue:`729`363738Version 7.1.239-------------
635 unmodified lines