completion.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. }
  44. class CompletionCommand(Command):
  45. """A helper command to be used for command completion."""
  46. ignore_require_venv = True
  47. def add_options(self) -> None:
  48. self.cmd_opts.add_option(
  49. '--bash', '-b',
  50. action='store_const',
  51. const='bash',
  52. dest='shell',
  53. help='Emit completion code for bash')
  54. self.cmd_opts.add_option(
  55. '--zsh', '-z',
  56. action='store_const',
  57. const='zsh',
  58. dest='shell',
  59. help='Emit completion code for zsh')
  60. self.cmd_opts.add_option(
  61. '--fish', '-f',
  62. action='store_const',
  63. const='fish',
  64. dest='shell',
  65. help='Emit completion code for fish')
  66. self.parser.insert_option_group(0, self.cmd_opts)
  67. def run(self, options: Values, args: List[str]) -> int:
  68. """Prints the completion code of the given shell"""
  69. shells = COMPLETION_SCRIPTS.keys()
  70. shell_options = ['--' + shell for shell in sorted(shells)]
  71. if options.shell in shells:
  72. script = textwrap.dedent(
  73. COMPLETION_SCRIPTS.get(options.shell, '').format(
  74. prog=get_prog())
  75. )
  76. print(BASE_COMPLETION.format(script=script, shell=options.shell))
  77. return SUCCESS
  78. else:
  79. sys.stderr.write(
  80. 'ERROR: You must pass {}\n' .format(' or '.join(shell_options))
  81. )
  82. return SUCCESS