Backport fixes for autocompletion on chained commands
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 5701c40 · 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
Provenance
- Commit
29ab0b0- Tree
63aaeb0- Author
Nicholas Wiles <nwiles@google.com>- Committer
Nicholas Wiles <nwiles@google.com>- Parent
5701c40
3 files changed+42 added−6 removed
modified:click/_bashcomplete.py+16 added-6 removed
28 unmodified lines29 30 31 def 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.parent43+44 return ctx45 41-46 def get_choices(cli, prog_name, args, incomplete):47 ctx = resolve_ctx(cli, prog_name, args)48 if ctx is None:49 return50 51 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))61 62+ 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)66+67 for item in choices:68 if item.startswith(incomplete):69 yield item70 23 unmodified linesmodified:tests/test_bashcomplete.py+25 added
58 unmodified lines59 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'], '')) == []63+64+65+def test_chaining():66+ @click.group('cli', chain=True)67+ @click.option('--cli-opt')68+ def cli(cli_opt):69+ pass70+71+ @cli.command('asub')72+ @click.option('--asub-opt')73+ def asub(asub_opt):74+ pass75+76+ @cli.command('bsub')77+ @click.option('--bsub-opt')78+ def bsub(bsub_opt, arg):79+ pass80+81+ 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 lines11 - 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.16 17 Version 6.718 -----------19 343 unmodified lines