Skip to content
Myr

Merge pull request #869 from stopthatcow/feature/866

Before adoption: this commit came in with the history the project adopted, so no landing records it.

ZSH completion auto-documentation

authored by David Lord · committed by GitHub · parents befb77b, 6c7276b · 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

6 files+177 added−113 removed

modified:click/_bashcomplete.py+41 added-19 removed
22 unmodified lines
23'''2425COMPLETION_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 )}")3435 for key descr in ${(kv)response}; do36 if [[ "$descr" == "_" ]]; then37 completions+=("$key")38 else39 completions_with_descriptions+=("$key":"$descr")40 fi41 done4243 if [ -n "$completions_with_descriptions" ]; then44 _describe '' completions_with_descriptions45 fi46 if [ -n "$completions" ]; then47 compadd -M 'r:|=* l:|=* r:|=*' -a completions48 fi49}5051compdef %(complete_func)s %(script_names)s52'''
92 unmodified lines
145 :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 results161162163def 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)])168169 # 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)])177178179def get_choices(cli, prog_name, args, incomplete):180 """
25 unmodified lines
206 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 lines
226 add_subcommand_completions(ctx, incomplete, completions)227 return completions228229211def do_complete(cli, prog_name):230def 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 line
236 except IndexError:237 incomplete = ''238239 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 '_')244245 return True246247
1 unmodified line
249 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 False
modified:click/core.py+6 added-2 removed
848 unmodified lines
849 formatter = ctx.make_formatter()850 self.format_help(ctx, formatter)851 return formatter.getvalue().rstrip('\n')852853 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 ''856857 def format_help(self, ctx, formatter):858 """Writes the help into the formatter if it exists.859860 This calls into the following methods:
183 unmodified lines
1044 limit = formatter.width - 6 - max(len(cmd[0]) for cmd in commands)10451046 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))10501051 if rows:1052 with formatter.section('Commands'):1053 formatter.write_dl(rows)
777 unmodified lines
modified:docs/bashcomplete.rst+30 added-4 removed
10 unmodified lines
11-----------1213Bash completion is only available if a script has been installed properly,14and not executed through the ``python`` command. For information about15how to do that, see :ref:`setuptools-integration`. Also, Click currently16only supports completion for Bash. Zsh support is available through Zsh's17bash completion compatibility mode.15how to do that, see :ref:`setuptools-integration`. Click currently16only supports completion for Bash and Zsh.1718What it Completes19-----------------20
24 unmodified lines
4546 import os4748 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]5051 @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])565758Completion help strings (ZSH only)59----------------------------------6061ZSH supports showing documentation strings for completions. These are taken62from the help parameters of options and subcommands. For dynamically generated63completions a help string can be provided by returning a tuple instead of a64string. The first element of the tuple is the completion and the second is the65help string to display.6667Here is an example of using a callback function to generate dynamic suggestions with help strings:6869.. click:example::7071 import os7273 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]]7879 @click.command()80 @click.argument("color", type=click.STRING, autocompletion=get_colors)81 def cmd1(color):82 click.echo('Chosen color is %s' % color)838485Activation86----------8788In order to activate Bash completion, you need to inform Bash that
43 unmodified lines
modified:examples/bashcompletion/bashcompletion.py+3 added-3 removed
11 unmodified lines
12 if incomplete in key:13 yield key141516@cli.command()16@cli.command(help='A command to print environment variables')17@click.argument("envvar", type=click.STRING, autocompletion=get_env_vars)18def cmd1(envvar):19 click.echo('Environment variable: %s' % envvar)20 click.echo('Value: %s' % os.environ[envvar])212223@click.group()23@click.group(help='A group that holds a subcommand')24def group():25 pass2627
4 unmodified lines
32 if user.startswith(incomplete):33 yield user343536@group.command()36@group.command(help='Choose a user')37@click.argument("user", type=click.STRING, autocompletion=list_users)38def subcmd(user):39 click.echo('Chosen user is %s' % user)40
1 unmodified line
modified:tests/test_bashcomplete.py+96 added-85 removed
2 unmodified lines
3import click4from click._bashcomplete import get_choices567def choices_without_help(cli, args, incomplete):8 completions = get_choices(cli, 'dummy', args, incomplete)9 return [c[0] for c in completions]101112def choices_with_help(cli, args, incomplete):13 return list(get_choices(cli, 'dummy', args, incomplete))141516def test_single_command():17 @click.command()18 @click.option('--local-opt')19 def cli(local_opt):20 pass2113 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, [], '') == []242526def test_boolean_flag():27 @click.command()28 @click.option('--shout/--no-shout', default=False)29 def cli(local_opt):30 pass3123 assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout']32 assert choices_without_help(cli, [], '-') == ['--shout', '--no-shout']333435def test_multi_value_option():36 @click.group()
5 unmodified lines
42 @click.option('--local-opt')43 def sub(local_opt):44 pass4537 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']505152def test_multi_option():53 @click.command()54 @click.option('--message', '-m', multiple=True)55 def cli(local_opt):56 pass5749 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'], '') == []606162def test_small_chain():63 @click.group()
5 unmodified lines
69 @click.option('--local-opt')70 def sub(local_opt):71 pass7264 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']777879def test_long_chain():80 @click.group('cli')
31 unmodified lines
112 @click.argument('color', autocompletion=get_colors)113 def csub(csub_opt, color):114 pass115107 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']130131132def test_chaining():133 @click.group('cli', chain=True)134 @click.option('--cli-opt')135 def cli(cli_opt):136 pass137129 @cli.command('asub')138 @cli.command()139 @click.option('--asub-opt')140 def asub(asub_opt):141 pass142134 @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 pass148140 @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 pass154146 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']165166167def test_argument_choice():168 @click.command()
2 unmodified lines
171 @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument']))172 def cli():173 pass174166 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']180181182def 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 pass189181 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']205195 assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']206 assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']207208209def test_option_and_arg_choice():210 @click.command()
2 unmodified lines
213 @click.option('--opt2', type=click.Choice(['opt21', 'opt22']))214 def cli():215 pass216206 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']221222223def test_boolean_flag_choice():224 @click.command()
1 unmodified line
226 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))227 def cli(local_opt):228 pass229219 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']232233234def test_multi_value_option_choice():235 @click.command()
1 unmodified line
237 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))238 def cli(local_opt):239 pass240230 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'], '') == []245246247def test_multi_option_choice():248 @click.command()
1 unmodified line
250 @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2']))251 def cli(local_opt):252 pass253243 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']257258259def test_variadic_argument_choice():260 @click.command()261 @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2']))262 def cli(local_opt):263 pass264254 assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']265 assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']266267268def test_long_chain_choice():269 @click.group()270 def cli():271 pass272262 @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 pass278268 @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 pass285275 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 lines
33 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