Merge branch 'stopthatcow-feature/fix_chained_args_completion'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 1867ede, 0dfccca · Browse files · 3 files·+44 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+44 added−6 removed
- tests:+26 added−0 removed
- other:+18 added−6 removed
Provenance
- Commit
b224988- Tree
e92dd33- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
1867ede- Parent 2
0dfccca
3 files changed+44 added−6 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 linesmodified:CHANGES+4 added-1 removed
6 unmodified lines7 -----------8 9 (upcoming release with new features, release date to be decided)10 - Added support for dynamic bash completion from a user-supplied callback.11- See #75511+ See #755.12 - Added support for bash completion of type=click.Choice for Options and13 Arguments. See #535.14 - The user is now presented with the available choices if prompt=True and15 type=click.Choice in a click.option. The choices are listed within5 unmodified lines21 - Usage errors now hint at the `--help` option.22 - ``launch`` now works properly under Cygwin. See #650.23 - `CliRunner.invoke` now may receive `args` as a string representing24 a Unix shell command. See #664.25+- Fix bug that caused bashcompletion to give inproper completions on 26+ chained commands. See #774.27 28 Version 6.829 -----------30 2 unmodified lines33 - Disabled sys._getframes() on Python interpreters that don't support it. See34 #728.35 - Fix bug in test runner when calling ``sys.exit`` with ``None``. See #739.36 - Fix crash on Windows console, see #744.37+- Fix bashcompletion on chained commands. See #754.38 39 Version 6.740 -----------41 342 unmodified lines