Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/pip/_internal/commands/completion.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author | shellac |
---|---|
date | Mon, 22 Mar 2021 18:12:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4f3585e2f14b |
---|---|
1 import sys | |
2 import textwrap | |
3 | |
4 from pip._internal.cli.base_command import Command | |
5 from pip._internal.cli.status_codes import SUCCESS | |
6 from pip._internal.utils.misc import get_prog | |
7 from pip._internal.utils.typing import MYPY_CHECK_RUNNING | |
8 | |
9 if MYPY_CHECK_RUNNING: | |
10 from optparse import Values | |
11 from typing import List | |
12 | |
13 BASE_COMPLETION = """ | |
14 # pip {shell} completion start{script}# pip {shell} completion end | |
15 """ | |
16 | |
17 COMPLETION_SCRIPTS = { | |
18 'bash': """ | |
19 _pip_completion() | |
20 {{ | |
21 COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\ | |
22 COMP_CWORD=$COMP_CWORD \\ | |
23 PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) ) | |
24 }} | |
25 complete -o default -F _pip_completion {prog} | |
26 """, | |
27 'zsh': """ | |
28 function _pip_completion {{ | |
29 local words cword | |
30 read -Ac words | |
31 read -cn cword | |
32 reply=( $( COMP_WORDS="$words[*]" \\ | |
33 COMP_CWORD=$(( cword-1 )) \\ | |
34 PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )) | |
35 }} | |
36 compctl -K _pip_completion {prog} | |
37 """, | |
38 'fish': """ | |
39 function __fish_complete_pip | |
40 set -lx COMP_WORDS (commandline -o) "" | |
41 set -lx COMP_CWORD ( \\ | |
42 math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\ | |
43 ) | |
44 set -lx PIP_AUTO_COMPLETE 1 | |
45 string split \\ -- (eval $COMP_WORDS[1]) | |
46 end | |
47 complete -fa "(__fish_complete_pip)" -c {prog} | |
48 """, | |
49 } | |
50 | |
51 | |
52 class CompletionCommand(Command): | |
53 """A helper command to be used for command completion.""" | |
54 | |
55 ignore_require_venv = True | |
56 | |
57 def add_options(self): | |
58 # type: () -> None | |
59 self.cmd_opts.add_option( | |
60 '--bash', '-b', | |
61 action='store_const', | |
62 const='bash', | |
63 dest='shell', | |
64 help='Emit completion code for bash') | |
65 self.cmd_opts.add_option( | |
66 '--zsh', '-z', | |
67 action='store_const', | |
68 const='zsh', | |
69 dest='shell', | |
70 help='Emit completion code for zsh') | |
71 self.cmd_opts.add_option( | |
72 '--fish', '-f', | |
73 action='store_const', | |
74 const='fish', | |
75 dest='shell', | |
76 help='Emit completion code for fish') | |
77 | |
78 self.parser.insert_option_group(0, self.cmd_opts) | |
79 | |
80 def run(self, options, args): | |
81 # type: (Values, List[str]) -> int | |
82 """Prints the completion code of the given shell""" | |
83 shells = COMPLETION_SCRIPTS.keys() | |
84 shell_options = ['--' + shell for shell in sorted(shells)] | |
85 if options.shell in shells: | |
86 script = textwrap.dedent( | |
87 COMPLETION_SCRIPTS.get(options.shell, '').format( | |
88 prog=get_prog()) | |
89 ) | |
90 print(BASE_COMPLETION.format(script=script, shell=options.shell)) | |
91 return SUCCESS | |
92 else: | |
93 sys.stderr.write( | |
94 'ERROR: You must pass {}\n' .format(' or '.join(shell_options)) | |
95 ) | |
96 return SUCCESS |