completion.py 4.4 KB

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