help.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from __future__ import absolute_import
  2. from pip._internal.cli.base_command import Command
  3. from pip._internal.cli.status_codes import SUCCESS
  4. from pip._internal.exceptions import CommandError
  5. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  6. if MYPY_CHECK_RUNNING:
  7. from typing import List
  8. from optparse import Values
  9. class HelpCommand(Command):
  10. """Show help for commands"""
  11. usage = """
  12. %prog <command>"""
  13. ignore_require_venv = True
  14. def run(self, options, args):
  15. # type: (Values, List[str]) -> int
  16. from pip._internal.commands import (
  17. commands_dict, create_command, get_similar_commands,
  18. )
  19. try:
  20. # 'pip help' with no args is handled by pip.__init__.parseopt()
  21. cmd_name = args[0] # the command we need help for
  22. except IndexError:
  23. return SUCCESS
  24. if cmd_name not in commands_dict:
  25. guess = get_similar_commands(cmd_name)
  26. msg = ['unknown command "{}"'.format(cmd_name)]
  27. if guess:
  28. msg.append('maybe you meant "{}"'.format(guess))
  29. raise CommandError(' - '.join(msg))
  30. command = create_command(cmd_name)
  31. command.parser.print_help()
  32. return SUCCESS