fix overzealous completion when required options are present
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicholas Wiles · committed by Nicholas Wiles · parent e634d7f · Browse files · 3 files·+38 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+38 added−25 removed
- tests:+5 added−4 removed
- other:+33 added−21 removed
Provenance
- Commit
65da5cc- Tree
5b41c7f- Author
Nicholas Wiles <nwiles@google.com>- Committer
Nicholas Wiles <nwiles@google.com>- Parent
e634d7f
3 files changed+38 added−25 removed
modified:click/_bashcomplete.py+30 added-21 removed
54 unmodified lines55 ctx = ctx.parent56 57 return ctx58 59+60 def start_of_option(param_str):61 """62 :param param_str: param_str to check63 :return: whether or not this is the start of an option declaration (i.e. starts "-" or "--")8 unmodified lines72 :return: whether or not the last option declaration (i.e. starts "-" or "--") is incomplete and73 corresponds to this cmd_param. In other words whether this cmd_param option can still accept74 values75 """76+ if not isinstance(cmd_param, Option):77+ return False78 if cmd_param.is_flag:79 return False80 last_option = None81 for index, arg_str in enumerate(reversed([arg for arg in all_args if arg != WORDBREAK])):11 unmodified lines93 :param cmd_param: the current command parameter94 :return: whether or not the last argument is incomplete and corresponds to this cmd_param. In95 other words whether or not the this cmd_param argument can still accept values96 """97+ if not isinstance(cmd_param, Argument):98+ return False99 current_param_values = current_params[cmd_param.name]100 if current_param_values is None:101 return True102 if cmd_param.nargs == -1:19 unmodified lines122 incomplete=incomplete)123 else:124 return []125 126+127 def get_choices(cli, prog_name, args, incomplete):128 """129 :param cli: command definition130 :param prog_name: the program that is running18 unmodified lines149 150 choices = []151 found_param = False152 if start_of_option(incomplete):147- # completions for options153+ # completions for partial options154 for param in ctx.command.params:155 if isinstance(param, Option):156 choices.extend([param_opt for param_opt in param.opts + param.secondary_opts157 if param_opt not in all_args or param.multiple])152- found_param = True153- if not found_param:154- # completion for option values by choices155- for cmd_param in ctx.command.params:156- if isinstance(cmd_param, Option) and is_incomplete_option(all_args, cmd_param):157- choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))158+ else:159+ # completion for option values from user supplied values160+ for param in ctx.command.params:161+ if is_incomplete_option(all_args, param):162+ choices.extend(get_user_autocompletions(ctx, all_args, incomplete, param))163 found_param = True164 break160- if not found_param:161- # completion for argument values by choices162- for cmd_param in ctx.command.params:163- if isinstance(cmd_param, Argument) and is_incomplete_argument(ctx.params, cmd_param):164- choices.extend(get_user_autocompletions(ctx, all_args, incomplete, cmd_param))165- found_param = True166- break165+ # completion for argument values from user supplied values166+ if not found_param:167+ for param in ctx.command.params:168+ if is_incomplete_argument(ctx.params, param):169+ choices.extend(get_user_autocompletions(ctx, all_args, incomplete, param))170+ # Stop looking for other completions only if this argument is required171+ found_param = param.required172+ break173 168- if not found_param and isinstance(ctx.command, MultiCommand):169- # completion for any subcommands170- choices.extend(ctx.command.list_commands(ctx))174+ if not found_param and isinstance(ctx.command, MultiCommand):175+ # completion for any subcommands176+ choices.extend(ctx.command.list_commands(ctx))177 172- if not start_of_option(incomplete) and ctx.parent is not None and isinstance(ctx.parent.command, MultiCommand) and ctx.parent.command.chain:173- # completion for chained commands174- remaining_comands = set(ctx.parent.command.list_commands(ctx.parent))-set(ctx.parent.protected_args)175- choices.extend(remaining_comands)178+ if not found_param:179+ # Walk up the context list and add any other completion possibilities from chained commands180+ while ctx.parent is not None:181+ ctx = ctx.parent182+ if isinstance(ctx.command, MultiCommand) and ctx.command.chain:183+ remaining_commands = set(ctx.command.list_commands(ctx))-set(ctx.protected_args)184+ choices.extend(remaining_commands)185 186 for item in choices:187 if item.startswith(incomplete):188 yield item24 unmodified linesmodified:tests/test_bashcomplete.py+5 added-4 removed
123 unmodified lines124 pass125 126 @cli.command('bsub')127 @click.option('--bsub-opt')128- @click.argument('arg', type=click.Choice(['arg1', 'arg2']))128+ @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)129 def bsub(bsub_opt, arg):130 pass131 132 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']133 assert list(get_choices(cli, 'lol', [], '')) == ['asub', 'bsub']134 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']135 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']136- assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['arg1', 'arg2', 'asub']136+ assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['arg1', 'arg2']137+ assert list(get_choices(cli, 'lol', ['asub', '--asub-opt'], '')) == []138 assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']139 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']140 141 113 unmodified lines255 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))256 def bsub(bsub_opt):257 pass258 258- assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2']259+ assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2', 'bsub']260 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2']261 assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \261- ['subarg1', 'subarg2']262+ ['subarg1', 'subarg2', 'bsub']263 assert list(get_choices(cli, 'lol',264 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt']265 assert list(get_choices(cli, 'lol',266 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \4 unmodified linesmodified:CHANGES+3 added
23 unmodified lines24 - `CliRunner.invoke` now may receive `args` as a string representing25 a Unix shell command. See #664.26 - Fix bug that caused bashcompletion to give inproper completions on 27 chained commands. See #774.28+- Fix bug that caused bashcompletion to give inproper completions on29+ chained commands when a required option/argument was being completed.30+ See #790.31 32 Version 6.833 -----------34 354 unmodified lines