Skip to content
Myr

Fix #630: Bash completion for nested commands broken

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

When concatenating protected_args and args, protected_args should be prepended. Otherwise the arguments are out of order and an incorrect subcommand is choosen.

authored by Peter Kelley · committed by Markus Unterwaditzer · parent 7e8324f · Browse files · 2 files+45 added−3 removed
This commit

State

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

Diff stats

2 files+45 added−3 removed

  • tests:+43 added−1 removed
  • other:+2 added−2 removed

2 files+45 added−3 removed

modified:click/_bashcomplete.py+2 added-2 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.args + ctx.protected_args and isinstance(ctx.command, MultiCommand):34 a = ctx.args + ctx.protected_args33 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])36 if cmd is None:37 return None38 ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)
45 unmodified lines
modified:tests/test_bashcomplete.py+43 added-1 removed
1 unmodified line
23import click4from click._bashcomplete import get_choices56def test_basic():67def test_single_command():8 @click.command()9 @click.option('--local-opt')10 def cli(local_opt):11 pass1213 assert list(get_choices(cli, 'lol', [], '-')) == ['--local-opt']14 assert list(get_choices(cli, 'lol', [], '')) == []151617def test_small_chain():18 @click.group()19 @click.option('--global-opt')20 def cli(global_opt):21 pass
6 unmodified lines
28 assert list(get_choices(cli, 'lol', [], '')) == ['sub']29 assert list(get_choices(cli, 'lol', [], '-')) == ['--global-opt']30 assert list(get_choices(cli, 'lol', ['sub'], '')) == []31 assert list(get_choices(cli, 'lol', ['sub'], '-')) == ['--local-opt']323334def test_long_chain():35 @click.group('cli')36 @click.option('--cli-opt')37 def cli(cli_opt):38 pass3940 @cli.group('asub')41 @click.option('--asub-opt')42 def asub(asub_opt):43 pass4445 @asub.group('bsub')46 @click.option('--bsub-opt')47 def bsub(bsub_opt):48 pass4950 @bsub.command('csub')51 @click.option('--csub-opt')52 def csub(csub_opt):53 pass5455 assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']56 assert list(get_choices(cli, 'lol', [], '')) == ['asub']57 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']58 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']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'], '')) == []