Skip to content
Myr

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

6 files+113 added−3 removed

modified:click/_bashcomplete.py+17 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 \\
40 unmodified lines
51 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 break7172 for item in choices:73 if item.startswith(incomplete):74 yield item
24 unmodified lines
modified:click/core.py+2 added
1688 unmodified lines
1689 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 lines
modified:docs/bashcomplete.rst+39 added-1 removed
19 unmodified lines
2021What it Completes22-----------------2324Generally, the Bash completion support will complete subcommands and24By default, the Bash completion support will complete subcommands and25parameters. Subcommands are always listed whereas parameters only if at26least a dash has been provided. Example::2728 $ repo <TAB><TAB>29 clone commit copy delete setuser30 $ repo clone -<TAB><TAB>31 --deep --help --rev --shallow -r32 33Additionally, custom suggestions can be provided for arguments with the34``autocompletion`` parameter. ``autocompletion`` may be a list of strings, as35in the following example:3637.. click:example::3839 @click.command()40 @click.argument("name", type=click.STRING, autocompletion=["John", "Simon", "Doe"])41 def cmd1(name):42 click.echo('Name: %s' % name)43 44Alternatively, ``autocompletion`` may be a callback function that returns a list45of strings. This is useful when the suggestions need to be dynamically generated46at bash completion time. The callback function will be passed 4 keyword47arguments:4849- ``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.5455Here is an example of using a callback function to generate dynamic suggestions:5657.. click:example::5859 import os60 61 def get_env_vars(ctx, incomplete, cwords, cword):62 return os.environ.keys()6364 @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 7071Activation72----------7374In order to activate Bash completion, you need to inform Bash that
33 unmodified lines
added:examples/bashcompletion/bashcompletion.py+28 added
1import click2import os34@click.group()5def cli():6 pass78@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"])12def cmd1(count, first, last):13 for c in range(count):14 click.echo('Name: %s %s' % (first, last))1516def get_env_vars(ctx, incomplete, cwords, cword):17 return os.environ.keys()1819@cli.command()20@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)21def cmd2(envvar):22 click.echo('Environment variable: %s' % envvar)23 click.echo('Value: %s' % os.environ[envvar])2425@cli.command()26def cmd3():27 pass28
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)