Cleanup get_choices() to leverage early returns.
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 54fec68 · Browse files · 1 file·+37 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+37 added−41 removed
- other:+37 added−41 removed
Provenance
- Commit
a02cbb7- Tree
9e06072- Author
Nicholas Wiles <nwiles@google.com>- Committer
Nicholas Wiles <nwiles@google.com>- Parent
54fec68
1 file changed+37 added−41 removed
modified:click/_bashcomplete.py+37 added-41 removed
124 unmodified lines125 else:126 return []127 128 129+def add_subcommand_completions(ctx, incomplete, completions_out):130+ # Add subcommand completions.131+ if isinstance(ctx.command, MultiCommand):132+ completions_out.extend(133+ [c for c in ctx.command.list_commands(ctx) if c.startswith(incomplete)])134+135+ # Walk up the context list and add any other completion possibilities from chained commands136+ while ctx.parent is not None:137+ ctx = ctx.parent138+ if isinstance(ctx.command, MultiCommand) and ctx.command.chain:139+ remaining_commands = sorted(140+ set(ctx.command.list_commands(ctx)) - set(ctx.protected_args))141+ completions_out.extend(142+ [c for c in remaining_commands if c.startswith(incomplete)])143+144+145 def get_choices(cli, prog_name, args, incomplete):146 """147 :param cli: command definition148 :param prog_name: the program that is running15 unmodified lines164 incomplete = partition_incomplete[2]165 elif incomplete == WORDBREAK:166 incomplete = ''167 152- choices = []153- user_choices = []154- found_param = False168+ completions = []169 if start_of_option(incomplete):170 # completions for partial options171 for param in ctx.command.params:172 if isinstance(param, Option):159- choices.extend([param_opt for param_opt in param.opts + param.secondary_opts160- if param_opt not in all_args or param.multiple])161- else:162- # completion for option values from user supplied values163- for param in ctx.command.params:164- if is_incomplete_option(all_args, param):165- user_choices.extend(get_user_autocompletions(166- ctx, all_args, incomplete, param))167- found_param = True168- break169- # completion for argument values from user supplied values170- if not found_param:171- for param in ctx.command.params:172- if is_incomplete_argument(ctx.params, param):173- user_choices.extend(get_user_autocompletions(174- ctx, all_args, incomplete, param))175- # Stop looking for other completions only if this argument is required176- found_param = param.required177- break173+ param_opts = [param_opt for param_opt in param.opts +174+ param.secondary_opts if param_opt not in all_args or param.multiple]175+ completions.extend(176+ [c for c in param_opts if c.startswith(incomplete)])177+ return completions178+ # completion for option values from user supplied values179+ for param in ctx.command.params:180+ if is_incomplete_option(all_args, param):181+ return get_user_autocompletions(ctx, all_args, incomplete, param)182+ # completion for argument values from user supplied values183+ for param in ctx.command.params:184+ if is_incomplete_argument(ctx.params, param):185+ completions.extend(get_user_autocompletions(186+ ctx, all_args, incomplete, param))187+ # Stop looking for other completions only if this argument is required.188+ if param.required:189+ return completions190+ break191 179- if not found_param and isinstance(ctx.command, MultiCommand):180- # completion for any subcommands181- choices.extend(ctx.command.list_commands(ctx))192+ add_subcommand_completions(ctx, incomplete, completions)193+ return completions194 183- if not found_param:184- # Walk up the context list and add any other completion possibilities from chained commands185- while ctx.parent is not None:186- ctx = ctx.parent187- if isinstance(ctx.command, MultiCommand) and ctx.command.chain:188- remaining_commands = sorted(189- set(ctx.command.list_commands(ctx)) - set(ctx.protected_args))190- choices.extend(remaining_commands)195 192- for item in user_choices:193- yield item194-195- for item in choices:196- if item.startswith(incomplete):197- yield item198-199-196 def do_complete(cli, prog_name):197 cwords = split_arg_string(os.environ['COMP_WORDS'])198 cword = int(os.environ['COMP_CWORD'])199 args = cwords[1:cword]18 unmodified lines