Skip to content
Myr

Added support for multiple defaults on options and nargs

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 8d0dfa5 · Browse files · 3 files+63 added−4 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+63 added−4 removed

  • tests:+46 added−0 removed
  • other:+17 added−4 removed

3 files+63 added−4 removed

modified:click/core.py+15 added-4 removed
1022 unmodified lines
1023 if callable(self.default):1024 rv = self.default()1025 else:1026 rv = self.default1027 return self.type(rv, self, ctx)1027 return self.type_cast_value(ctx, rv)10281029 def add_to_parser(self, parser, ctx):1030 pass1031
4 unmodified lines
1036 if value is None:1037 value = self.value_from_envvar(ctx)1038 return value10391040 def process_value(self, ctx, value):1041 """Given a value and context this runs the logic to convert the1042 value as necessary.1040 def type_cast_value(self, ctx, value):1041 """Given a value this runs it properly through the type system.1042 This automatically handles things like `nargs` and `multiple`.1043 """1044 def _convert(value, level):1045 if level == 0:1046 return self.type(value, self, ctx)1047 return tuple(_convert(x, level - 1) for x in value or ())1048 return _convert(value, (self.nargs != 1) + bool(self.multiple))10491050 def process_value(self, ctx, value):1051 """Given a value and context this runs the logic to convert the1052 value as necessary.1053 """1054 # If the value we were given is None we do nothing. This way1055 # code that calls this can easily figure out if something was1056 # not provided. Otherwise it would be converted into an empty1057 # tuple for multiple invocations which is inconvenient.1058 if value is not None:1059 return self.type_cast_value(ctx, value)10601061 def value_is_missing(self, value):1062 if value is None:1063 return True1064 if (self.nargs != 1 or self.multiple) and value == ():
378 unmodified lines
added:tests/test_defaults.py+46 added
1import click234def test_basic_defaults(runner):5 @click.command()6 @click.option('--foo', default=42, type=click.FLOAT)7 def cli(foo):8 assert type(foo) is float9 click.echo('FOO:[%s]' % foo)1011 result = runner.invoke(cli, [])12 assert not result.exception13 assert 'FOO:[42.0]' in result.output141516def test_multiple_defaults(runner):17 @click.command()18 @click.option('--foo', default=[23, 42], type=click.FLOAT,19 multiple=True)20 def cli(foo):21 for item in foo:22 assert type(item) is float23 click.echo(item)2425 result = runner.invoke(cli, [])26 assert not result.exception27 assert result.output.splitlines() == [28 '23.0',29 '42.0',30 ]313233def test_nargs_plus_multiple(runner):34 @click.command()35 @click.option('--arg', default=((1, 2), (3, 4)),36 nargs=2, multiple=True, type=click.INT)37 def cli(arg):38 for item in arg:39 click.echo('<%d|%d>' % item)4041 result = runner.invoke(cli, [])42 assert not result.exception43 assert result.output.splitlines() == [44 '<1|2>',45 '<3|4>',46 ]
modified:CHANGES+2 added
21 unmodified lines
22 passing. This should catch situations where users wanted to23 created arguments instead of options.24- Restructured Click internally to support vendoring.25- Added support for multi command chaining.26- Added support for defaults on options with `multiple` and27 options and arguments with `nargs != 1`.2829Version 2.630-----------31
111 unmodified lines