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

Handle completion of Choices in Tuple Options #761

Closed
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
25 changes: 24 additions & 1 deletion click/_bashcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .utils import echo
from .parser import split_arg_string
from .core import MultiCommand, Option, Argument
from .types import Choice
from .types import Choice, Tuple


WORDBREAK = '='
Expand Down Expand Up @@ -80,6 +80,19 @@ def is_incomplete_option(all_args, cmd_param):
return True if last_option and last_option in cmd_param.opts else False


def last_option_values(all_args):
option_index = last_option_index(all_args)
return all_args[option_index:]


def last_option_index(all_args):
for index, arg_str in enumerate(reversed([arg for arg in all_args if arg != WORDBREAK])):
if start_of_option(arg_str):
return len(all_args) - index

return -1


def is_incomplete_argument(current_params, cmd_param):
"""
:param current_params: the current params and values for this argument as already entered
Expand Down Expand Up @@ -136,6 +149,16 @@ def get_choices(cli, prog_name, args, incomplete):
if isinstance(cmd_param, Option) and is_incomplete_option(all_args, cmd_param):
if isinstance(cmd_param.type, Choice):
choices.extend(cmd_param.type.choices)
elif isinstance(cmd_param.type, Tuple):
values_provided = len(last_option_values(all_args))
tuple_value_types = cmd_param.type.types
current_value_type = tuple_value_types[values_provided]

if isinstance(current_value_type, Choice):
# We're in a Choice value of a Tuple Option, so add the
# available choices.
choices.extend(current_value_type.choices)

found_param = True
break
if not found_param:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_bashcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,23 @@ def bsub(bsub_opt):
assert list(get_choices(cli, 'lol',
['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],
'')) == ['bbsubarg1', 'bbsubarg2']


def test_tuple_option_choice():
option_type = (
click.Choice(['opt_value1_1', 'opt_value1_2']),
click.Choice(['opt_value2_1', 'opt_value2_2']),
str
)

@click.command()
@click.option('--opt', type=option_type)
@click.option('--some-other-opt')
def cli(opt):
pass

assert list(get_choices(cli, 'prog_name', ['--opt'], '')) == \
['opt_value1_1', 'opt_value1_2']
assert list(get_choices(cli, 'prog_name', ['--opt', 'opt_value1_2'], '')) == \
['opt_value2_1', 'opt_value2_2']
assert list(get_choices(cli, 'prog_name', ['--opt', 'opt_value1_2', 'opt_value2_1'], '')) == []