completion.py 3.9 KB

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