Skip to content
Myr

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

3 files+71 added−8 removed

modified:click/core.py+27 added-8 removed
1import os2import sys3import codecs4from itertools import chain4from itertools import chain, repeat56from .types import convert_type, IntRange, BOOL7from .utils import make_str, make_default_short_help, echo8from .exceptions import ClickException, UsageError, Abort
5 unmodified lines
1415_missing = object()161718def batch(iterable, batch_size):19 return list(zip(*repeat(iter(iterable), batch_size)))202122def 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 lines
831 ' / '.join('"%s"' % x for x in chain(832 self.opts, self.secondary_opts)),833 )834831 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 lines
842 return rv843 else:844 return os.environ.get(self.envvar)845846 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 rv851852 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 lines
1087 hide_input=self.hide_input,1088 confirmation_prompt=self.confirmation_prompt,1089 value_proc=lambda x: self.process_value(ctx, x))10901081 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)10991100 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 rv11101111 def full_process_value(self, ctx, value):1112 if value is None and self.prompt is not None:
49 unmodified lines
modified:tests/test_arguments.py+21 added
68 unmodified lines
6970 result = runner.invoke(inout, ['hello.txt', '-'])71 assert result.output == 'Hey!'72 assert result.exit_code == 0737475def test_nargs_envvar(runner):76 @click.command()77 @click.option('--arg', nargs=-1)78 def cmd(arg):79 click.echo('|'.join(arg))8081 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'8586 @click.command()87 @click.option('--arg', envvar='X', nargs=-1)88 def cmd(arg):89 click.echo('|'.join(arg))9091 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 lines
84 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'888990def test_nargs_envvar(runner):91 @click.command()92 @click.option('--arg', nargs=2)93 def cmd(arg):94 click.echo('|'.join(arg))9596 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'100101 @click.command()102 @click.option('--arg', nargs=2, multiple=True)103 def cmd(arg):104 for item in arg:105 click.echo('|'.join(item))106107 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'