Skip to content
Myr

Merge pull request #778 from stopthatcow/hotfix/fix_bashcompletion_on_chained_commands

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

Backport fixes for autocompletion on chained commands

authored by Markus Unterwaditzer · committed by GitHub · parents 5701c40, 29ab0b0 · Browse files · 3 files+42 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

3 files+42 added−6 removed

  • tests:+25 added−0 removed
  • other:+17 added−6 removed

3 files+42 added−6 removed

modified:click/_bashcomplete.py+16 added-6 removed
28 unmodified lines
293031def resolve_ctx(cli, prog_name, args):32 ctx = cli.make_context(prog_name, args, resilient_parsing=True)33 while ctx.protected_args + ctx.args and isinstance(ctx.command, MultiCommand):34 a = ctx.protected_args + ctx.args35 cmd = ctx.command.get_command(ctx, a[0])33 args_remaining = ctx.protected_args + ctx.args34 while ctx is not None and args_remaining:35 if isinstance(ctx.command, MultiCommand):36 cmd = ctx.command.get_command(ctx, args_remaining[0])37 if cmd is None:38 return None38 ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)39 ctx = cmd.make_context(args_remaining[0], args_remaining[1:], parent=ctx, resilient_parsing=True)40 args_remaining = ctx.protected_args + ctx.args41 else:42 ctx = ctx.parent4344 return ctx454146def get_choices(cli, prog_name, args, incomplete):47 ctx = resolve_ctx(cli, prog_name, args)48 if ctx is None:49 return5051 choices = []48 if incomplete and not incomplete[:1].isalnum():52 incomplete_is_start_of_option = incomplete and not incomplete[:1].isalnum()53 if incomplete_is_start_of_option:54 for param in ctx.command.params:55 if not isinstance(param, Option):56 continue57 choices.extend(param.opts)58 choices.extend(param.secondary_opts)59 elif isinstance(ctx.command, MultiCommand):60 choices.extend(ctx.command.list_commands(ctx))6162 if not incomplete_is_start_of_option and ctx.parent is not None and isinstance(ctx.parent.command, MultiCommand) and ctx.parent.command.chain:63 # completion for chained commands64 remaining_comands = set(ctx.parent.command.list_commands(ctx.parent))-set(ctx.parent.protected_args)65 choices.extend(remaining_comands)6667 for item in choices:68 if item.startswith(incomplete):69 yield item70
23 unmodified lines
modified:tests/test_bashcomplete.py+25 added
58 unmodified lines
59 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']60 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub']61 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt']62 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == []636465def test_chaining():66 @click.group('cli', chain=True)67 @click.option('--cli-opt')68 def cli(cli_opt):69 pass7071 @cli.command('asub')72 @click.option('--asub-opt')73 def asub(asub_opt):74 pass7576 @cli.command('bsub')77 @click.option('--bsub-opt')78 def bsub(bsub_opt, arg):79 pass8081 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']82 assert list(get_choices(cli, 'lol', [], '')) == ['asub', 'bsub']83 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']84 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']85 assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['asub']86 assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']87 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']
modified:CHANGES+1 added
10 unmodified lines
11- Disabled sys._getframes() on Python interpreters that don't support it. See12 #728.13- Fix bug in test runner when calling ``sys.exit`` with ``None``. See #739.14- Fix crash on Windows console, see #744.15- Fix bashcompletion on chained commands. See #754.1617Version 6.718-----------19
343 unmodified lines