help.py 1.1 KB

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