Skip to content
Myr

Fix bash completion when defaults are present and the way subcommands are handled.

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

This makes the completion logic behave the same way the paser does. Fixes #925 Fixes #919

authored by Nicholas Wiles · committed by Nicholas Wiles · parent cd90e83 · Browse files · 4 files+151 added−55 removed
This commit

State

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

Diff stats

4 files+151 added−55 removed

  • tests:+100 added−24 removed
  • other:+51 added−31 removed

4 files+151 added−55 removed

modified:click/_bashcomplete.py+28 added-18 removed
70 unmodified lines
71 :param prog_name: the program that is running72 :param args: full list of args73 :return: the final context/command parsed74 """75 ctx = cli.make_context(prog_name, args, resilient_parsing=True)76 args_remaining = ctx.protected_args + ctx.args77 while ctx is not None and args_remaining:75 ctx = cli.make_context(prog_name, args, resilient_parsing=True, ignore_default_values=True)76 args = ctx.protected_args + ctx.args77 while args:78 if isinstance(ctx.command, MultiCommand):79 cmd = ctx.command.get_command(ctx, args_remaining[0])80 if cmd is None:81 return None82 ctx = cmd.make_context(83 args_remaining[0], args_remaining[1:], parent=ctx, resilient_parsing=True)84 args_remaining = ctx.protected_args + ctx.args79 if not ctx.command.chain:80 cmd_name, cmd, args = ctx.command.resolve_command(ctx, args)81 if cmd is None:82 return ctx83 ctx = cmd.make_context(cmd_name, args, parent=ctx,84 resilient_parsing=True,85 ignore_default_values=True)86 args = ctx.protected_args + ctx.args87 else:88 # Walk chained subcommand contexts saving the last one.89 while args:90 cmd_name, cmd, args = ctx.command.resolve_command(ctx, args)91 if cmd is None:92 return ctx93 sub_ctx = cmd.make_context(cmd_name, args, parent=ctx,94 allow_extra_args=True,95 allow_interspersed_args=False,96 resilient_parsing=True,97 ignore_default_values=True)98 args = sub_ctx.args99 ctx = sub_ctx100 args = sub_ctx.protected_args + sub_ctx.args101 else:86 ctx = ctx.parent87102 break103 return ctx104105106def start_of_option(param_str):
123 unmodified lines
230 return get_user_autocompletions(ctx, all_args, incomplete, param)231 # completion for argument values from user supplied values232 for param in ctx.command.params:233 if is_incomplete_argument(ctx.params, param):219 completions.extend(get_user_autocompletions(220 ctx, all_args, incomplete, param))221 # Stop looking for other completions only if this argument is required.222 if param.required:223 return completions224 break234 return get_user_autocompletions(ctx, all_args, incomplete, param)235236 add_subcommand_completions(ctx, incomplete, completions)237 return completions238
22 unmodified lines
261 echo(get_completion_script(prog_name, complete_var, shell))262 return True263 elif complete_instr == 'complete' or complete_instr == 'complete_zsh':264 return do_complete(cli, prog_name, complete_instr == 'complete_zsh')255 return False265 return False
modified:click/core.py+9 added-3 removed
212 unmodified lines
213 terminal_width=None, max_content_width=None,214 resilient_parsing=False, allow_extra_args=None,215 allow_interspersed_args=None,216 ignore_unknown_options=None, help_option_names=None,217 token_normalize_func=None, color=None):217 token_normalize_func=None, color=None, ignore_default_values=False):218 #: the parent context or `None` if none exists.219 self.parent = parent220 #: the :class:`Command` for this context.221 self.command = command
92 unmodified lines
314 #: Indicates if resilient parsing is enabled. In that case Click315 #: will do its best to not cause any failures.316 self.resilient_parsing = resilient_parsing317318 #: Indicates that default values should be ignored.319 #: Useful for completion.320321 #: .. versionadded:: 7.0322 self.ignore_default_values = ignore_default_values323324 # If there is no envvar prefix yet, but the parent has one and325 # the command on this level has a name, we can expand the envvar326 # prefix automatically.327 if auto_envvar_prefix is None:
830 unmodified lines
1158 # something else we should do: if the first argument looks like1159 # an option we want to kick off parsing again for arguments to1160 # resolve things like --help which now should go to the main1161 # place.1156 if cmd is None:1162 if cmd is None and not ctx.resilient_parsing:1163 if split_opt(cmd_name)[0]:1164 self.parse_args(ctx, ctx.args)1165 ctx.fail('No such command "%s".' % original_cmd_name)1166
247 unmodified lines
14141415 def full_process_value(self, ctx, value):1416 value = self.process_value(ctx, value)14171412 if value is None:1418 if value is None and not ctx.ignore_default_values:1419 value = self.get_default(ctx)14201421 if self.required and self.value_is_missing(value):1422 raise MissingParameter(ctx=ctx, param=self)
414 unmodified lines
modified:tests/test_bashcomplete.py+100 added-24 removed
130 unmodified lines
131132def test_chaining():133 @click.group('cli', chain=True)134 @click.option('--cli-opt')135 @click.argument('arg', type=click.Choice(['cliarg1', 'cliarg2']))135 def cli(cli_opt):136 def cli(cli_opt, arg):137 pass138139 @cli.command()140 @click.option('--asub-opt')
1 unmodified line
142 pass143144 @cli.command(help='bsub help')145 @click.option('--bsub-opt')145 @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)146 @click.argument('arg', type=click.Choice(['arg1', 'arg2']))147 def bsub(bsub_opt, arg):148 pass149150 @cli.command()151 @click.option('--csub-opt')151 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False)152 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), default='carg1')153 def csub(csub_opt, arg):154 pass155156 assert choices_without_help(cli, [], '-') == ['--cli-opt']156 assert choices_without_help(cli, [], '') == ['asub', 'bsub', 'csub']157 assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']158 assert choices_without_help(cli, ['asub'], '') == ['bsub', 'csub']159 assert choices_without_help(cli, ['bsub'], '') == ['arg1', 'arg2']160 assert choices_without_help(cli, ['asub', '--asub-opt'], '') == []161 assert choices_without_help(cli, ['asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']162 assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']163 assert choices_with_help(cli, ['asub'], 'b') == [('bsub', 'bsub help')]164 assert choices_without_help(cli, ['asub', 'csub'], '-') == ['--csub-opt']157 assert choices_without_help(cli, [], '') == ['cliarg1', 'cliarg2']158 assert choices_without_help(cli, ['cliarg1', 'asub'], '-') == ['--asub-opt']159 assert choices_without_help(cli, ['cliarg1', 'asub'], '') == ['bsub', 'csub']160 assert choices_without_help(cli, ['cliarg1', 'bsub'], '') == ['arg1', 'arg2']161 assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt'], '') == []162 assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']163 assert choices_without_help(cli, ['cliarg1', 'asub', 'bsub'], '-') == ['--bsub-opt']164 assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '') == ['carg1', 'carg2']165 assert choices_without_help(cli, ['cliarg1', 'bsub', 'arg1', 'csub'], '') == ['carg1', 'carg2']166 assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '-') == ['--csub-opt']167 assert choices_with_help(cli, ['cliarg1', 'asub'], 'b') == [('bsub', 'bsub help')]168169170def test_argument_choice():171 @click.command()169 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))170 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))171 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))172 @click.argument('arg1', required=True, type=click.Choice(['arg11', 'arg12']))173 @click.argument('arg2', type=click.Choice(['arg21', 'arg22']), default='arg21')174 @click.argument('arg3', type=click.Choice(['arg', 'argument']), default='arg')175 def cli():176 pass177178 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']
5 unmodified lines
184185def test_option_choice():186 @click.command()187 @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')185 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))188 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']), default='opt21')189 @click.option('--opt3', type=click.Choice(['opt', 'option']))190 def cli():191 pass192
27 unmodified lines
220 assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']221 assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']222 assert choices_without_help(cli, [], '') == ['arg11', 'arg12']223 assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']224 assert choices_without_help(cli, ['arg11'], '--opt') == ['--opt1', '--opt2']225 assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2']226227228def test_boolean_flag_choice():229 @click.command()
32 unmodified lines
262263264def test_variadic_argument_choice():265 @click.command()266 @click.option('--opt', type=click.Choice(['opt1', 'opt2']))267 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))268 def cli(local_opt):269 pass270271 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']272 assert choices_without_help(cli, ['src1', 'src2'], '--o') == ['--opt']273 assert choices_without_help(cli, ['src1', 'src2', '--opt'], '') == ['opt1', 'opt2']274 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']275276277def test_variadic_argument_complete():278279 def _complete(ctx, args, incomplete):280 return ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stu', 'vwx', 'yz']281282 @click.group()283 def entrypoint():284 pass285286 @click.command()287 @click.option('--opt', autocompletion=_complete)288 @click.argument('arg', nargs=-1)289 def subcommand(opt, arg):290 pass291292 entrypoint.add_command(subcommand)293294 assert choices_without_help(entrypoint, ['subcommand', '--opt'], '') == _complete(0,0,0)295 assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt'], '') == _complete(0,0,0)296 assert choices_without_help(entrypoint, ['subcommand', 'whatever', '--opt', 'abc'], '') == []297298299def test_long_chain_choice():300 @click.group()301 def cli():302 pass303304 @cli.group()305 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))306 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))276 def sub(sub_opt):307 def sub(sub_opt, sub_arg):308 pass309310 @sub.command(short_help='bsub help')311 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))
1 unmodified line
313 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))314 def bsub(bsub_opt):315 pass316286 assert choices_with_help(cli, ['sub'], '') == [('subarg1', None), ('subarg2', None), ('bsub', 'bsub help')]317 @sub.group('csub')318 def csub():319 pass320321 @csub.command()322 def dsub():323 pass324325 assert choices_with_help(cli, ['sub', 'subarg1'], '') == [('bsub', 'bsub help'), ('csub', '')]326 assert choices_without_help(cli, ['sub'], '') == ['subarg1', 'subarg2']327 assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']328 assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \289 ['subarg1', 'subarg2', 'bsub']329 ['subarg1', 'subarg2']330 assert choices_without_help(cli,291 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']331 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']332 assert choices_without_help(cli,293 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \294 ['bsubopt1', 'bsubopt2']333 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '') == ['bsubarg1', 'bsubarg2']334 assert choices_without_help(cli,335 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \336 ['bsubopt1', 'bsubopt2']337 assert choices_without_help(cli,296 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],297 '') == ['bbsubarg1', 'bbsubarg2']338 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],339 '') == ['bbsubarg1', 'bbsubarg2']340 assert choices_without_help(cli,341 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'csub'],342 '') == ['dsub']343344345def test_chained_multi():346 @click.group()347 def cli():348 pass349350 @cli.group()351 def sub():352 pass353354 @sub.group()355 def bsub():356 pass357358 @sub.group(chain=True)359 def csub():360 pass361362 @csub.command()363 def dsub():364 pass365366 @csub.command()367 def esub():368 pass369370 assert choices_without_help(cli, ['sub'], '') == ['bsub', 'csub']371 assert choices_without_help(cli, ['sub'], 'c') == ['csub']372 assert choices_without_help(cli, ['sub', 'csub'], '') == ['dsub', 'esub']373 assert choices_without_help(cli, ['sub', 'csub', 'dsub'], '') == ['esub']
modified:CHANGES.rst+14 added-10 removed
72 unmodified lines
73- Use deterministic option name; can't rely on list sort. (`#794`_)(`#793`_)74- Added support for bash completions containing spaces. (`#773`_)75- Added support for dynamic bash completion from a user-supplied callback.76 (`#755`_)77- Fix bug that caused bashcompletion to give improper completions on78 chained commands when a required option/argument was being completed.79 (`#790`_)(`#806`_)80- Allow autocompletion function to determine whether or not to return81 completions that start with the incomplete argument.82- Add native ZSH autocompletion support. (`#323`_)(`#865`_)83- Add support for auto-completion documentation. (`#866`_)(`#869`_)84- Added support for bash completion of type=click.Choice for Options and85 Arguments. (`#535`_)86- Fix bug that caused bashcompletion to give improper completions on87 chained commands. (`#774`_)88- Fix completion of subcommand options after last argument (`#919`_)89- Fix incorrect completions when defaults are present (`#925`_)90- The user is now presented with the available choices if prompt=True and91 type=click.Choice in a click.option. The choices are listed within92 parenthesis like 'Choose fruit (apple, orange): '.93- The exception objects now store unicode properly.
3 unmodified lines
97- Usage errors now hint at the `--help` option.98- ``launch`` now works properly under Cygwin. (`#650`_)99- `CliRunner.invoke` now may receive `args` as a string representing100 a Unix shell command. See (`#664`_).90- Fix bug that caused bashcompletion to give improper completions on91 chained commands. (`#774`_)101- Add support for bright colors.102- 't' and 'f' are now converted to True and False.94- Fix bug that caused bashcompletion to give improper completions on95 chained commands when a required option/argument was being completed.96 (`#790`_)(`#806`_)97- Allow autocompletion function to determine whether or not to return98 completions that start with the incomplete argument.99- Add native ZSH autocompletion support. (`#323`_)(`#865`_)100- Add support for auto-completion documentation. See (`#866`_)(`#869`_)103- Subcommands that are named by the function now automatically have the104 underscore replaced with a dash. So if you register a function named105 `my_command` it becomes `my-command` in the command line interface.106- Stdout is now automatically set to non blocking.
1 unmodified line
108 path so that changing working directories does not harm it.109- Force stdout/stderr writable. This works around issues with badly patched110 standard streams like those from jupyter.111112.. _#866: https://github.com/pallets/click/issues/866113.. _#919: https://github.com/pallets/click/issues/919114.. _#925: https://github.com/pallets/click/issues/925115.. _#1027: https://github.com/pallets/click/pull/1027116.. _#1012: https://github.com/pallets/click/pull/1012117.. _#447: https://github.com/pallets/click/issues/447118.. _#1022: https://github.com/pallets/click/pull/1022
78 unmodified lines
197.. _#664: https://github.com/pallets/click/pull/664198.. _#774: https://github.com/pallets/click/pull/774199.. _#790: https://github.com/pallets/click/pull/790200196201Version 6.8202-----------203204(bugfix release; yet to be released)
356 unmodified lines