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
Provenance
- Commit
c86487b- Tree
a796641- Author
Claudio Bandera <claudio.bandera@kit.edu>- Committer
Claudio Bandera <claudio.bandera@kit.edu>- Parent
89d3bfd
6 files changed+68 added−2 removed
modified:click/_bashcomplete.py+8 added-2 removed
1 import os2 import re3 from .utils import echo4 from .parser import split_arg_string5-from .core import MultiCommand, Option5+from .core import MultiCommand, Option, Argument6 7-7 COMPLETION_SCRIPT = '''8 %(complete_func)s() {9 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\10 COMP_CWORD=$COMP_CWORD \\48 unmodified lines59 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+ pass70 71 for item in choices:72 if item.startswith(incomplete):73 echo(item)11 unmodified linesmodified:click/core.py+2 added
1674 unmodified lines1675 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 linesmodified:docs/bashcomplete.rst+10 added
27 unmodified lines28 $ repo <TAB><TAB>29 clone commit copy delete setuser30 $ repo clone -<TAB><TAB>31 --deep --help --rev --shallow -r32+ 33+Additionally, custom suggestions can be given to an argument with the ``autocomplete`` parameter as a list of strings. Example::34 35+.. click:example::36+37+ @cli.command()38+ @click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])39+ def cmd1(name):40+ click.echo('Name: %s' % name)41+42+43 Activation44 ----------45 46 In order to activate Bash completion, you need to inform Bash that33 unmodified linesadded:examples/bashcompletion/bashcompletion.py+21 added
1+import click2+3+@click.group()4+def cli():5+ pass6+7+@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)11+def cmd1():12+ pass13+14+@cli.command()15+def cmd2():16+ pass17+18+@cli.command()19+def cmd3():20+ pass21+added:examples/bashcompletion/README+12 added
1+$ bashcompletion2+3+ bashcompletion is a simple example of an application that4+ tries to autocomplete commands, arguments and options.5+6+ This example requires Click 2.0 or higher.7+8+Usage:9+10+ $ pip install --editable .11+ $ eval "$(_BASHCOMPLETION_COMPLETE=source bashcompletion)"12+ $ bashcompletion --helpadded:examples/bashcompletion/setup.py+15 added
1+from setuptools import setup2+3+setup(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+)