Merge pull request #887 from sirosen/feature/case-insensitive-choice
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add `case_sensitive=False` as an option to Choice types
authored by Zachery Bir · committed by GitHub · parents a00e018, 138a6e3 · 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
Provenance
- Commit
c70d463- Tree
ccbe40d- Author
Zachery Bir <zbir@zacbir.net>- Committer
GitHub <noreply@github.com>- Parent 1
a00e018- Parent 2
138a6e3
2 files changed+53 added−6 removed
modified:click/types.py+22 added-6 removed
131 unmodified lines132 You should only pass *choices* as list or tuple. Other iterables (like133 generators) may lead to surprising results.134 135 See :ref:`choice-opts` for an example.136+137+ :param case_sensitive: Set to false to make choices case insensitive.138+ Defaults to true.139 """140 name = 'choice'141 139- def __init__(self, choices):142+ def __init__(self, choices, case_sensitive=True):143 self.choices = choices144+ self.case_sensitive = case_sensitive145 146 def get_metavar(self, param):147 return '[%s]' % '|'.join(self.choices)148 4 unmodified lines153 # Exact match154 if value in self.choices:155 return value156 153- # 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.choices163+164 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]169 170+ if not self.case_sensitive:171+ normed_value = normed_value.lower()172+ normed_choices = [choice.lower() for choice in normed_choices]173+174+ if normed_value in normed_choices:175+ return normed_value176+177 self.fail('invalid choice: %s. (choose from %s)' %178 (value, ', '.join(self.choices)), param, ctx)179 180 def __repr__(self):430 unmodified linesmodified:tests/test_options.py+31 added
308 unmodified lines309 assert 'Error: Missing option "--foo". Choose from foo, bar.' \310 in result.output311 312 313+def test_case_insensitive_choice(runner):314+ @click.command()315+ @click.option('--foo', type=click.Choice(316+ ['Orange', 'Apple'], case_sensitive=False))317+ def cmd(foo):318+ click.echo(foo)319+320+ result = runner.invoke(cmd, ['--foo', 'apple'])321+ assert result.exit_code == 0322+323+ result = runner.invoke(cmd, ['--foo', 'oRANGe'])324+ assert result.exit_code == 0325+326+ result = runner.invoke(cmd, ['--foo', 'Apple'])327+ assert result.exit_code == 0328+329+ @click.command()330+ @click.option('--foo', type=click.Choice(['Orange', 'Apple']))331+ def cmd2(foo):332+ click.echo(foo)333+334+ result = runner.invoke(cmd2, ['--foo', 'apple'])335+ assert result.exit_code == 2336+337+ result = runner.invoke(cmd2, ['--foo', 'oRANGe'])338+ assert result.exit_code == 2339+340+ result = runner.invoke(cmd2, ['--foo', 'Apple'])341+ assert result.exit_code == 0342+343+344 def test_multiline_help(runner):345 @click.command()346 @click.option('--foo', help="""347 hello126 unmodified lines