Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
bashcomplete: Adapt to changes for chain commands
Done in 0a2919f

Fix #471
  • Loading branch information
untitaker committed Mar 23, 2016
1 parent 57c6f09 commit 37011e7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
35 changes: 20 additions & 15 deletions click/_bashcomplete.py
Expand Up @@ -30,27 +30,19 @@ def get_completion_script(prog_name, complete_var):

def resolve_ctx(cli, prog_name, args):
ctx = cli.make_context(prog_name, args, resilient_parsing=True)
while ctx.args and isinstance(ctx.command, MultiCommand):
cmd = ctx.command.get_command(ctx, ctx.args[0])
while ctx.args + ctx.protected_args and isinstance(ctx.command, MultiCommand):
a = ctx.args + ctx.protected_args
cmd = ctx.command.get_command(ctx, a[0])
if cmd is None:
return None
ctx = cmd.make_context(ctx.args[0], ctx.args[1:], parent=ctx,
resilient_parsing=True)
ctx = cmd.make_context(a[0], a[1:], parent=ctx, resilient_parsing=True)
return ctx


def do_complete(cli, prog_name):
cwords = split_arg_string(os.environ['COMP_WORDS'])
cword = int(os.environ['COMP_CWORD'])
args = cwords[1:cword]
try:
incomplete = cwords[cword]
except IndexError:
incomplete = ''

def get_choices(cli, prog_name, args, incomplete):
ctx = resolve_ctx(cli, prog_name, args)
if ctx is None:
return True
return

choices = []
if incomplete and not incomplete[:1].isalnum():
Expand All @@ -64,7 +56,20 @@ def do_complete(cli, prog_name):

for item in choices:
if item.startswith(incomplete):
echo(item)
yield item


def do_complete(cli, prog_name):
cwords = split_arg_string(os.environ['COMP_WORDS'])
cword = int(os.environ['COMP_CWORD'])
args = cwords[1:cword]
try:
incomplete = cwords[cword]
except IndexError:
incomplete = ''

for item in get_choices(cli, prog_name, args, incomplete):
echo(item)

return True

Expand Down
20 changes: 20 additions & 0 deletions tests/test_bashcomplete.py
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-

import click
from click._bashcomplete import get_choices

def test_basic():
@click.group()
@click.option('--global-opt')
def cli(global_opt):
pass

@cli.command()
@click.option('--local-opt')
def sub(local_opt):
pass

assert list(get_choices(cli, 'lol', [], '')) == ['sub']
assert list(get_choices(cli, 'lol', [], '-')) == ['--global-opt']
assert list(get_choices(cli, 'lol', ['sub'], '')) == []
assert list(get_choices(cli, 'lol', ['sub'], '-')) == ['--local-opt']

0 comments on commit 37011e7

Please sign in to comment.