python (3.11.7)

(root)/
lib/
python3.11/
site-packages/
pip/
_internal/
commands/
completion.py
       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          #compdef -P pip[0-9.]#
      26          compadd $( COMP_WORDS="$words[*]" \\
      27                     COMP_CWORD=$((CURRENT-1)) \\
      28                     PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null )
      29      """,
      30      "fish": """
      31          function __fish_complete_pip
      32              set -lx COMP_WORDS (commandline -o) ""
      33              set -lx COMP_CWORD ( \\
      34                  math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
      35              )
      36              set -lx PIP_AUTO_COMPLETE 1
      37              string split \\  -- (eval $COMP_WORDS[1])
      38          end
      39          complete -fa "(__fish_complete_pip)" -c {prog}
      40      """,
      41      "powershell": """
      42          if ((Test-Path Function:\\TabExpansion) -and -not `
      43              (Test-Path Function:\\_pip_completeBackup)) {{
      44              Rename-Item Function:\\TabExpansion _pip_completeBackup
      45          }}
      46          function TabExpansion($line, $lastWord) {{
      47              $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
      48              if ($lastBlock.StartsWith("{prog} ")) {{
      49                  $Env:COMP_WORDS=$lastBlock
      50                  $Env:COMP_CWORD=$lastBlock.Split().Length - 1
      51                  $Env:PIP_AUTO_COMPLETE=1
      52                  (& {prog}).Split()
      53                  Remove-Item Env:COMP_WORDS
      54                  Remove-Item Env:COMP_CWORD
      55                  Remove-Item Env:PIP_AUTO_COMPLETE
      56              }}
      57              elseif (Test-Path Function:\\_pip_completeBackup) {{
      58                  # Fall back on existing tab expansion
      59                  _pip_completeBackup $line $lastWord
      60              }}
      61          }}
      62      """,
      63  }
      64  
      65  
      66  class ESC[4;38;5;81mCompletionCommand(ESC[4;38;5;149mCommand):
      67      """A helper command to be used for command completion."""
      68  
      69      ignore_require_venv = True
      70  
      71      def add_options(self) -> None:
      72          self.cmd_opts.add_option(
      73              "--bash",
      74              "-b",
      75              action="store_const",
      76              const="bash",
      77              dest="shell",
      78              help="Emit completion code for bash",
      79          )
      80          self.cmd_opts.add_option(
      81              "--zsh",
      82              "-z",
      83              action="store_const",
      84              const="zsh",
      85              dest="shell",
      86              help="Emit completion code for zsh",
      87          )
      88          self.cmd_opts.add_option(
      89              "--fish",
      90              "-f",
      91              action="store_const",
      92              const="fish",
      93              dest="shell",
      94              help="Emit completion code for fish",
      95          )
      96          self.cmd_opts.add_option(
      97              "--powershell",
      98              "-p",
      99              action="store_const",
     100              const="powershell",
     101              dest="shell",
     102              help="Emit completion code for powershell",
     103          )
     104  
     105          self.parser.insert_option_group(0, self.cmd_opts)
     106  
     107      def run(self, options: Values, args: List[str]) -> int:
     108          """Prints the completion code of the given shell"""
     109          shells = COMPLETION_SCRIPTS.keys()
     110          shell_options = ["--" + shell for shell in sorted(shells)]
     111          if options.shell in shells:
     112              script = textwrap.dedent(
     113                  COMPLETION_SCRIPTS.get(options.shell, "").format(prog=get_prog())
     114              )
     115              print(BASE_COMPLETION.format(script=script, shell=options.shell))
     116              return SUCCESS
     117          else:
     118              sys.stderr.write(
     119                  "ERROR: You must pass {}\n".format(" or ".join(shell_options))
     120              )
     121              return SUCCESS