Moved bashcomplete into an internal module by name.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent ce616c9 · Browse files · 3 files·+75 added−75 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+75 added−75 removed
- other:+75 added−75 removed
Provenance
- Commit
0801796- Tree
bd0e374- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ce616c9
3 files changed+75 added−75 removed
added:click/_bashcomplete.py+74 added
1+import os2+from click.utils import echo3+from click.parser import split_arg_string4+from click.core import MultiCommand, Option5+6+7+COMPLETION_SCRIPT = '''8+%(complete_func)s() {9+ COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\10+ COMP_CWORD=$COMP_CWORD \\11+ %(autocomplete_var)s=complete $1 ) )12+ return 013+}14+15+complete -F %(complete_func)s -o default %(script_names)s16+'''17+18+19+def get_completion_script(prog_name, complete_var):20+ return (COMPLETION_SCRIPT % {21+ 'complete_func': '_%s_completion' % prog_name,22+ 'script_names': prog_name,23+ 'autocomplete_var': complete_var,24+ }).strip() + ';'25+26+27+def resolve_ctx(cli, prog_name, args):28+ ctx = cli.make_context(prog_name, args, resilient_parsing=True)29+ while ctx.args and isinstance(ctx.command, MultiCommand):30+ cmd = ctx.command.get_command(ctx, ctx.args[0])31+ if cmd is None:32+ return None33+ ctx = cmd.make_context(ctx.args[0], ctx.args[1:], parent=ctx,34+ resilient_parsing=True)35+ return ctx36+37+38+def do_complete(cli, prog_name):39+ cwords = split_arg_string(os.environ['COMP_WORDS'])40+ cword = int(os.environ['COMP_CWORD'])41+ args = cwords[1:cword]42+ try:43+ incomplete = cwords[cword]44+ except IndexError:45+ incomplete = ''46+47+ ctx = resolve_ctx(cli, prog_name, args)48+ if ctx is None:49+ return True50+51+ choices = []52+ if incomplete and not incomplete[:1].isalnum():53+ for param in ctx.command.params:54+ if not isinstance(param, Option):55+ continue56+ choices.extend(param.opts)57+ choices.extend(param.secondary_opts)58+ elif isinstance(ctx.command, MultiCommand):59+ choices.extend(ctx.command.list_commands(ctx))60+61+ for item in choices:62+ if item.startswith(incomplete):63+ echo(item)64+65+ return True66+67+68+def bashcomplete(cli, prog_name, complete_var, complete_instr):69+ if complete_instr == 'source':70+ echo(get_completion_script(prog_name, complete_var))71+ return True72+ elif complete_instr == 'complete':73+ return do_complete(cli, prog_name)74+ return Falsedeleted:click/bashcomplete.py-74 removed
1-import os2-from click.utils import echo3-from click.parser import split_arg_string4-from click.core import MultiCommand, Option5-6-7-COMPLETION_SCRIPT = '''8-%(complete_func)s() {9- COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\10- COMP_CWORD=$COMP_CWORD \\11- %(autocomplete_var)s=complete $1 ) )12- return 013-}14-15-complete -F %(complete_func)s -o default %(script_names)s16-'''17-18-19-def get_completion_script(prog_name, complete_var):20- return (COMPLETION_SCRIPT % {21- 'complete_func': '_%s_completion' % prog_name,22- 'script_names': prog_name,23- 'autocomplete_var': complete_var,24- }).strip() + ';'25-26-27-def resolve_ctx(cli, prog_name, args):28- ctx = cli.make_context(prog_name, args, resilient_parsing=True)29- while ctx.args and isinstance(ctx.command, MultiCommand):30- cmd = ctx.command.get_command(ctx, ctx.args[0])31- if cmd is None:32- return None33- ctx = cmd.make_context(ctx.args[0], ctx.args[1:], parent=ctx,34- resilient_parsing=True)35- return ctx36-37-38-def do_complete(cli, prog_name):39- cwords = split_arg_string(os.environ['COMP_WORDS'])40- cword = int(os.environ['COMP_CWORD'])41- args = cwords[1:cword]42- try:43- incomplete = cwords[cword]44- except IndexError:45- incomplete = ''46-47- ctx = resolve_ctx(cli, prog_name, args)48- if ctx is None:49- return True50-51- choices = []52- if incomplete and not incomplete[:1].isalnum():53- for param in ctx.command.params:54- if not isinstance(param, Option):55- continue56- choices.extend(param.opts)57- choices.extend(param.secondary_opts)58- elif isinstance(ctx.command, MultiCommand):59- choices.extend(ctx.command.list_commands(ctx))60-61- for item in choices:62- if item.startswith(incomplete):63- echo(item)64-65- return True66-67-68-def bashcomplete(cli, prog_name, complete_var, complete_instr):69- if complete_instr == 'source':70- echo(get_completion_script(prog_name, complete_var))71- return True72- elif complete_instr == 'complete':73- return do_complete(cli, prog_name)74- return Falsemodified:click/core.py+1 added-1 removed
22 unmodified lines23 complete_instr = os.environ.get(complete_var)24 if not complete_instr:25 return26 27- from click.bashcomplete import bashcomplete27+ from click._bashcomplete import bashcomplete28 if bashcomplete(cmd, prog_name, complete_var, complete_instr):29 sys.exit(1)30 31 1222 unmodified lines