2010-04-16 00:19:31 +02:00
|
|
|
import sys
|
2017-05-23 07:26:22 +02:00
|
|
|
import textwrap
|
2021-02-19 13:56:59 +01:00
|
|
|
from optparse import Values
|
|
|
|
from typing import List
|
2017-05-16 12:16:30 +02:00
|
|
|
|
2018-07-30 06:02:47 +02:00
|
|
|
from pip._internal.cli.base_command import Command
|
2020-05-15 13:27:31 +02:00
|
|
|
from pip._internal.cli.status_codes import SUCCESS
|
2017-10-12 07:29:53 +02:00
|
|
|
from pip._internal.utils.misc import get_prog
|
2020-04-14 21:41:57 +02:00
|
|
|
|
2009-11-20 19:20:52 +01:00
|
|
|
BASE_COMPLETION = """
|
2020-02-29 20:53:59 +01:00
|
|
|
# pip {shell} completion start{script}# pip {shell} completion end
|
2009-11-20 19:20:52 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
COMPLETION_SCRIPTS = {
|
|
|
|
"bash": """
|
2017-05-23 07:26:22 +02:00
|
|
|
_pip_completion()
|
2020-02-29 20:53:59 +01:00
|
|
|
{{
|
|
|
|
COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\
|
2017-05-23 07:26:22 +02:00
|
|
|
COMP_CWORD=$COMP_CWORD \\
|
2018-10-26 18:11:11 +02:00
|
|
|
PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )
|
2020-02-29 20:53:59 +01:00
|
|
|
}}
|
|
|
|
complete -o default -F _pip_completion {prog}
|
2017-05-23 07:26:22 +02:00
|
|
|
""",
|
|
|
|
"zsh": """
|
2022-08-28 11:29:25 +02:00
|
|
|
#compdef -P pip[0-9.]#
|
2023-07-22 09:31:12 +02:00
|
|
|
__pip() {{
|
|
|
|
compadd $( COMP_WORDS="$words[*]" \\
|
|
|
|
COMP_CWORD=$((CURRENT-1)) \\
|
|
|
|
PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
|
|
|
|
}}
|
|
|
|
if [[ $zsh_eval_context[-1] == loadautofunc ]]; then
|
|
|
|
# autoload from fpath, call function directly
|
|
|
|
__pip "$@"
|
|
|
|
else
|
|
|
|
# eval/source/. command, register function for later
|
|
|
|
compdef __pip -P 'pip[0-9.]#'
|
|
|
|
fi
|
2017-05-23 07:26:22 +02:00
|
|
|
""",
|
|
|
|
"fish": """
|
|
|
|
function __fish_complete_pip
|
|
|
|
set -lx COMP_WORDS (commandline -o) ""
|
2017-10-12 07:29:53 +02:00
|
|
|
set -lx COMP_CWORD ( \\
|
|
|
|
math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
|
|
|
|
)
|
2017-05-23 07:26:22 +02:00
|
|
|
set -lx PIP_AUTO_COMPLETE 1
|
|
|
|
string split \\ -- (eval $COMP_WORDS[1])
|
|
|
|
end
|
2020-02-29 20:53:59 +01:00
|
|
|
complete -fa "(__fish_complete_pip)" -c {prog}
|
2017-10-12 07:29:53 +02:00
|
|
|
""",
|
2022-04-15 10:32:42 +02:00
|
|
|
"powershell": """
|
|
|
|
if ((Test-Path Function:\\TabExpansion) -and -not `
|
|
|
|
(Test-Path Function:\\_pip_completeBackup)) {{
|
|
|
|
Rename-Item Function:\\TabExpansion _pip_completeBackup
|
|
|
|
}}
|
|
|
|
function TabExpansion($line, $lastWord) {{
|
|
|
|
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
|
|
|
|
if ($lastBlock.StartsWith("{prog} ")) {{
|
|
|
|
$Env:COMP_WORDS=$lastBlock
|
|
|
|
$Env:COMP_CWORD=$lastBlock.Split().Length - 1
|
|
|
|
$Env:PIP_AUTO_COMPLETE=1
|
|
|
|
(& {prog}).Split()
|
|
|
|
Remove-Item Env:COMP_WORDS
|
|
|
|
Remove-Item Env:COMP_CWORD
|
|
|
|
Remove-Item Env:PIP_AUTO_COMPLETE
|
|
|
|
}}
|
|
|
|
elseif (Test-Path Function:\\_pip_completeBackup) {{
|
|
|
|
# Fall back on existing tab expansion
|
|
|
|
_pip_completeBackup $line $lastWord
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
""",
|
2009-11-20 19:20:52 +01:00
|
|
|
}
|
2010-06-03 04:25:26 +02:00
|
|
|
|
2009-11-20 19:20:52 +01:00
|
|
|
|
|
|
|
class CompletionCommand(Command):
|
2013-02-16 19:00:29 +01:00
|
|
|
"""A helper command to be used for command completion."""
|
2019-07-10 09:36:33 +02:00
|
|
|
|
2017-05-18 15:20:01 +02:00
|
|
|
ignore_require_venv = True
|
2009-11-20 19:20:52 +01:00
|
|
|
|
2021-07-23 12:27:43 +02:00
|
|
|
def add_options(self) -> None:
|
2020-05-22 11:31:58 +02:00
|
|
|
self.cmd_opts.add_option(
|
2009-11-20 19:20:52 +01:00
|
|
|
"--bash",
|
|
|
|
"-b",
|
|
|
|
action="store_const",
|
|
|
|
const="bash",
|
|
|
|
dest="shell",
|
|
|
|
help="Emit completion code for bash",
|
|
|
|
)
|
2020-05-22 11:31:58 +02:00
|
|
|
self.cmd_opts.add_option(
|
2009-11-20 19:20:52 +01:00
|
|
|
"--zsh",
|
|
|
|
"-z",
|
|
|
|
action="store_const",
|
|
|
|
const="zsh",
|
|
|
|
dest="shell",
|
|
|
|
help="Emit completion code for zsh",
|
|
|
|
)
|
2020-05-22 11:31:58 +02:00
|
|
|
self.cmd_opts.add_option(
|
2016-07-21 01:57:20 +02:00
|
|
|
"--fish",
|
|
|
|
"-f",
|
|
|
|
action="store_const",
|
|
|
|
const="fish",
|
|
|
|
dest="shell",
|
|
|
|
help="Emit completion code for fish",
|
|
|
|
)
|
2022-04-15 10:32:42 +02:00
|
|
|
self.cmd_opts.add_option(
|
|
|
|
"--powershell",
|
|
|
|
"-p",
|
|
|
|
action="store_const",
|
|
|
|
const="powershell",
|
|
|
|
dest="shell",
|
|
|
|
help="Emit completion code for powershell",
|
|
|
|
)
|
2009-11-20 19:20:52 +01:00
|
|
|
|
2020-05-22 11:31:58 +02:00
|
|
|
self.parser.insert_option_group(0, self.cmd_opts)
|
2014-04-24 13:56:31 +02:00
|
|
|
|
2021-07-23 12:27:43 +02:00
|
|
|
def run(self, options: Values, args: List[str]) -> int:
|
2009-11-20 19:20:52 +01:00
|
|
|
"""Prints the completion code of the given shell"""
|
2010-04-16 00:19:31 +02:00
|
|
|
shells = COMPLETION_SCRIPTS.keys()
|
2013-02-16 19:02:41 +01:00
|
|
|
shell_options = ["--" + shell for shell in sorted(shells)]
|
2010-04-16 00:19:31 +02:00
|
|
|
if options.shell in shells:
|
2017-05-23 07:26:22 +02:00
|
|
|
script = textwrap.dedent(
|
2020-03-06 18:30:16 +01:00
|
|
|
COMPLETION_SCRIPTS.get(options.shell, "").format(prog=get_prog())
|
2017-05-23 07:26:22 +02:00
|
|
|
)
|
2020-02-29 20:53:59 +01:00
|
|
|
print(BASE_COMPLETION.format(script=script, shell=options.shell))
|
2020-04-14 21:41:57 +02:00
|
|
|
return SUCCESS
|
2010-04-16 00:19:31 +02:00
|
|
|
else:
|
2014-01-27 15:07:10 +01:00
|
|
|
sys.stderr.write(
|
2020-01-29 18:24:26 +01:00
|
|
|
"ERROR: You must pass {}\n".format(" or ".join(shell_options))
|
2014-01-27 15:07:10 +01:00
|
|
|
)
|
2020-05-15 13:27:31 +02:00
|
|
|
return SUCCESS
|