completion.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from __future__ import absolute_import
  2. import sys
  3. import textwrap
  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. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  8. if MYPY_CHECK_RUNNING:
  9. from typing import List
  10. from optparse import Values
  11. BASE_COMPLETION = """
  12. # pip {shell} completion start{script}# pip {shell} completion end
  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. function _pip_completion {{
  26. local words cword
  27. read -Ac words
  28. read -cn cword
  29. reply=( $( COMP_WORDS="$words[*]" \\
  30. COMP_CWORD=$(( cword-1 )) \\
  31. PIP_AUTO_COMPLETE=1 $words[1] 2>/dev/null ))
  32. }}
  33. compctl -K _pip_completion {prog}
  34. """,
  35. 'fish': """
  36. function __fish_complete_pip
  37. set -lx COMP_WORDS (commandline -o) ""
  38. set -lx COMP_CWORD ( \\
  39. math (contains -i -- (commandline -t) $COMP_WORDS)-1 \\
  40. )
  41. set -lx PIP_AUTO_COMPLETE 1
  42. string split \\ -- (eval $COMP_WORDS[1])
  43. end
  44. complete -fa "(__fish_complete_pip)" -c {prog}
  45. """,
  46. }
  47. class CompletionCommand(Command):
  48. """A helper command to be used for command completion."""
  49. ignore_require_venv = True
  50. def add_options(self):
  51. # type: () -> None
  52. self.cmd_opts.add_option(
  53. '--bash', '-b',
  54. action='store_const',
  55. const='bash',
  56. dest='shell',
  57. help='Emit completion code for bash')
  58. self.cmd_opts.add_option(
  59. '--zsh', '-z',
  60. action='store_const',
  61. const='zsh',
  62. dest='shell',
  63. help='Emit completion code for zsh')
  64. self.cmd_opts.add_option(
  65. '--fish', '-f',
  66. action='store_const',
  67. const='fish',
  68. dest='shell',
  69. help='Emit completion code for fish')
  70. self.parser.insert_option_group(0, self.cmd_opts)
  71. def run(self, options, args):
  72. # type: (Values, List[str]) -> int
  73. """Prints the completion code of the given shell"""
  74. shells = COMPLETION_SCRIPTS.keys()
  75. shell_options = ['--' + shell for shell in sorted(shells)]
  76. if options.shell in shells:
  77. script = textwrap.dedent(
  78. COMPLETION_SCRIPTS.get(options.shell, '').format(
  79. prog=get_prog())
  80. )
  81. print(BASE_COMPLETION.format(script=script, shell=options.shell))
  82. return SUCCESS
  83. else:
  84. sys.stderr.write(
  85. 'ERROR: You must pass {}\n' .format(' or '.join(shell_options))
  86. )
  87. return SUCCESS