You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

completion.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # The following comment should be removed at some point in the future.
  2. # mypy: disallow-untyped-defs=False
  3. from __future__ import absolute_import
  4. import sys
  5. import textwrap
  6. from pip._internal.cli.base_command import Command
  7. from pip._internal.utils.misc import get_prog
  8. BASE_COMPLETION = """
  9. # pip %(shell)s completion start%(script)s# pip %(shell)s 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)s
  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)s
  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)s
  42. """,
  43. }
  44. class CompletionCommand(Command):
  45. """A helper command to be used for command completion."""
  46. ignore_require_venv = True
  47. def __init__(self, *args, **kw):
  48. super(CompletionCommand, self).__init__(*args, **kw)
  49. cmd_opts = self.cmd_opts
  50. cmd_opts.add_option(
  51. '--bash', '-b',
  52. action='store_const',
  53. const='bash',
  54. dest='shell',
  55. help='Emit completion code for bash')
  56. cmd_opts.add_option(
  57. '--zsh', '-z',
  58. action='store_const',
  59. const='zsh',
  60. dest='shell',
  61. help='Emit completion code for zsh')
  62. cmd_opts.add_option(
  63. '--fish', '-f',
  64. action='store_const',
  65. const='fish',
  66. dest='shell',
  67. help='Emit completion code for fish')
  68. self.parser.insert_option_group(0, cmd_opts)
  69. def run(self, options, args):
  70. """Prints the completion code of the given shell"""
  71. shells = COMPLETION_SCRIPTS.keys()
  72. shell_options = ['--' + shell for shell in sorted(shells)]
  73. if options.shell in shells:
  74. script = textwrap.dedent(
  75. COMPLETION_SCRIPTS.get(options.shell, '') % {
  76. 'prog': get_prog(),
  77. }
  78. )
  79. print(BASE_COMPLETION % {'script': script, 'shell': options.shell})
  80. else:
  81. sys.stderr.write(
  82. 'ERROR: You must pass %s\n' % ' or '.join(shell_options)
  83. )