help.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 optparse import Values
  8. from typing import List
  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,
  18. create_command,
  19. get_similar_commands,
  20. )
  21. try:
  22. # 'pip help' with no args is handled by pip.__init__.parseopt()
  23. cmd_name = args[0] # the command we need help for
  24. except IndexError:
  25. return SUCCESS
  26. if cmd_name not in commands_dict:
  27. guess = get_similar_commands(cmd_name)
  28. msg = ['unknown command "{}"'.format(cmd_name)]
  29. if guess:
  30. msg.append('maybe you meant "{}"'.format(guess))
  31. raise CommandError(' - '.join(msg))
  32. command = create_command(cmd_name)
  33. command.parser.print_help()
  34. return SUCCESS