Skip to content
Myr

Merge pull request #545 from mitsuhiko/bashcomplete-arg-shift

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 84e1572, 9e7d158 · Browse files · 3 files+48 added−15 removed
This commit

State

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

Trailers

  • bashcomplete: Adapt to changes for chain commands

Diff stats

3 files+48 added−15 removed

  • tests:+20 added−0 removed
  • other:+28 added−15 removed

3 files+48 added−15 removed

modified:click/_bashcomplete.py+20 added-15 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 and isinstance(ctx.command, MultiCommand):34 cmd = ctx.command.get_command(ctx, ctx.args[0])33 while ctx.args + ctx.protected_args and isinstance(ctx.command, MultiCommand):34 a = ctx.args + ctx.protected_args35 cmd = ctx.command.get_command(ctx, a[0])36 if cmd is None:37 return None37 ctx = cmd.make_context(ctx.args[0], ctx.args[1:], parent=ctx,38 resilient_parsing=True)38 ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)39 return ctx404142def do_complete(cli, prog_name):43 cwords = split_arg_string(os.environ['COMP_WORDS'])44 cword = int(os.environ['COMP_CWORD'])45 args = cwords[1:cword]46 try:47 incomplete = cwords[cword]48 except IndexError:49 incomplete = ''5042def get_choices(cli, prog_name, args, incomplete):43 ctx = resolve_ctx(cli, prog_name, args)44 if ctx is None:53 return True45 return4647 choices = []48 if incomplete and not incomplete[:1].isalnum():49 for param in ctx.command.params:
5 unmodified lines
55 choices.extend(ctx.command.list_commands(ctx))5657 for item in choices:58 if item.startswith(incomplete):67 echo(item)59 yield item606162def do_complete(cli, prog_name):63 cwords = split_arg_string(os.environ['COMP_WORDS'])64 cword = int(os.environ['COMP_CWORD'])65 args = cwords[1:cword]66 try:67 incomplete = cwords[cword]68 except IndexError:69 incomplete = ''7071 for item in get_choices(cli, prog_name, args, incomplete):72 echo(item)7374 return True757677def bashcomplete(cli, prog_name, complete_var, complete_instr):
6 unmodified lines
added:tests/test_bashcomplete.py+20 added
1# -*- coding: utf-8 -*-23import click4from click._bashcomplete import get_choices56def test_basic():7 @click.group()8 @click.option('--global-opt')9 def cli(global_opt):10 pass1112 @cli.command()13 @click.option('--local-opt')14 def sub(local_opt):15 pass1617 assert list(get_choices(cli, 'lol', [], '')) == ['sub']18 assert list(get_choices(cli, 'lol', [], '-')) == ['--global-opt']19 assert list(get_choices(cli, 'lol', ['sub'], '')) == []20 assert list(get_choices(cli, 'lol', ['sub'], '-')) == ['--local-opt']
modified:CHANGES+8 added
1 unmodified line
2===============34This contains all major version changes between Click releases.56Version 6.47-----------89(bugfix release)1011- Fix bug in bash completion where click would discard one or more trailing12 arguments. See #471.1314Version 6.315-----------1617(bugfix release; released on February 22 2016)
316 unmodified lines