Skip to content

Commit

Permalink
Added support for autocompletion of arguments, intending to fix issue p…
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudio Bandera committed Sep 20, 2015
1 parent 89d3bfd commit c86487b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 2 deletions.
10 changes: 8 additions & 2 deletions click/_bashcomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
import re
from .utils import echo
from .parser import split_arg_string
from .core import MultiCommand, Option

from .core import MultiCommand, Option, Argument

COMPLETION_SCRIPT = '''
%(complete_func)s() {
Expand Down Expand Up @@ -61,6 +60,13 @@ def do_complete(cli, prog_name):
choices.extend(param.secondary_opts)
elif isinstance(ctx.command, MultiCommand):
choices.extend(ctx.command.list_commands(ctx))
else:
for param in ctx.command.params:
if isinstance(param, Argument):
try:
choices.extend(param.autocompletion)
except AttributeError:
pass

for item in choices:
if item.startswith(incomplete):
Expand Down
2 changes: 2 additions & 0 deletions click/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1676,6 +1676,8 @@ def __init__(self, param_decls, required=None, **attrs):
required = False
else:
required = attrs.get('nargs', 1) > 0
if attrs.get('autocompletion') is not None:
self.autocompletion = attrs.pop('autocompletion')
Parameter.__init__(self, param_decls, required=required, **attrs)
if self.default is not None and self.nargs < 0:
raise TypeError('nargs=-1 in combination with a default value '
Expand Down
10 changes: 10 additions & 0 deletions docs/bashcomplete.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ least a dash has been provided. Example::
clone commit copy delete setuser
$ repo clone -<TAB><TAB>
--deep --help --rev --shallow -r
Additionally, custom suggestions can be given to an argument with the ``autocomplete`` parameter as a list of strings. Example::

.. click:example::
@cli.command()
@click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])
def cmd1(name):
click.echo('Name: %s' % name)


Activation
----------
Expand Down
12 changes: 12 additions & 0 deletions examples/bashcompletion/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$ bashcompletion

bashcompletion is a simple example of an application that
tries to autocomplete commands, arguments and options.

This example requires Click 2.0 or higher.

Usage:

$ pip install --editable .
$ eval "$(_BASHCOMPLETION_COMPLETE=source bashcompletion)"
$ bashcompletion --help
21 changes: 21 additions & 0 deletions examples/bashcompletion/bashcompletion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import click

@click.group()
def cli():
pass

@cli.command()
@click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])
@click.option('--debug/--no-debug', default=False)
@click.option('-f', default=False)
def cmd1():
pass

@cli.command()
def cmd2():
pass

@cli.command()
def cmd3():
pass

15 changes: 15 additions & 0 deletions examples/bashcompletion/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from setuptools import setup

setup(
name='click-example-bashcompletion',
version='1.0',
py_modules=['bashcompletion'],
include_package_data=True,
install_requires=[
'click',
],
entry_points='''
[console_scripts]
bashcompletion=bashcompletion:cli
''',
)

0 comments on commit c86487b

Please sign in to comment.