Added support for lists from envvars. This fixes #87
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent 955beb8 · Browse files · 3 files·+47 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+47 added−1 removed
- tests:+23 added−0 removed
- other:+24 added−1 removed
Provenance
- Commit
817ef38- Tree
5ce773a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
955beb8
3 files changed+47 added−1 removed
modified:click/core.py+4 added-1 removed
1082 unmodified lines1083 if rv is not None:1084 return rv1085 if self.allow_from_autoenv and ctx.auto_envvar_prefix is not None:1086 envvar = '%s_%s' % (ctx.auto_envvar_prefix, self.name.upper())1087- return os.environ.get(envvar)1087+ rv = os.environ.get(envvar)1088+ if self.multiple:1089+ rv = self.type.split_envvar_value(rv)1090+ return rv1091 1092 def full_process_value(self, ctx, value):1093 if value is None and self.prompt is not None:1094 return self.prompt_for_value(ctx)48 unmodified linesmodified:click/types.py+20 added
23 unmodified lines24 25 #: the descriptive name of this type26 name = None27 28+ #: if a list of this type is expected and the value is pulled from a29+ #: string environment variable, this is what splits it up. `None`30+ #: means any whitespace. For all parameter the general rule is that31+ #: whitespace splits them up. The exception are paths and files which32+ #: are split by ``os.path.pathsep`` by default (":" on unix and ";' on33+ #: windows).34+ envvar_list_splitter = None35+36 def __call__(self, value, param=None, ctx=None):37 if value is not None:38 return self.convert(value, param, ctx)39 5 unmodified lines45 `None` (the missing value).46 """47 return value48 49+ def split_envvar_value(self, rv):50+ """Given a value from an environment variable this splits it up51+ into small chunks depending on the defined envvar list splitter.52+53+ If the splitter is set to `None` which means that whitespace splits,54+ then leading and trailing whitespace is ignored. Otherwise leading55+ and trailing splitters usually lead to empty items being included.56+ """57+ return (rv or '').split(self.envvar_list_splitter)58+59 def fail(self, message, param=None, ctx=None):60 """Helper method to fail with an invalid value message."""61 if param is None:62 message = 'Invalid value: %s' % message179 unmodified lines242 243 See :ref:`file-args` for more information.244 """245 name = 'filename'246+ envvar_list_splitter = os.path.pathsep247 248 def __init__(self, mode='r', encoding=None, errors='strict', lazy=None):249 self.mode = mode250 self.encoding = encoding59 unmodified lines310 :param resolve_path: if this is true, then the path is fully resolved311 before the value is passed onwards. This means312 that it's absolute and symlinks are resolved.313 """314+ envvar_list_splitter = os.path.pathsep315 316 def __init__(self, exists=False, file_okay=True, dir_okay=True,317 writable=False, readable=True, resolve_path=False):318 self.exists = exists100 unmodified linesmodified:tests/test_options.py+23 added
1 # -*- coding: utf-8 -*-2 import re3+import os4 import click5 6 7 def test_prefixes(runner):45 unmodified lines53 54 result = runner.invoke(cli, [])55 assert result.exception56 assert 'Error: Missing option "-m" / "--message".' in result.output57+58+59+def test_multiple_envvar(runner):60+ @click.command()61+ @click.option('--arg', multiple=True)62+ def cmd(arg):63+ click.echo('|'.join(arg))64+65+ result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',66+ env={'TEST_ARG': 'foo bar baz'})67+ assert not result.exception68+ assert result.output == 'foo|bar|baz\n'69+70+ @click.command()71+ @click.option('--arg', multiple=True, type=click.Path())72+ def cmd(arg):73+ click.echo('|'.join(arg))74+75+ result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',76+ env={'TEST_ARG': 'foo%sbar' % os.path.pathsep})77+ assert not result.exception78+ assert result.output == 'foo|bar\n'