Add choices to autocompletion
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicholas Wiles · committed by Nicholas Wiles · parent 4fd8955 · Browse files · 3 files·+20 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+20 added−7 removed
- tests:+9 added−2 removed
- other:+11 added−5 removed
Provenance
- Commit
8ac004f- Tree
ab20b69- Author
Nicholas Wiles <nwiles@google.com>- Committer
Nicholas Wiles <nwiles@google.com>- Parent
4fd8955
3 files changed+20 added−7 removed
modified:click/_bashcomplete.py+7 added-2 removed
1 unmodified line2 import re3 from .utils import echo4 from .parser import split_arg_string5 from .core import MultiCommand, Option, Argument6+from .types import Choice7 8 COMPLETION_SCRIPT = '''9 %(complete_func)s() {10 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\43 unmodified lines54 elif isinstance(ctx.command, MultiCommand):55 choices.extend(ctx.command.list_commands(ctx))56 else:57 for param in ctx.command.params:57- if isinstance(param, Argument):58+ if isinstance(param, Option) and isinstance(param.type, Choice):59+ if args[-1] in param.opts:60+ choices.extend(param.type.choices)61+ break62+ elif hasattr(param, 'autocompletion'):63 value = ctx.params[param.name]59- if (value == None) or (isinstance(value, tuple) and (len(value) < param.nargs or param.nargs == -1)):64+ if (value is None) or (isinstance(value, tuple) and (len(value) < param.nargs or param.nargs == -1)):65 try:66 if callable(param.autocompletion):67 choices.extend(param.autocompletion(ctx=ctx,68 args=args,34 unmodified linesmodified:click/core.py+4 added-3 removed
1264 unmodified lines1265 param_type_name = 'parameter'1266 1267 def __init__(self, param_decls=None, type=None, required=False,1268 default=None, callback=None, nargs=None, metavar=None,1269- expose_value=True, is_eager=False, envvar=None):1269+ expose_value=True, is_eager=False, envvar=None,1270+ autocompletion=None):1271 self.name, self.opts, self.secondary_opts = \1272 self._parse_decls(param_decls or (), expose_value)1273 1274 self.type = convert_type(type, default)14 unmodified lines1289 self.default = default1290 self.is_eager = is_eager1291 self.metavar = metavar1292 self.envvar = envvar1293+ if autocompletion is not None:1294+ self.autocompletion = autocompletion1295 1296 @property1297 def human_readable_name(self):1298 """Returns the human readable name of this parameter. This is the409 unmodified lines1708 if attrs.get('default') is not None:1709 required = False1710 else:1711 required = attrs.get('nargs', 1) > 01709- if attrs.get('autocompletion') is not None:1710- self.autocompletion = attrs.pop('autocompletion')1712 Parameter.__init__(self, param_decls, required=required, **attrs)1713 if self.default is not None and self.nargs < 0:1714 raise TypeError('nargs=-1 in combination with a default value '1715 'is not supported.')42 unmodified linesmodified:tests/test_bashcomplete.py+9 added-2 removed
49 unmodified lines50 COLORS = ['red', 'green', 'blue']51 def get_colors(ctx, args, incomplete):52 return COLORS53 54+ CSUB_OPT_CHOICES = ['foo', 'bar']55+ CSUB_CHOICES = ['bar', 'baz']56 @bsub.command('csub')55- @click.option('--csub-opt')57+ @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES))58+ @click.option('--csub', type=click.Choice(CSUB_CHOICES))59 @click.argument('color', autocompletion=get_colors)60 def csub(csub_opt, color):61 pass62 2 unmodified lines65 assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']66 assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']67 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']68 assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub']66- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt']69+ assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt', '--csub']70+ assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], '')) == CSUB_OPT_CHOICES71+ assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '--csub')) == ['--csub-opt', '--csub']72+ assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub'], '')) == CSUB_CHOICES73+ assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], 'f')) == ['foo']74 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == COLORS75 assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], 'b')) == ['blue']