Mercurial > repos > bcclaywell > argo_navis
comparison venv/lib/python2.7/site-packages/click/_bashcomplete.py @ 0:d67268158946 draft
planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author | bcclaywell |
---|---|
date | Mon, 12 Oct 2015 17:43:33 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d67268158946 |
---|---|
1 import os | |
2 import re | |
3 from .utils import echo | |
4 from .parser import split_arg_string | |
5 from .core import MultiCommand, Option | |
6 | |
7 | |
8 COMPLETION_SCRIPT = ''' | |
9 %(complete_func)s() { | |
10 COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \\ | |
11 COMP_CWORD=$COMP_CWORD \\ | |
12 %(autocomplete_var)s=complete $1 ) ) | |
13 return 0 | |
14 } | |
15 | |
16 complete -F %(complete_func)s -o default %(script_names)s | |
17 ''' | |
18 | |
19 _invalid_ident_char_re = re.compile(r'[^a-zA-Z0-9_]') | |
20 | |
21 | |
22 def get_completion_script(prog_name, complete_var): | |
23 cf_name = _invalid_ident_char_re.sub('', prog_name.replace('-', '_')) | |
24 return (COMPLETION_SCRIPT % { | |
25 'complete_func': '_%s_completion' % cf_name, | |
26 'script_names': prog_name, | |
27 'autocomplete_var': complete_var, | |
28 }).strip() + ';' | |
29 | |
30 | |
31 def resolve_ctx(cli, prog_name, args): | |
32 ctx = cli.make_context(prog_name, args, resilient_parsing=True) | |
33 while ctx.args and isinstance(ctx.command, MultiCommand): | |
34 cmd = ctx.command.get_command(ctx, ctx.args[0]) | |
35 if cmd is None: | |
36 return None | |
37 ctx = cmd.make_context(ctx.args[0], ctx.args[1:], parent=ctx, | |
38 resilient_parsing=True) | |
39 return ctx | |
40 | |
41 | |
42 def do_complete(cli, prog_name): | |
43 cwords = split_arg_string(os.environ['COMP_WORDS']) | |
44 cword = int(os.environ['COMP_CWORD']) | |
45 args = cwords[1:cword] | |
46 try: | |
47 incomplete = cwords[cword] | |
48 except IndexError: | |
49 incomplete = '' | |
50 | |
51 ctx = resolve_ctx(cli, prog_name, args) | |
52 if ctx is None: | |
53 return True | |
54 | |
55 choices = [] | |
56 if incomplete and not incomplete[:1].isalnum(): | |
57 for param in ctx.command.params: | |
58 if not isinstance(param, Option): | |
59 continue | |
60 choices.extend(param.opts) | |
61 choices.extend(param.secondary_opts) | |
62 elif isinstance(ctx.command, MultiCommand): | |
63 choices.extend(ctx.command.list_commands(ctx)) | |
64 | |
65 for item in choices: | |
66 if item.startswith(incomplete): | |
67 echo(item) | |
68 | |
69 return True | |
70 | |
71 | |
72 def bashcomplete(cli, prog_name, complete_var, complete_instr): | |
73 if complete_instr == 'source': | |
74 echo(get_completion_script(prog_name, complete_var)) | |
75 return True | |
76 elif complete_instr == 'complete': | |
77 return do_complete(cli, prog_name) | |
78 return False |