Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keewenaw
GitHub Repository: keewenaw/ethereum-wallet-cracker
Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/commands/completion.py
4804 views
1
import sys
2
import textwrap
3
from optparse import Values
4
from typing import List
5
6
from pip._internal.cli.base_command import Command
7
from pip._internal.cli.status_codes import SUCCESS
8
from pip._internal.utils.misc import get_prog
9
10
BASE_COMPLETION = """
11
# pip {shell} completion start{script}# pip {shell} completion end
12
"""
13
14
COMPLETION_SCRIPTS = {
15
"bash": """
16
_pip_completion()
17
{{
18
COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\
19
COMP_CWORD=$COMP_CWORD \\
20
PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )
21
}}
22
complete -o default -F _pip_completion {prog}
23
""",
24
"zsh": """
25
function _pip_completion {{
26
local words cword
27
read -Ac words
28
read -cn cword
29
reply=( $( COMP_WORDS="$words[*]" \\
30
COMP_CWORD=$(( cword-1 )) \\
31
PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null ))
32
}}
33
compctl -K _pip_completion {prog}
34
""",
35
"fish": """
36
function __fish_complete_pip
37
set -lx COMP_WORDS (commandline -o) ""
38
set -lx COMP_CWORD ( \\
39
math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
40
)
41
set -lx PIP_AUTO_COMPLETE 1
42
string split \\ -- (eval $COMP_WORDS[1])
43
end
44
complete -fa "(__fish_complete_pip)" -c {prog}
45
""",
46
"powershell": """
47
if ((Test-Path Function:\\TabExpansion) -and -not `
48
(Test-Path Function:\\_pip_completeBackup)) {{
49
Rename-Item Function:\\TabExpansion _pip_completeBackup
50
}}
51
function TabExpansion($line, $lastWord) {{
52
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
53
if ($lastBlock.StartsWith("{prog} ")) {{
54
$Env:COMP_WORDS=$lastBlock
55
$Env:COMP_CWORD=$lastBlock.Split().Length - 1
56
$Env:PIP_AUTO_COMPLETE=1
57
(& {prog}).Split()
58
Remove-Item Env:COMP_WORDS
59
Remove-Item Env:COMP_CWORD
60
Remove-Item Env:PIP_AUTO_COMPLETE
61
}}
62
elseif (Test-Path Function:\\_pip_completeBackup) {{
63
# Fall back on existing tab expansion
64
_pip_completeBackup $line $lastWord
65
}}
66
}}
67
""",
68
}
69
70
71
class CompletionCommand(Command):
72
"""A helper command to be used for command completion."""
73
74
ignore_require_venv = True
75
76
def add_options(self) -> None:
77
self.cmd_opts.add_option(
78
"--bash",
79
"-b",
80
action="store_const",
81
const="bash",
82
dest="shell",
83
help="Emit completion code for bash",
84
)
85
self.cmd_opts.add_option(
86
"--zsh",
87
"-z",
88
action="store_const",
89
const="zsh",
90
dest="shell",
91
help="Emit completion code for zsh",
92
)
93
self.cmd_opts.add_option(
94
"--fish",
95
"-f",
96
action="store_const",
97
const="fish",
98
dest="shell",
99
help="Emit completion code for fish",
100
)
101
self.cmd_opts.add_option(
102
"--powershell",
103
"-p",
104
action="store_const",
105
const="powershell",
106
dest="shell",
107
help="Emit completion code for powershell",
108
)
109
110
self.parser.insert_option_group(0, self.cmd_opts)
111
112
def run(self, options: Values, args: List[str]) -> int:
113
"""Prints the completion code of the given shell"""
114
shells = COMPLETION_SCRIPTS.keys()
115
shell_options = ["--" + shell for shell in sorted(shells)]
116
if options.shell in shells:
117
script = textwrap.dedent(
118
COMPLETION_SCRIPTS.get(options.shell, "").format(prog=get_prog())
119
)
120
print(BASE_COMPLETION.format(script=script, shell=options.shell))
121
return SUCCESS
122
else:
123
sys.stderr.write(
124
"ERROR: You must pass {}\n".format(" or ".join(shell_options))
125
)
126
return SUCCESS
127
128