Also added support for nargs != for values from environment.
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 5105e6b · Browse files · 3 files·+71 added−8 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+71 added−8 removed
- tests:+44 added−0 removed
- other:+27 added−8 removed
Provenance
- Commit
eeb1378- Tree
b58611b- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5105e6b
3 files changed+71 added−8 removed
modified:click/core.py+27 added-8 removed
1 import os2 import sys3 import codecs4-from itertools import chain4+from itertools import chain, repeat5 6 from .types import convert_type, IntRange, BOOL7 from .utils import make_str, make_default_short_help, echo8 from .exceptions import ClickException, UsageError, Abort5 unmodified lines14 15 _missing = object()16 17 18+def batch(iterable, batch_size):19+ return list(zip(*repeat(iter(iterable), batch_size)))20+21+22 def iter_params_for_processing(invocation_order, declaration_order):23 """Given a sequence of parameters in the order as should be considered24 for processing and an iterable of parameters that exist, this returns25 a list in the correct order as they should be processed.805 unmodified lines831 ' / '.join('"%s"' % x for x in chain(832 self.opts, self.secondary_opts)),833 )834 831- def value_from_envvar(self, ctx):835+ def resolve_envvar_value(self, ctx):836 if self.envvar is None:837 return838 if isinstance(self.envvar, (tuple, list)):839 for envvar in self.envvar:2 unmodified lines842 return rv843 else:844 return os.environ.get(self.envvar)845 846+ def value_from_envvar(self, ctx):847+ rv = self.resolve_envvar_value(ctx)848+ if rv is not None and self.nargs != 1:849+ rv = self.type.split_envvar_value(rv)850+ return rv851+852 def handle_parse_result(self, ctx, opts, args):853 value = self.consume_value(ctx, opts)854 value = self.full_process_value(ctx, value)855 if self.callback is not None:231 unmodified lines1087 hide_input=self.hide_input,1088 confirmation_prompt=self.confirmation_prompt,1089 value_proc=lambda x: self.process_value(ctx, x))1090 1081- def value_from_envvar(self, ctx):1082- rv = Parameter.value_from_envvar(self, ctx)1083- if rv is None and \1084- self.allow_from_autoenv and \1091+ def resolve_envvar_value(self, ctx):1092+ rv = Parameter.resolve_envvar_value(self, ctx)1093+ if rv is not None:1094+ return rv1095+ if self.allow_from_autoenv and \1096 ctx.auto_envvar_prefix is not None:1097 envvar = '%s_%s' % (ctx.auto_envvar_prefix, self.name.upper())1087- rv = os.environ.get(envvar)1088- if rv is not None and self.multiple:1098+ return os.environ.get(envvar)1099+1100+ def value_from_envvar(self, ctx):1101+ rv = self.resolve_envvar_value(ctx)1102+ if rv is None:1103+ return None1104+ value_depth = (self.nargs != 1) + bool(self.multiple)1105+ if value_depth > 0 and rv is not None:1106 rv = self.type.split_envvar_value(rv)1107+ if self.multiple and self.nargs != 1:1108+ rv = batch(rv, self.nargs)1109 return rv1110 1111 def full_process_value(self, ctx, value):1112 if value is None and self.prompt is not None:49 unmodified linesmodified:tests/test_arguments.py+21 added
68 unmodified lines69 70 result = runner.invoke(inout, ['hello.txt', '-'])71 assert result.output == 'Hey!'72 assert result.exit_code == 073+74+75+def test_nargs_envvar(runner):76+ @click.command()77+ @click.option('--arg', nargs=-1)78+ def cmd(arg):79+ click.echo('|'.join(arg))80+81+ result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',82+ env={'TEST_ARG': 'foo bar'})83+ assert not result.exception84+ assert result.output == 'foo|bar\n'85+86+ @click.command()87+ @click.option('--arg', envvar='X', nargs=-1)88+ def cmd(arg):89+ click.echo('|'.join(arg))90+91+ result = runner.invoke(cmd, [], env={'X': 'foo bar'})92+ assert not result.exception93+ assert result.output == 'foo|bar\n'modified:tests/test_options.py+23 added
83 unmodified lines84 result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',85 env={'TEST_ARG': 'foo%sbar' % os.path.pathsep})86 assert not result.exception87 assert result.output == 'foo|bar\n'88+89+90+def test_nargs_envvar(runner):91+ @click.command()92+ @click.option('--arg', nargs=2)93+ def cmd(arg):94+ click.echo('|'.join(arg))95+96+ result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',97+ env={'TEST_ARG': 'foo bar'})98+ assert not result.exception99+ assert result.output == 'foo|bar\n'100+101+ @click.command()102+ @click.option('--arg', nargs=2, multiple=True)103+ def cmd(arg):104+ for item in arg:105+ click.echo('|'.join(item))106+107+ result = runner.invoke(cmd, [], auto_envvar_prefix='TEST',108+ env={'TEST_ARG': 'x 1 y 2'})109+ assert not result.exception110+ assert result.output == 'x|1\ny|2\n'