Skip to content
Myr

Add `case_sensitive=False` as an option to Choice

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

Adds a test that validates both the new and the old behavior WRT case sensitivity. Applies lowercasing *after* token normalization, in case someone has a `token_normalize_func` which is, itself, case-sensitive. Closes #569

authored by Stephen Rosen · committed by Zachery Bir · parent bd59717 · Browse files · 2 files+53 added−6 removed
This commit

State

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

Diff stats

2 files+53 added−6 removed

  • tests:+31 added−0 removed
  • other:+22 added−6 removed

2 files+53 added−6 removed

modified:click/types.py+22 added-6 removed
131 unmodified lines
132 You should only pass *choices* as list or tuple. Other iterables (like133 generators) may lead to surprising results.134135 See :ref:`choice-opts` for an example.136137 :param case_sensitive: Set to false to make choices case insensitive.138 Defaults to true.139 """140 name = 'choice'141139 def __init__(self, choices):142 def __init__(self, choices, case_sensitive=True):143 self.choices = choices144 self.case_sensitive = case_sensitive145146 def get_metavar(self, param):147 return '[%s]' % '|'.join(self.choices)148
4 unmodified lines
153 # Exact match154 if value in self.choices:155 return value156153 # Match through normalization157 # Match through normalization and case sensitivity158 # first do token_normalize_func, then lowercase159 # preserve original `value` to produce an accurate message in160 # `self.fail`161 normed_value = value162 normed_choices = self.choices163164 if ctx is not None and \165 ctx.token_normalize_func is not None:156 value = ctx.token_normalize_func(value)157 for choice in self.choices:158 if ctx.token_normalize_func(choice) == value:159 return choice166 normed_value = ctx.token_normalize_func(value)167 normed_choices = [ctx.token_normalize_func(choice) for choice in168 self.choices]169170 if not self.case_sensitive:171 normed_value = normed_value.lower()172 normed_choices = [choice.lower() for choice in normed_choices]173174 if normed_value in normed_choices:175 return normed_value176177 self.fail('invalid choice: %s. (choose from %s)' %178 (value, ', '.join(self.choices)), param, ctx)179180 def __repr__(self):
430 unmodified lines
modified:tests/test_options.py+31 added
281 unmodified lines
282 assert 'Error: Missing option "--foo". Choose from foo, bar.' \283 in result.output284285286def test_case_insensitive_choice(runner):287 @click.command()288 @click.option('--foo', type=click.Choice(289 ['Orange', 'Apple'], case_sensitive=False))290 def cmd(foo):291 click.echo(foo)292293 result = runner.invoke(cmd, ['--foo', 'apple'])294 assert result.exit_code == 0295296 result = runner.invoke(cmd, ['--foo', 'oRANGe'])297 assert result.exit_code == 0298299 result = runner.invoke(cmd, ['--foo', 'Apple'])300 assert result.exit_code == 0301302 @click.command()303 @click.option('--foo', type=click.Choice(['Orange', 'Apple']))304 def cmd2(foo):305 click.echo(foo)306307 result = runner.invoke(cmd2, ['--foo', 'apple'])308 assert result.exit_code == 2309310 result = runner.invoke(cmd2, ['--foo', 'oRANGe'])311 assert result.exit_code == 2312313 result = runner.invoke(cmd2, ['--foo', 'Apple'])314 assert result.exit_code == 0315316317def test_multiline_help(runner):318 @click.command()319 @click.option('--foo', help="""320 hello
127 unmodified lines