Merge branch 'feature/parameter_autocompletion' of github.com:bb4242/click into feature/bash_completion_6.6
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by I · committed by I · parents d4ae22b, 35c6cc7 · Browse files · 6 files·+113 added−3 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+113 added−3 removed
- other:+113 added−3 removed
Provenance
- Commit
483e943- Tree
7494bfc- Author
I <nhwiles@gmail.com>- Committer
I <nhwiles@gmail.com>- Parent 1
d4ae22b- Parent 2
35c6cc7
6 files changed+113 added−3 removed
modified:click/_bashcomplete.py+17 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 \\40 unmodified lines51 choices.extend(param.opts)52 choices.extend(param.secondary_opts)53 elif isinstance(ctx.command, MultiCommand):54 choices.extend(ctx.command.list_commands(ctx))55+ else:56+ for param in ctx.command.params:57+ if isinstance(param, Argument):58+ value = ctx.params[param.name]59+ if (value == None) or (isinstance(value, tuple) and (len(value) < param.nargs or param.nargs == -1)):60+ try:61+ if callable(param.autocompletion):62+ choices.extend(param.autocompletion(ctx=ctx,63+ incomplete=incomplete,64+ cwords=cwords,65+ cword=cword))66+ else:67+ choices.extend(param.autocompletion)68+ except AttributeError:69+ pass70+ break71 72 for item in choices:73 if item.startswith(incomplete):74 yield item24 unmodified linesmodified:click/core.py+2 added
1688 unmodified lines1689 if attrs.get('default') is not None:1690 required = False1691 else:1692 required = attrs.get('nargs', 1) > 01693+ if attrs.get('autocompletion') is not None:1694+ self.autocompletion = attrs.pop('autocompletion')1695 Parameter.__init__(self, param_decls, required=required, **attrs)1696 if self.default is not None and self.nargs < 0:1697 raise TypeError('nargs=-1 in combination with a default value '1698 'is not supported.')42 unmodified linesmodified:docs/bashcomplete.rst+39 added-1 removed
19 unmodified lines20 21 What it Completes22 -----------------23 24-Generally, the Bash completion support will complete subcommands and24+By default, the Bash completion support will complete subcommands and25 parameters. Subcommands are always listed whereas parameters only if at26 least a dash has been provided. Example::27 28 $ 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 provided for arguments with the34+``autocompletion`` parameter. ``autocompletion`` may be a list of strings, as35+in the following example:36 37+.. click:example::38+39+ @click.command()40+ @click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])41+ def cmd1(name):42+ click.echo('Name: %s' % name)43+ 44+Alternatively, ``autocompletion`` may be a callback function that returns a list45+of strings. This is useful when the suggestions need to be dynamically generated46+at bash completion time. The callback function will be passed 4 keyword47+arguments:48+49+- ``ctx`` - The current click context.50+- ``incomplete`` - The partial word that is being completed, as a string. May51+ be an empty string ``''`` if no characters have been entered yet.52+- ``cwords`` - The bash `COMP_WORDS <https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion>`_ array, as a list of strings.53+- ``cword`` - The bash `COMP_CWORD <https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion.html#Programmable-Completion>`_ variable, as an integer.54+55+Here is an example of using a callback function to generate dynamic suggestions:56+57+.. click:example::58+59+ import os60+ 61+ def get_env_vars(ctx, incomplete, cwords, cword):62+ return os.environ.keys()63+64+ @click.command()65+ @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)66+ def cmd1(envvar):67+ click.echo('Environment variable: %s' % envvar)68+ click.echo('Value: %s' % os.environ[envvar])69+ 70+71 Activation72 ----------73 74 In order to activate Bash completion, you need to inform Bash that33 unmodified linesadded:examples/bashcompletion/bashcompletion.py+28 added
1+import click2+import os3+4+@click.group()5+def cli():6+ pass7+8+@cli.command()9+@click.option('-c', '--count', type=click.INT, default=1)10+@click.argument("first", type=click.STRING, autocompletion=["John", "Bob", "Fred"])11+@click.argument("last", type=click.STRING, autocompletion=["Smith", "Simon", "Doe"])12+def cmd1(count, first, last):13+ for c in range(count):14+ click.echo('Name: %s %s' % (first, last))15+16+def get_env_vars(ctx, incomplete, cwords, cword):17+ return os.environ.keys()18+19+@cli.command()20+@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)21+def cmd2(envvar):22+ click.echo('Environment variable: %s' % envvar)23+ click.echo('Value: %s' % os.environ[envvar])24+25+@cli.command()26+def cmd3():27+ pass28+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+)