Skip to content
Myr

Always return originally passed choice for Choice()

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Kyle Altendorf · committed by Stephen Rosen · parent 68c9328 · Browse files · 4 files+47 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+47 added−15 removed

  • tests:+16 added−1 removed
  • other:+31 added−14 removed

4 files+47 added−15 removed

modified:click/types.py+21 added-12 removed
131 unmodified lines
132133 You should only pass a list or tuple of choices. Other iterables134 (like generators) may lead to surprising results.135136 The resulting value will always be one of the originally passed choices137 regardless of ``case_sensitive`` or any ``ctx.token_normalize_func``138 being specified.139140 See :ref:`choice-opts` for an example.141142 :param case_sensitive: Set to false to make choices case143 insensitive. Defaults to true.
11 unmodified lines
155 def get_missing_message(self, param):156 return 'Choose from:\n\t%s.' % ',\n\t'.join(self.choices)157158 def convert(self, value, param, ctx):155 # Exact match156 if value in self.choices:157 return value158159 # Match through normalization and case sensitivity160 # first do token_normalize_func, then lowercase161 # preserve original `value` to produce an accurate message in162 # `self.fail`163 normed_value = value164 normed_choices = self.choices164 normed_choices = {choice: choice for choice in self.choices}165166 if ctx is not None and \167 ctx.token_normalize_func is not None:166 if ctx is not None and ctx.token_normalize_func is not None:167 normed_value = ctx.token_normalize_func(value)169 normed_choices = [ctx.token_normalize_func(choice) for choice in170 self.choices]168 normed_choices = {169 ctx.token_normalize_func(normed_choice): original170 for normed_choice, original in normed_choices.items()171 }172173 if not self.case_sensitive:173 normed_value = normed_value.lower()174 normed_choices = [choice.lower() for choice in normed_choices]174 if PY2:175 lower = str.lower176 else:177 lower = str.casefold178179 normed_value = lower(normed_value)180 normed_choices = {181 lower(normed_choice): original182 for normed_choice, original in normed_choices.items()183 }184185 if normed_value in normed_choices:177 return normed_value186 return normed_choices[normed_value]187188 self.fail('invalid choice: %s. (choose from %s)' %189 (value, ', '.join(self.choices)), param, ctx)190
487 unmodified lines
modified:docs/options.rst+10 added-2 removed
313 unmodified lines
314--------------315316Sometimes, you want to have a parameter be a choice of a list of values.317In that case you can use :class:`Choice` type. It can be instantiated318with a list of valid values.318with a list of valid values. The originally passed choice will be returned,319not the str passed on the command line. Token normalization functions and320``case_sensitive=False`` can cause the two to be different but still match.321322Example:323324.. click:example::325326 @click.command()325 @click.option('--hash-type', type=click.Choice(['md5', 'sha1']))327 @click.option('--hash-type',328 type=click.Choice(['MD5', 'SHA1'], case_sensitive=False))329 def digest(hash_type):330 click.echo(hash_type)331332What it looks like:333334.. click:run::335336 invoke(digest, args=['--hash-type=MD5'])337 println()338 invoke(digest, args=['--hash-type=md5'])339 println()340 invoke(digest, args=['--hash-type=foo'])341 println()
3 unmodified lines
345346 You should only pass the choices as list or tuple. Other iterables (like347 generators) may lead to surprising results.348349 Choices should be unique after considering the effects of350 ``case_sensitive`` and any specified token normalization function.351352.. _option-prompting:353354Prompting355---------
449 unmodified lines
modified:tests/test_normalization.py+1 added-1 removed
18 unmodified lines
19def test_choice_normalization(runner):20 @click.command(context_settings=CONTEXT_SETTINGS)21 @click.option('--choice', type=click.Choice(['Foo', 'Bar']))22 def cli(choice):23 click.echo('Foo')23 click.echo(choice)2425 result = runner.invoke(cli, ['--CHOICE', 'FOO'])26 assert result.output == 'Foo\n'27
12 unmodified lines
modified:tests/test_options.py+15 added
333 unmodified lines
334 click.echo(foo)335336 result = runner.invoke(cmd, ['--foo', 'apple'])337 assert result.exit_code == 0338 assert result.output == 'Apple\n'339340 result = runner.invoke(cmd, ['--foo', 'oRANGe'])341 assert result.exit_code == 0342 assert result.output == 'Orange\n'343344 result = runner.invoke(cmd, ['--foo', 'Apple'])345 assert result.exit_code == 0346 assert result.output == 'Apple\n'347348 @click.command()349 @click.option('--foo', type=click.Choice(['Orange', 'Apple']))350 def cmd2(foo):
8 unmodified lines
359 result = runner.invoke(cmd2, ['--foo', 'Apple'])360 assert result.exit_code == 0361362363def test_case_insensitive_choice_returned_exactly(runner):364 @click.command()365 @click.option('--foo', type=click.Choice(366 ['Orange', 'Apple'], case_sensitive=False))367 def cmd(foo):368 click.echo(foo)369370 result = runner.invoke(cmd, ['--foo', 'apple'])371 assert result.exit_code == 0372 assert result.output == 'Apple\n'373374375def test_multiline_help(runner):376 @click.command()377 @click.option('--foo', help="""378 hello
126 unmodified lines