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.
 
 
 
 

69 lines
1.9 KiB

  1. from __future__ import absolute_import
  2. import sys
  3. from pip.basecommand import Command
  4. BASE_COMPLETION = """
  5. # pip %(shell)s completion start%(script)s# pip %(shell)s completion end
  6. """
  7. COMPLETION_SCRIPTS = {
  8. 'bash': """
  9. _pip_completion()
  10. {
  11. COMPREPLY=( $( COMP_WORDS="${COMP_WORDS[*]}" \\
  12. COMP_CWORD=$COMP_CWORD \\
  13. PIP_AUTO_COMPLETE=1 $1 ) )
  14. }
  15. complete -o default -F _pip_completion pip
  16. """, 'zsh': """
  17. function _pip_completion {
  18. local words cword
  19. read -Ac words
  20. read -cn cword
  21. reply=( $( COMP_WORDS="$words[*]" \\
  22. COMP_CWORD=$(( cword-1 )) \\
  23. PIP_AUTO_COMPLETE=1 $words[1] ) )
  24. }
  25. compctl -K _pip_completion pip
  26. """}
  27. class CompletionCommand(Command):
  28. """A helper command to be used for command completion."""
  29. name = 'completion'
  30. summary = 'A helper command to be used for command completion'
  31. hidden = True
  32. def __init__(self, *args, **kw):
  33. super(CompletionCommand, self).__init__(*args, **kw)
  34. cmd_opts = self.cmd_opts
  35. cmd_opts.add_option(
  36. '--bash', '-b',
  37. action='store_const',
  38. const='bash',
  39. dest='shell',
  40. help='Emit completion code for bash')
  41. cmd_opts.add_option(
  42. '--zsh', '-z',
  43. action='store_const',
  44. const='zsh',
  45. dest='shell',
  46. help='Emit completion code for zsh')
  47. self.parser.insert_option_group(0, cmd_opts)
  48. def run(self, options, args):
  49. """Prints the completion code of the given shell"""
  50. shells = COMPLETION_SCRIPTS.keys()
  51. shell_options = ['--' + shell for shell in sorted(shells)]
  52. if options.shell in shells:
  53. script = COMPLETION_SCRIPTS.get(options.shell, '')
  54. print(BASE_COMPLETION % {'script': script, 'shell': options.shell})
  55. else:
  56. sys.stderr.write(
  57. 'ERROR: You must pass %s\n' % ' or '.join(shell_options)
  58. )