refactor completion command splitting
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 792f03f · Browse files · 1 file·+27 added−21 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+27 added−21 removed
- other:+27 added−21 removed
Provenance
- Commit
347b45b- Tree
0f38844- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
792f03f
1 file changed+27 added−21 removed
modified:click/_bashcomplete.py+27 added-21 removed
75 unmodified lines76 COMPLETION_SCRIPT_FISH = '''77 complete --no-files --command %(script_names)s --arguments "(env %(autocomplete_var)s=complete_fish COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) %(script_names)s)"78 '''79 80+_completion_scripts = {81+ "bash": COMPLETION_SCRIPT_BASH,82+ "zsh": COMPLETION_SCRIPT_ZSH,83+ "fish": COMPLETION_SCRIPT_FISH,84+}85+86 _invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]')87 88 89 def get_completion_script(prog_name, complete_var, shell):90 cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_'))85- if shell == 'zsh':86- script = COMPLETION_SCRIPT_ZSH87- elif shell == 'fish':88- script = COMPLETION_SCRIPT_FISH89- else:90- script = COMPLETION_SCRIPT_BASH91+ script = _completion_scripts.get(shell, COMPLETION_SCRIPT_BASH)92 return (script % {93 'complete_func': '_%s_completion' % cf_name,94 'script_names': prog_name,95 'autocomplete_var': complete_var,196 unmodified lines292 293 for item in get_choices(cli, prog_name, args, incomplete):294 echo(item[0])295 if include_descriptions:295- # ZSH has trouble dealing with empty array parameters when returned from commands, so use a well defined character '_' to indicate no description is present.296+ # ZSH has trouble dealing with empty array parameters when297+ # returned from commands, use '_' to indicate no description298+ # is present.299 echo(item[1] if item[1] else '_')300 301 return True302 303 304 def do_complete_fish(cli, prog_name):302- cwords = split_arg_string(os.environ['COMP_WORDS'])303- incomplete = os.environ['COMP_CWORD']305+ cwords = split_arg_string(os.environ["COMP_WORDS"])306+ incomplete = os.environ["COMP_CWORD"]307 args = cwords[1:]308+309 for item in get_choices(cli, prog_name, args, incomplete):310 if item[1]:307- echo("%(arg)s\t%(desc)s" % {'arg': item[0], 'desc': item[1]})311+ echo("%(arg)s\t%(desc)s" % {"arg": item[0], "desc": item[1]})312 else:313 echo(item[0])314 315 return True316 317 318 def bashcomplete(cli, prog_name, complete_var, complete_instr):315- if complete_instr.startswith('source'):316- if complete_instr == 'source_zsh':317- shell = 'zsh'318- elif complete_instr == 'source_fish':319- shell = 'fish'320- else:321- shell = 'bash'319+ if "_" in complete_instr:320+ command, shell = complete_instr.split("_", 1)321+ else:322+ command = complete_instr323+ shell = "bash"324+325+ if command == "source":326 echo(get_completion_script(prog_name, complete_var, shell))327 return True324- elif complete_instr == 'complete_fish':325- return do_complete_fish(cli, prog_name)326- elif complete_instr == 'complete' or complete_instr == 'complete_zsh':327- return do_complete(cli, prog_name, complete_instr == 'complete_zsh')328+ elif command == "complete":329+ if shell == "fish":330+ return do_complete_fish(cli, prog_name)331+ elif shell in {"bash", "zsh"}:332+ return do_complete(cli, prog_name, shell == "zsh")333+334 return False