Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple options passed via environment variables are split by character #87

Closed
sampsyo opened this issue May 20, 2014 · 2 comments
Closed

Comments

@sampsyo
Copy link

sampsyo commented May 20, 2014

An option with multiple=True yields unexpected (i.e., not useful) behavior when it is specified via an environment variable.

Here's a simple example script:

import click

@click.command()
@click.option('--arg', multiple=True)
def cmd(arg):
    print(arg)

if __name__ == '__main__':
    cmd(auto_envvar_prefix='TEST')

It works as expected when arg is specified with an ordinary command-line option:

$ python3 test.py --arg foo
('foo',)

But when we use an environment variable instead, Click splits the string into characters:

$ TEST_ARG=foo python3 test.py
('f', 'o', 'o')

This is the classic Python iterating-over-a-string-when-you-thought-you-had-a-list failure mode. (These examples were run with click 0.7 from PyPI.)

I can see two reasonable resolutions here: split environment variables on whitespace or don't split them at all. The latter is probably more predictable but the former is more flexible.

@mitsuhiko
Copy link
Contributor

I found a nice way to handle this now. It's the type's responsibility to provide a way to customize this. This means that I can have "foo:bar:baz" for paths and files and "foo bar baz" for everything else. This can also be further customized to even fully disable that feature.

Thanks for bringing it up.

@sampsyo
Copy link
Author

sampsyo commented May 21, 2014

Fantastic; this seems like the perfect solution.

Thanks for click! ✨

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants