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
Provenance
- Commit
c1b3497- Tree
21c203f- Author
Kyle Altendorf <sda@fstab.net>- Committer
Stephen Rosen <sirosen@globus.org>- Parent
68c9328
4 files changed+47 added−15 removed
modified:click/types.py+21 added-12 removed
131 unmodified lines132 133 You should only pass a list or tuple of choices. Other iterables134 (like generators) may lead to surprising results.135 136+ 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.139+140 See :ref:`choice-opts` for an example.141 142 :param case_sensitive: Set to false to make choices case143 insensitive. Defaults to true.11 unmodified lines155 def get_missing_message(self, param):156 return 'Choose from:\n\t%s.' % ',\n\t'.join(self.choices)157 158 def convert(self, value, param, ctx):155- # Exact match156- if value in self.choices:157- return value158-159 # 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}165 166- 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+ }172 173 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.casefold178 179+ normed_value = lower(normed_value)180+ normed_choices = {181+ lower(normed_choice): original182+ for normed_choice, original in normed_choices.items()183+ }184+185 if normed_value in normed_choices:177- return normed_value186+ return normed_choices[normed_value]187 188 self.fail('invalid choice: %s. (choose from %s)' %189 (value, ', '.join(self.choices)), param, ctx)190 487 unmodified linesmodified:docs/options.rst+10 added-2 removed
313 unmodified lines314 --------------315 316 Sometimes, you want to have a parameter be a choice of a list of values.317 In that case you can use :class:`Choice` type. It can be instantiated318-with a list of valid values.318+with a list of valid values. The originally passed choice will be returned,319+not the str passed on the command line. Token normalization functions and320+``case_sensitive=False`` can cause the two to be different but still match.321 322 Example:323 324 .. click:example::325 326 @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)331 332 What it looks like:333 334 .. click:run::335 336+ 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 lines345 346 You should only pass the choices as list or tuple. Other iterables (like347 generators) may lead to surprising results.348 349+ Choices should be unique after considering the effects of350+ ``case_sensitive`` and any specified token normalization function.351+352 .. _option-prompting:353 354 Prompting355 ---------449 unmodified linesmodified:tests/test_normalization.py+1 added-1 removed
18 unmodified lines19 def 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)24 25 result = runner.invoke(cli, ['--CHOICE', 'FOO'])26 assert result.output == 'Foo\n'27 12 unmodified linesmodified:tests/test_options.py+15 added
333 unmodified lines334 click.echo(foo)335 336 result = runner.invoke(cmd, ['--foo', 'apple'])337 assert result.exit_code == 0338+ assert result.output == 'Apple\n'339 340 result = runner.invoke(cmd, ['--foo', 'oRANGe'])341 assert result.exit_code == 0342+ assert result.output == 'Orange\n'343 344 result = runner.invoke(cmd, ['--foo', 'Apple'])345 assert result.exit_code == 0346+ assert result.output == 'Apple\n'347 348 @click.command()349 @click.option('--foo', type=click.Choice(['Orange', 'Apple']))350 def cmd2(foo):8 unmodified lines359 result = runner.invoke(cmd2, ['--foo', 'Apple'])360 assert result.exit_code == 0361 362 363+def 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)369+370+ result = runner.invoke(cmd, ['--foo', 'apple'])371+ assert result.exit_code == 0372+ assert result.output == 'Apple\n'373+374+375 def test_multiline_help(runner):376 @click.command()377 @click.option('--foo', help="""378 hello126 unmodified lines