Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add case_sensitive=False as an option to Choice types #887

Merged
merged 1 commit into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions click/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,15 @@ class Choice(ParamType):
generators) may lead to surprising results.

See :ref:`choice-opts` for an example.

:param case_sensitive: Set to false to make choices case insensitive.
Defaults to true.
"""
name = 'choice'

def __init__(self, choices):
def __init__(self, choices, case_sensitive=True):
self.choices = choices
self.case_sensitive = case_sensitive

def get_metavar(self, param):
return '[%s]' % '|'.join(self.choices)
Expand All @@ -150,13 +154,25 @@ def convert(self, value, param, ctx):
if value in self.choices:
return value

# Match through normalization
# Match through normalization and case sensitivity
# first do token_normalize_func, then lowercase
# preserve original `value` to produce an accurate message in
# `self.fail`
normed_value = value
normed_choices = self.choices

if ctx is not None and \
ctx.token_normalize_func is not None:
value = ctx.token_normalize_func(value)
for choice in self.choices:
if ctx.token_normalize_func(choice) == value:
return choice
normed_value = ctx.token_normalize_func(value)
normed_choices = [ctx.token_normalize_func(choice) for choice in
self.choices]

if not self.case_sensitive:
normed_value = normed_value.lower()
normed_choices = [choice.lower() for choice in normed_choices]

if normed_value in normed_choices:
return normed_value

self.fail('invalid choice: %s. (choose from %s)' %
(value, ', '.join(self.choices)), param, ctx)
Expand Down
31 changes: 31 additions & 0 deletions tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,37 @@ def cmd(foo):
in result.output


def test_case_insensitive_choice(runner):
@click.command()
@click.option('--foo', type=click.Choice(
['Orange', 'Apple'], case_sensitive=False))
def cmd(foo):
click.echo(foo)

result = runner.invoke(cmd, ['--foo', 'apple'])
assert result.exit_code == 0

result = runner.invoke(cmd, ['--foo', 'oRANGe'])
assert result.exit_code == 0

result = runner.invoke(cmd, ['--foo', 'Apple'])
assert result.exit_code == 0

@click.command()
@click.option('--foo', type=click.Choice(['Orange', 'Apple']))
def cmd2(foo):
click.echo(foo)

result = runner.invoke(cmd2, ['--foo', 'apple'])
assert result.exit_code == 2

result = runner.invoke(cmd2, ['--foo', 'oRANGe'])
assert result.exit_code == 2

result = runner.invoke(cmd2, ['--foo', 'Apple'])
assert result.exit_code == 0


def test_multiline_help(runner):
@click.command()
@click.option('--foo', help="""
Expand Down