Skip to content
Myr

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

3 files+47 added−1 removed

modified:click/core.py+4 added-1 removed
1082 unmodified lines
1083 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 rv10911092 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 lines
modified:click/types.py+20 added
23 unmodified lines
2425 #: the descriptive name of this type26 name = None2728 #: 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 = None3536 def __call__(self, value, param=None, ctx=None):37 if value is not None:38 return self.convert(value, param, ctx)39
5 unmodified lines
45 `None` (the missing value).46 """47 return value4849 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.5253 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)5859 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' % message
179 unmodified lines
242243 See :ref:`file-args` for more information.244 """245 name = 'filename'246 envvar_list_splitter = os.path.pathsep247248 def __init__(self, mode='r', encoding=None, errors='strict', lazy=None):249 self.mode = mode250 self.encoding = encoding
59 unmodified lines
310 :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.pathsep315316 def __init__(self, exists=False, file_okay=True, dir_okay=True,317 writable=False, readable=True, resolve_path=False):318 self.exists = exists
100 unmodified lines
modified:tests/test_options.py+23 added
1# -*- coding: utf-8 -*-2import re3import os4import click567def test_prefixes(runner):
45 unmodified lines
5354 result = runner.invoke(cli, [])55 assert result.exception56 assert 'Error: Missing option "-m" / "--message".' in result.output575859def test_multiple_envvar(runner):60 @click.command()61 @click.option('--arg', multiple=True)62 def cmd(arg):63 click.echo('|'.join(arg))6465 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'6970 @click.command()71 @click.option('--arg', multiple=True, type=click.Path())72 def cmd(arg):73 click.echo('|'.join(arg))7475 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'