completion.py 4.0 KB

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