@click.argument(name, nargs=2, envvar=NAME)
Works as expected. If you set NAME="a b" and run your command name in the called function will be a tuple of ('a', 'b').
@click.argument(name, nargs=-1, envvar=NAME)
on the other hand doesn't work as expected. If you call it in the same exact way, name will be set to the empty tuple.
Thanks!
Looks like there may be a couple bugs around this behavior.
my test code to reproduce
@click.group()
def cli():
pass
@cli.command(short_help='test')
@click.argument('src', nargs=-1, envvar="TEST")
def testing(src):
click.echo("src")
click.echo(src)
click.echo("len %s" % len(src))
click.echo("type %s" % type(src))
for c,v in enumerate(src):
click.echo('elemenet %s of src: %s ' % (c,v))
cli()
case above nargs=-1 gives an empty tuple
TEST="one two three 4" python repo.py testing
src
()
len 0
type <class 'tuple'>
if we run with nargs=2 shouldn't become a 4-tuple (the correct behavior here is a bit ambiguous)
TEST="one two three 4" python repo.py testing
src
('one', 'two', 'three', '4')
len 4
type <class 'tuple'>
elemenet 0 of src: one
elemenet 1 of src: two
elemenet 2 of src: three
elemenet 3 of src: 4
@rch-liu and I are working on this issue! While we've found a fix on the nargs=-1 problem, we are also uncertain about the correct behavior for the case where nargs != len(envvar)
An idea was to raise an error like it would have if it weren't an envvar. Alternatively, if this is indeed correct behavior, maybe we can update the docs to indicate that?
Raising an error seems correct. Click should behave the same regardless of where the value for a parameter came from.
What currently happens for nargs != len? Were env vars not working at all with nargs?
What currently happens for nargs != len? Were env vars not working at all with nargs?
It works when nargs == 1 since it just treats the entire arg as a string. But for nargs > 1, it wasn't enforced. Following fdavis's example, nargs=2 it could end up as a length 4 tuple or even a length 1 tuple. However, it does correctly raise an error when the env var isn't bound to a value.
OK, raising an error sounds fine. Make sure to describe that in the changelog.