Path: blob/main/test/lib/python3.9/site-packages/pip/_internal/commands/completion.py
4804 views
import sys1import textwrap2from optparse import Values3from typing import List45from pip._internal.cli.base_command import Command6from pip._internal.cli.status_codes import SUCCESS7from pip._internal.utils.misc import get_prog89BASE_COMPLETION = """10# pip {shell} completion start{script}# pip {shell} completion end11"""1213COMPLETION_SCRIPTS = {14"bash": """15_pip_completion()16{{17COMPREPLY=( $( COMP_WORDS="${{COMP_WORDS[*]}}" \\18COMP_CWORD=$COMP_CWORD \\19PIP_AUTO_COMPLETE=1 $1 2>/dev/null ) )20}}21complete -o default -F _pip_completion {prog}22""",23"zsh": """24function _pip_completion {{25local words cword26read -Ac words27read -cn cword28reply=( $( COMP_WORDS="$words[*]" \\29COMP_CWORD=$(( cword-1 )) \\30PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null ))31}}32compctl -K _pip_completion {prog}33""",34"fish": """35function __fish_complete_pip36set -lx COMP_WORDS (commandline -o) ""37set -lx COMP_CWORD ( \\38math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\39)40set -lx PIP_AUTO_COMPLETE 141string split \\ -- (eval $COMP_WORDS[1])42end43complete -fa "(__fish_complete_pip)" -c {prog}44""",45"powershell": """46if ((Test-Path Function:\\TabExpansion) -and -not `47(Test-Path Function:\\_pip_completeBackup)) {{48Rename-Item Function:\\TabExpansion _pip_completeBackup49}}50function TabExpansion($line, $lastWord) {{51$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()52if ($lastBlock.StartsWith("{prog} ")) {{53$Env:COMP_WORDS=$lastBlock54$Env:COMP_CWORD=$lastBlock.Split().Length - 155$Env:PIP_AUTO_COMPLETE=156(& {prog}).Split()57Remove-Item Env:COMP_WORDS58Remove-Item Env:COMP_CWORD59Remove-Item Env:PIP_AUTO_COMPLETE60}}61elseif (Test-Path Function:\\_pip_completeBackup) {{62# Fall back on existing tab expansion63_pip_completeBackup $line $lastWord64}}65}}66""",67}686970class CompletionCommand(Command):71"""A helper command to be used for command completion."""7273ignore_require_venv = True7475def add_options(self) -> None:76self.cmd_opts.add_option(77"--bash",78"-b",79action="store_const",80const="bash",81dest="shell",82help="Emit completion code for bash",83)84self.cmd_opts.add_option(85"--zsh",86"-z",87action="store_const",88const="zsh",89dest="shell",90help="Emit completion code for zsh",91)92self.cmd_opts.add_option(93"--fish",94"-f",95action="store_const",96const="fish",97dest="shell",98help="Emit completion code for fish",99)100self.cmd_opts.add_option(101"--powershell",102"-p",103action="store_const",104const="powershell",105dest="shell",106help="Emit completion code for powershell",107)108109self.parser.insert_option_group(0, self.cmd_opts)110111def run(self, options: Values, args: List[str]) -> int:112"""Prints the completion code of the given shell"""113shells = COMPLETION_SCRIPTS.keys()114shell_options = ["--" + shell for shell in sorted(shells)]115if options.shell in shells:116script = textwrap.dedent(117COMPLETION_SCRIPTS.get(options.shell, "").format(prog=get_prog())118)119print(BASE_COMPLETION.format(script=script, shell=options.shell))120return SUCCESS121else:122sys.stderr.write(123"ERROR: You must pass {}\n".format(" or ".join(shell_options))124)125return SUCCESS126127128