nargs=-1 works with envvar
Before adoption: this commit came in with the history the project adopted, so no landing records it.
nargs > 1 is validated for envvar and default values
authored by Amy Lei · committed by David Lord · parent 6271cee · 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.
Trailers
- Co-authored-by: Rachel Liu <r248liu@uwaterloo.ca>
Diff stats
4 files+62 added−17 removed
- tests:+42 added−17 removed
- other:+20 added−0 removed
Provenance
- Commit
3d3ea9c- Tree
dbb6554- Author
Amy Lei <leiamy12@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
6271cee
4 files changed+62 added−17 removed
modified:src/click/core.py+14 added
1622 unmodified lines1623 value = self.process_value(ctx, value)1624 1625 if value is None and not ctx.resilient_parsing:1626 value = self.get_default(ctx)1627+1628 if value is not None:1629 ctx.set_parameter_source(self.name, ParameterSource.DEFAULT)1630 1631 if self.required and self.value_is_missing(value):1632 raise MissingParameter(ctx=ctx, param=self)1633 1634+ # 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+ )1646+1647 return value1648 1649 def resolve_envvar_value(self, ctx):1650 if self.envvar is None:438 unmodified linesmodified:src/click/parser.py+4 added
181 unmodified lines182 elif holes != 0:183 raise BadArgumentUsage(184 f"argument {self.dest} takes {self.nargs} values"185 )186+187+ if self.nargs == -1 and self.obj.envvar is not None:188+ value = None189+190 state.opts[self.dest] = value191 state.order.append(self.obj)192 193 242 unmodified linesmodified:tests/test_arguments.py+42 added-17 removed
16 unmodified lines17 assert not result.exception18 assert result.output.splitlines() == ["src=foo.txt|bar.txt", "dst=dir"]19 20 21-def test_nargs_default(runner):21+def test_argument_unbounded_nargs_cant_have_default(runner):22 with pytest.raises(TypeError, match="nargs=-1"):23 24 @click.command()25 @click.argument("src", nargs=-1, default=42)136 unmodified lines162 assert not result.exception163 assert result.output == "Foo bar baz\n"164 165 166-def 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+)177+def 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)182 172- 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"177-183 @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)}")187 183- 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.output191 192 193 def test_empty_nargs(runner):194 @click.command()112 unmodified lines307 @click.command()308 @click.argument("x", click.Choice(["a", "b"]))309 def copy(x):310 click.echo(x)311+312+313+@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+)322+def 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)}")327+328+ result = runner.invoke(cmd)329+ assert result.exit_code == code330+ assert output in result.outputmodified:CHANGES.rst+2 added
29 unmodified lines30 ``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`36 37 38 Version 7.1.239 -------------635 unmodified lines