Skip to content
Myr

Added support for autocompletion of arguments, intending to fix issue #241

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

authored by Claudio Bandera · committed by Claudio Bandera · parent 89d3bfd · Browse files · 6 files+68 added−2 removed
This commit

State

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

Diff stats

6 files+68 added−2 removed

  • other:+68 added−2 removed

6 files+68 added−2 removed

modified:click/_bashcomplete.py+8 added-2 removed
1import os2import re3from .utils import echo4from .parser import split_arg_string5from .core import MultiCommand, Option5from .core import MultiCommand, Option, Argument677COMPLETION_SCRIPT = '''8%(complete_func)s() {9 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\10 COMP_CWORD=$COMP_CWORD \\
48 unmodified lines
59 choices.extend(param.opts)60 choices.extend(param.secondary_opts)61 elif isinstance(ctx.command, MultiCommand):62 choices.extend(ctx.command.list_commands(ctx))63 else:64 for param in ctx.command.params:65 if isinstance(param, Argument):66 try:67 choices.extend(param.autocompletion)68 except AttributeError:69 pass7071 for item in choices:72 if item.startswith(incomplete):73 echo(item)
11 unmodified lines
modified:click/core.py+2 added
1674 unmodified lines
1675 if attrs.get('default') is not None:1676 required = False1677 else:1678 required = attrs.get('nargs', 1) > 01679 if attrs.get('autocompletion') is not None:1680 self.autocompletion = attrs.pop('autocompletion')1681 Parameter.__init__(self, param_decls, required=required, **attrs)1682 if self.default is not None and self.nargs < 0:1683 raise TypeError('nargs=-1 in combination with a default value '1684 'is not supported.')
42 unmodified lines
modified:docs/bashcomplete.rst+10 added
27 unmodified lines
28 $ repo <TAB><TAB>29 clone commit copy delete setuser30 $ repo clone -<TAB><TAB>31 --deep --help --rev --shallow -r32 33Additionally, custom suggestions can be given to an argument with the ``autocomplete`` parameter as a list of strings. Example::3435.. click:example::3637 @cli.command()38 @click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])39 def cmd1(name):40 click.echo('Name: %s' % name)414243Activation44----------4546In order to activate Bash completion, you need to inform Bash that
33 unmodified lines
added:examples/bashcompletion/bashcompletion.py+21 added
1import click23@click.group()4def cli():5 pass67@cli.command()8@click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])9@click.option('--debug/--no-debug', default=False)10@click.option('-f', default=False)11def cmd1():12 pass1314@cli.command()15def cmd2():16 pass1718@cli.command()19def cmd3():20 pass21
added:examples/bashcompletion/README+12 added
1$ bashcompletion23 bashcompletion is a simple example of an application that4 tries to autocomplete commands, arguments and options.56 This example requires Click 2.0 or higher.78Usage:910 $ pip install --editable .11 $ eval "$(_BASHCOMPLETION_COMPLETE=source bashcompletion)"12 $ bashcompletion --help
added:examples/bashcompletion/setup.py+15 added
1from setuptools import setup23setup(4 name='click-example-bashcompletion',5 version='1.0',6 py_modules=['bashcompletion'],7 include_package_data=True,8 install_requires=[9 'click',10 ],11 entry_points='''12 [console_scripts]13 bashcompletion=bashcompletion:cli14 ''',15)