Skip to content

Commit

Permalink
Use already-parsed values of aruments to see whether they should be c…
Browse files Browse the repository at this point in the history
…ompleted.
  • Loading branch information
Brett Bethke committed Dec 21, 2015
1 parent e27493b commit 35c6cc7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions click/_bashcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def do_complete(cli, prog_name):
else:
for param in ctx.command.params:
if isinstance(param, Argument):
arguments = [k for k in args if k != ctx.command.name]
if len(arguments) <= param.nargs or param.nargs == -1:
value = ctx.params[param.name]
if (value == None) or (isinstance(value, tuple) and (len(value) < param.nargs or param.nargs == -1)):
try:
if callable(param.autocompletion):
choices.extend(param.autocompletion(ctx=ctx,
Expand All @@ -75,6 +75,7 @@ def do_complete(cli, prog_name):
choices.extend(param.autocompletion)
except AttributeError:
pass
break

for item in choices:
if item.startswith(incomplete):
Expand Down
9 changes: 6 additions & 3 deletions examples/bashcompletion/bashcompletion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ def cli():
pass

@cli.command()
@click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])
def cmd1(name):
click.echo('Name: %s' % name)
@click.option('-c', '--count', type=click.INT, default=1)
@click.argument("first", type=click.STRING, autocompletion=["John", "Bob", "Fred"])
@click.argument("last", type=click.STRING, autocompletion=["Smith", "Simon", "Doe"])
def cmd1(count, first, last):
for c in range(count):
click.echo('Name: %s %s' % (first, last))

def get_env_vars(ctx, incomplete, cwords, cword):
return os.environ.keys()
Expand Down

0 comments on commit 35c6cc7

Please sign in to comment.