support descriptions for zsh completions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
use associative array for zsh completion response add documentation for completion help strings fix missing short_help on commands that broke ZSH auto documentation
authored by Nicholas Wiles · committed by David Lord · parent befb77b · Browse files · 6 files·+177 added−113 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+177 added−113 removed
- tests:+96 added−85 removed
- other:+81 added−28 removed
Provenance
- Commit
6c7276b- Tree
fb9c720- Author
Nicholas Wiles <nwiles@google.com>- Committer
David Lord <davidism@gmail.com>- Parent
befb77b
6 files changed+177 added−113 removed
modified:click/_bashcomplete.py+41 added-19 removed
22 unmodified lines23 '''24 25 COMPLETION_SCRIPT_ZSH = '''26 %(complete_func)s() {27- emulate -L zsh28- local IFS=$'\n'29- local completions=( $( env COMP_WORDS="${words[*]}" \\27+ local -a completions28+ local -a completions_with_descriptions29+ local -a response30+ response=("${(@f)$( env COMP_WORDS=\"${words[*]}\" \\31 COMP_CWORD=$((CURRENT-1)) \\31- %(autocomplete_var)s="complete" \\32- %(script_names)s ) )33- compadd -M 'r:|=* l:|=* r:|=*' -a -- completions 32+ %(autocomplete_var)s=\"complete_zsh\" \\33+ %(script_names)s )}")34+35+ for key descr in ${(kv)response}; do36+ if [[ "$descr" == "_" ]]; then37+ completions+=("$key")38+ else39+ completions_with_descriptions+=("$key":"$descr")40+ fi41+ done42+43+ if [ -n "$completions_with_descriptions" ]; then44+ _describe '' completions_with_descriptions45+ fi46+ if [ -n "$completions" ]; then47+ compadd -M 'r:|=* l:|=* r:|=*' -a completions48+ fi49 }50 51 compdef %(complete_func)s %(script_names)s52 '''92 unmodified lines145 :param incomplete: the incomplete text to autocomplete146 :param cmd_param: command definition147 :return: all the possible user-specified completions for the param148 """149+ results = []150 if isinstance(cmd_param.type, Choice):151+ # Choices don't support descriptions.152+ results = [(c, None)135- return [c for c in cmd_param.type.choices if c.startswith(incomplete)]153+ for c in cmd_param.type.choices if c.startswith(incomplete)]154 elif cmd_param.autocompletion is not None:137- return cmd_param.autocompletion(ctx=ctx,138- args=args,139- incomplete=incomplete)140- else:141- return []155+ dynamic_completions = cmd_param.autocompletion(ctx=ctx,156+ args=args,157+ incomplete=incomplete)158+ results = [c if isinstance(c, tuple) else (c, None)159+ for c in dynamic_completions]160+ return results161 162 163 def add_subcommand_completions(ctx, incomplete, completions_out):164 # Add subcommand completions.165 if isinstance(ctx.command, MultiCommand):166 completions_out.extend(148- [c for c in ctx.command.list_commands(ctx) if c.startswith(incomplete)])167+ [(c, ctx.command.get_command(ctx, c).get_short_help_str()) for c in ctx.command.list_commands(ctx) if c.startswith(incomplete)])168 169 # Walk up the context list and add any other completion possibilities from chained commands170 while ctx.parent is not None:171 ctx = ctx.parent172 if isinstance(ctx.command, MultiCommand) and ctx.command.chain:173 remaining_commands = sorted(174 set(ctx.command.list_commands(ctx)) - set(ctx.protected_args))175 completions_out.extend(157- [c for c in remaining_commands if c.startswith(incomplete)])176+ [(c, ctx.command.get_command(ctx, c).get_short_help_str()) for c in remaining_commands if c.startswith(incomplete)])177 178 179 def get_choices(cli, prog_name, args, incomplete):180 """25 unmodified lines206 if isinstance(param, Option):207 param_opts = [param_opt for param_opt in param.opts +208 param.secondary_opts if param_opt not in all_args or param.multiple]209 completions.extend(191- [c for c in param_opts if c.startswith(incomplete)])210+ [(o, param.help) for o in param_opts if o.startswith(incomplete)])211 return completions212 # completion for option values from user supplied values213 for param in ctx.command.params:214 if is_incomplete_option(all_args, param):11 unmodified lines226 add_subcommand_completions(ctx, incomplete, completions)227 return completions228 229 211-def do_complete(cli, prog_name):230+def do_complete(cli, prog_name, include_descriptions):231 cwords = split_arg_string(os.environ['COMP_WORDS'])232 cword = int(os.environ['COMP_CWORD'])233 args = cwords[1:cword]234 try:1 unmodified line236 except IndexError:237 incomplete = ''238 239 for item in get_choices(cli, prog_name, args, incomplete):221- echo(item)240+ echo(item[0])241+ if include_descriptions:242+ # ZSH has trouble dealing with empty array parameters when returned from commands, so use a well defined character '_' to indicate no description is present.243+ echo(item[1] if item[1] else '_')244 245 return True246 247 1 unmodified line249 if complete_instr.startswith('source'):250 shell = 'zsh' if complete_instr == 'source_zsh' else 'bash'251 echo(get_completion_script(prog_name, complete_var, shell))252 return True231- elif complete_instr == 'complete':232- return do_complete(cli, prog_name)253+ elif complete_instr == 'complete' or complete_instr == 'complete_zsh':254+ return do_complete(cli, prog_name, complete_instr == 'complete_zsh')255 return Falsemodified:click/core.py+6 added-2 removed
848 unmodified lines849 formatter = ctx.make_formatter()850 self.format_help(ctx, formatter)851 return formatter.getvalue().rstrip('\n')852 853+ def get_short_help_str(self, limit=45):854+ """Gets short help for the command or makes it by shortening the long help string."""855+ return self.short_help or self.help and make_default_short_help(self.help, limit) or ''856+857 def format_help(self, ctx, formatter):858 """Writes the help into the formatter if it exists.859 860 This calls into the following methods:183 unmodified lines1044 limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)1045 1046 rows = []1047 for subcommand, cmd in commands:1044- help = cmd.short_help or cmd.help and make_default_short_help(cmd.help, limit)1045- rows.append((subcommand, help or ''))1048+ help = cmd.get_short_help_str(limit)1049+ rows.append((subcommand, help))1050 1051 if rows:1052 with formatter.section('Commands'):1053 formatter.write_dl(rows)777 unmodified linesmodified:docs/bashcomplete.rst+30 added-4 removed
10 unmodified lines11 -----------12 13 Bash completion is only available if a script has been installed properly,14 and not executed through the ``python`` command. For information about15-how to do that, see :ref:`setuptools-integration`. Also, Click currently16-only supports completion for Bash. Zsh support is available through Zsh's17-bash completion compatibility mode.15+how to do that, see :ref:`setuptools-integration`. Click currently16+only supports completion for Bash and Zsh.17 18 What it Completes19 -----------------20 24 unmodified lines45 46 import os47 48 def get_env_vars(ctx, args, incomplete):50- return os.environ.keys()49+ return [k for k in os.environ.keys() if incomplete in k]50 51 @click.command()52 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)53 def cmd1(envvar):54 click.echo('Environment variable: %s' % envvar)55 click.echo('Value: %s' % os.environ[envvar])56 57 58+Completion help strings (ZSH only)59+----------------------------------60+61+ZSH supports showing documentation strings for completions. These are taken62+from the help parameters of options and subcommands. For dynamically generated63+completions a help string can be provided by returning a tuple instead of a64+string. The first element of the tuple is the completion and the second is the65+help string to display.66+67+Here is an example of using a callback function to generate dynamic suggestions with help strings:68+69+.. click:example::70+71+ import os72+73+ def get_colors(ctx, args, incomplete):74+ colors = [('red', 'help string for the color red'),75+ ('blue', 'help string for the color blue'),76+ ('green', 'help string for the color green')]77+ return [c for c in colors if incomplete in c[0]]78+79+ @click.command()80+ @click.argument("color", type=click.STRING, autocompletion=get_colors)81+ def cmd1(color):82+ click.echo('Chosen color is %s' % color)83+84+85 Activation86 ----------87 88 In order to activate Bash completion, you need to inform Bash that43 unmodified linesmodified:examples/bashcompletion/bashcompletion.py+3 added-3 removed
11 unmodified lines12 if incomplete in key:13 yield key14 15 16-@cli.command()16+@cli.command(help='A command to print environment variables')17 @click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)18 def cmd1(envvar):19 click.echo('Environment variable: %s' % envvar)20 click.echo('Value: %s' % os.environ[envvar])21 22 23-@click.group()23+@click.group(help='A group that holds a subcommand')24 def group():25 pass26 27 4 unmodified lines32 if user.startswith(incomplete):33 yield user34 35 36-@group.command()36+@group.command(help='Choose a user')37 @click.argument("user", type=click.STRING, autocompletion=list_users)38 def subcmd(user):39 click.echo('Chosen user is %s' % user)40 1 unmodified linemodified:tests/test_bashcomplete.py+96 added-85 removed
2 unmodified lines3 import click4 from click._bashcomplete import get_choices5 6 7+def choices_without_help(cli, args, incomplete):8+ completions = get_choices(cli, 'dummy', args, incomplete)9+ return [c[0] for c in completions]10+11+12+def choices_with_help(cli, args, incomplete):13+ return list(get_choices(cli, 'dummy', args, incomplete))14+15+16 def test_single_command():17 @click.command()18 @click.option('--local-opt')19 def cli(local_opt):20 pass21 13- assert list(get_choices(cli, 'lol', [], '-')) == ['--local-opt']14- assert list(get_choices(cli, 'lol', [], '')) == []22+ assert choices_without_help(cli, [], '-') == ['--local-opt']23+ assert choices_without_help(cli, [], '') == []24 25 26 def test_boolean_flag():27 @click.command()28 @click.option('--shout/--no-shout', default=False)29 def cli(local_opt):30 pass31 23- assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']32+ assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']33 34 35 def test_multi_value_option():36 @click.group()5 unmodified lines42 @click.option('--local-opt')43 def sub(local_opt):44 pass45 37- assert list(get_choices(cli, 'lol', [], '-')) == ['--pos']38- assert list(get_choices(cli, 'lol', ['--pos'], '')) == []39- assert list(get_choices(cli, 'lol', ['--pos', '1.0'], '')) == []40- assert list(get_choices(cli, 'lol', ['--pos', '1.0', '1.0'], '')) == ['sub']46+ assert choices_without_help(cli, [], '-') == ['--pos']47+ assert choices_without_help(cli, ['--pos'], '') == []48+ assert choices_without_help(cli, ['--pos', '1.0'], '') == []49+ assert choices_without_help(cli, ['--pos', '1.0', '1.0'], '') == ['sub']50 51 52 def test_multi_option():53 @click.command()54 @click.option('--message', '-m', multiple=True)55 def cli(local_opt):56 pass57 49- assert list(get_choices(cli, 'lol', [], '-')) == ['--message', '-m']50- assert list(get_choices(cli, 'lol', ['-m'], '')) == []58+ assert choices_without_help(cli, [], '-') == ['--message', '-m']59+ assert choices_without_help(cli, ['-m'], '') == []60 61 62 def test_small_chain():63 @click.group()5 unmodified lines69 @click.option('--local-opt')70 def sub(local_opt):71 pass72 64- assert list(get_choices(cli, 'lol', [], '')) == ['sub']65- assert list(get_choices(cli, 'lol', [], '-')) == ['--global-opt']66- assert list(get_choices(cli, 'lol', ['sub'], '')) == []67- assert list(get_choices(cli, 'lol', ['sub'], '-')) == ['--local-opt']73+ assert choices_without_help(cli, [], '') == ['sub']74+ assert choices_without_help(cli, [], '-') == ['--global-opt']75+ assert choices_without_help(cli, ['sub'], '') == []76+ assert choices_without_help(cli, ['sub'], '-') == ['--local-opt']77 78 79 def test_long_chain():80 @click.group('cli')31 unmodified lines112 @click.argument('color', autocompletion=get_colors)113 def csub(csub_opt, color):114 pass115 107- assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']108- assert list(get_choices(cli, 'lol', [], '')) == ['asub']109- assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']110- assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub']111- assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']112- assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub']113- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt', '--csub', '--search-color']114- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], '')) == CSUB_OPT_CHOICES115- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '--csub')) == ['--csub-opt', '--csub']116- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub'], '')) == CSUB_CHOICES117- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], 'f')) == ['foo']118- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == COLORS119- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], 'b')) == ['blue']120- assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--search-color'], 'een')) == ['green']116+ assert choices_without_help(cli, [], '-') == ['--cli-opt']117+ assert choices_without_help(cli, [], '') == ['asub']118+ assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']119+ assert choices_without_help(cli, ['asub'], '') == ['bsub']120+ assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']121+ assert choices_without_help(cli, ['asub', 'bsub'], '') == ['csub']122+ assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '-') == ['--csub-opt', '--csub', '--search-color']123+ assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], '') == CSUB_OPT_CHOICES124+ assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '--csub') == ['--csub-opt', '--csub']125+ assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub'], '') == CSUB_CHOICES126+ assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--csub-opt'], 'f') == ['foo']127+ assert choices_without_help(cli, ['asub', 'bsub', 'csub'], '') == COLORS128+ assert choices_without_help(cli, ['asub', 'bsub', 'csub'], 'b') == ['blue']129+ assert choices_without_help(cli, ['asub', 'bsub', 'csub', '--search-color'], 'een') == ['green']130 131 132 def test_chaining():133 @click.group('cli', chain=True)134 @click.option('--cli-opt')135 def cli(cli_opt):136 pass137 129- @cli.command('asub')138+ @cli.command()139 @click.option('--asub-opt')140 def asub(asub_opt):141 pass142 134- @cli.command('bsub')143+ @cli.command(help='bsub help')144 @click.option('--bsub-opt')145 @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True)146 def bsub(bsub_opt, arg):147 pass148 140- @cli.command('csub')149+ @cli.command()150 @click.option('--csub-opt')151 @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False)152 def csub(csub_opt, arg):153 pass154 146- assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt']147- assert list(get_choices(cli, 'lol', [], '')) == ['asub', 'bsub', 'csub']148- assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt']149- assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub', 'csub']150- assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['arg1', 'arg2']151- assert list(get_choices(cli, 'lol', ['asub', '--asub-opt'], '')) == []152- assert list(get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt']153- assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt']154- assert list(get_choices(cli, 'lol', ['asub', 'csub'], '')) == ['carg1', 'carg2', 'bsub']155- assert list(get_choices(cli, 'lol', ['asub', 'csub'], '-')) == ['--csub-opt']155+ assert choices_without_help(cli, [], '-') == ['--cli-opt']156+ assert choices_without_help(cli, [], '') == ['asub', 'bsub', 'csub']157+ assert choices_without_help(cli, ['asub'], '-') == ['--asub-opt']158+ assert choices_without_help(cli, ['asub'], '') == ['bsub', 'csub']159+ assert choices_without_help(cli, ['bsub'], '') == ['arg1', 'arg2']160+ assert choices_without_help(cli, ['asub', '--asub-opt'], '') == []161+ assert choices_without_help(cli, ['asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt']162+ assert choices_without_help(cli, ['asub', 'bsub'], '-') == ['--bsub-opt']163+ assert choices_with_help(cli, ['asub'], 'b') == [('bsub', 'bsub help')]164+ assert choices_without_help(cli, ['asub', 'csub'], '-') == ['--csub-opt']165 166 167 def test_argument_choice():168 @click.command()2 unmodified lines171 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))172 def cli():173 pass174 166- assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']167- assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12']168- assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22']169- assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument']170- assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']175+ assert choices_without_help(cli, [], '') == ['arg11', 'arg12']176+ assert choices_without_help(cli, [], 'arg') == ['arg11', 'arg12']177+ assert choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22']178+ assert choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument']179+ assert choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']180 181 182 def test_option_choice():183 @click.command()175- @click.option('--opt1', type=click.Choice(['opt11', 'opt12']))184+ @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help')185 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))186 @click.option('--opt3', type=click.Choice(['opt', 'option']))187 def cli():188 pass189 181- assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3']182- assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3']183- assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12']184- assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22']185- assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22']186- assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22']187- assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']188- assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']189- assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22']190- assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3']191- assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3']192- assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12']193- assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option']190+ assert choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'),191+ ('--opt2', None),192+ ('--opt3', None)]193+ assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3']194+ assert choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12']195+ assert choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22']196+ assert choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22']197+ assert choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22']198+ assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']199+ assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']200+ assert choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22']201+ assert choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3']202+ assert choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3']203+ assert choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12']204+ assert choices_without_help(cli, ['--opt3'], 'opti') == ['option']205 195- assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']206+ assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']207 208 209 def test_option_and_arg_choice():210 @click.command()2 unmodified lines213 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))214 def cli():215 pass216 206- assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12']207- assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12']208- assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12']209- assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']217+ assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12']218+ assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12']219+ assert choices_without_help(cli, [], '') == ['arg11', 'arg12']220+ assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22']221 222 223 def test_boolean_flag_choice():224 @click.command()1 unmodified line226 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))227 def cli(local_opt):228 pass229 219- assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']220- assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2']230+ assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']231+ assert choices_without_help(cli, ['--shout'], '') == ['arg1', 'arg2']232 233 234 def test_multi_value_option_choice():235 @click.command()1 unmodified line237 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))238 def cli(local_opt):239 pass240 230- assert list(get_choices(cli, 'lol', ['--pos'], '')) == ['pos1', 'pos2']231- assert list(get_choices(cli, 'lol', ['--pos', 'pos1'], '')) == ['pos1', 'pos2']232- assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2'], '')) == ['arg1', 'arg2']233- assert list(get_choices(cli, 'lol', ['--pos', 'pos1', 'pos2', 'arg1'], '')) == []241+ assert choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2']242+ assert choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2']243+ assert choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2']244+ assert choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []245 246 247 def test_multi_option_choice():248 @click.command()1 unmodified line250 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))251 def cli(local_opt):252 pass253 243- assert list(get_choices(cli, 'lol', ['-m'], '')) == ['m1', 'm2']244- assert list(get_choices(cli, 'lol', ['-m', 'm1', '-m'], '')) == ['m1', 'm2']245- assert list(get_choices(cli, 'lol', ['-m', 'm1'], '')) == ['arg1', 'arg2']254+ assert choices_without_help(cli, ['-m'], '') == ['m1', 'm2']255+ assert choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2']256+ assert choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']257 258 259 def test_variadic_argument_choice():260 @click.command()261 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))262 def cli(local_opt):263 pass264 254- assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']265+ assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']266 267 268 def test_long_chain_choice():269 @click.group()270 def cli():271 pass272 262- @cli.group('sub')273+ @cli.group()274 @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2']))275 @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2']))276 def sub(sub_opt):277 pass278 268- @sub.command('bsub')279+ @sub.command(short_help='bsub help')280 @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2']))281 @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2']))282 @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2']))283 def bsub(bsub_opt):284 pass285 275- assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2', 'bsub']276- assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2']277- assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \286+ assert choices_with_help(cli, ['sub'], '') == [('subarg1', None), ('subarg2', None), ('bsub', 'bsub help')]287+ assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2']288+ assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \289 ['subarg1', 'subarg2', 'bsub']279- assert list(get_choices(cli, 'lol',280- ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt']281- assert list(get_choices(cli, 'lol',282- ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \290+ assert choices_without_help(cli,291+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt']292+ assert choices_without_help(cli,293+ ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \294 ['bsubopt1', 'bsubopt2']284- assert list(get_choices(cli, 'lol',295+ assert choices_without_help(cli,296 ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1'],286- '')) == ['bbsubarg1', 'bbsubarg2']297+ '') == ['bbsubarg1', 'bbsubarg2']modified:CHANGES+1 added
32 unmodified lines33 See #790.34 - Allow autocompletion function to determine whether or not to return35 completions that start with the incomplete argument.36 - Add native ZSH autocompletion support See #323.37+- Add support for auto-completion documentation. See #866.38 - Subcommands that are named by the function now automatically have the39 underscore replaced with a dash. So if you register a function named40 `my_command` it becomes `my-command` in the command line interface.41 - Stdout is now automatically set to non blocking.365 unmodified lines