Fix bashautocompletion of arguments 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 5f2ff4f · Browse files · 2 files·+40 added−5 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+40 added−5 removed
- tests:+26 added−0 removed
- other:+14 added−5 removed
Provenance
- Commit
92c8053- Tree
83d4a4b- Author
Nicholas Wiles <nwiles@google.com>- Committer
Nicholas Wiles <nwiles@google.com>- Parent
5f2ff4f
2 files changed+40 added−5 removed
modified:click/_bashcomplete.py+14 added-5 removed
40 unmodified lines41 :param args: full list of args42 :return: the final context/command parsed43 """44 ctx = cli.make_context(prog_name, args, resilient_parsing=True)45- while ctx.protected_args + ctx.args and isinstance(ctx.command, MultiCommand):46- a = ctx.protected_args + ctx.args47- cmd = ctx.command.get_command(ctx, a[0])45+ args_remaining = ctx.protected_args + ctx.args46+ while ctx is not None and args_remaining:47+ if isinstance(ctx.command, MultiCommand):48+ cmd = ctx.command.get_command(ctx, args_remaining[0])49 if cmd is None:50 return None50- ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)51+ ctx = cmd.make_context(args_remaining[0], args_remaining[1:], parent=ctx, resilient_parsing=True)52+ args_remaining = ctx.protected_args + ctx.args53+ else:54+ ctx = ctx.parent55+56 return ctx57 53-58 def start_of_option(param_str):59 """60 :param param_str: param_str to check61 :return: whether or not this is the start of an option declaration (i.e. starts "-" or "--")105 unmodified lines167 if not found_param and isinstance(ctx.command, MultiCommand):168 # completion for any subcommands169 choices.extend(ctx.command.list_commands(ctx))170 171+ if not start_of_option(incomplete) and ctx.parent is not None and isinstance(ctx.parent.command, MultiCommand) and ctx.parent.command.chain:172+ # completion for chained commands173+ remaining_comands = set(ctx.parent.command.list_commands(ctx.parent))-set(ctx.parent.protected_args)174+ choices.extend(remaining_comands)175+176 for item in choices:177 if item.startswith(incomplete):178 yield item179 23 unmodified linesmodified:tests/test_bashcomplete.py+26 added
110 unmodified lines111 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == COLORS112 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], 'b')) == ['blue']113 114 115+def test_chaining():116+ @click.group('cli', chain=True)117+ @click.option('--cli-opt')118+ def cli(cli_opt):119+ pass120+121+ @cli.command('asub')122+ @click.option('--asub-opt')123+ def asub(asub_opt):124+ pass125+126+ @cli.command('bsub')127+ @click.option('--bsub-opt')128+ @click.argument('arg', type=click.Choice(['arg1', 'arg2']))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']137+ assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']138+ assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']139+140+141 def test_argument_choice():142 @click.command()143 @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12']))144 @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22']))125 unmodified lines