help.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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: Values, args: List[str]) -> int:
  12. from pip._internal.commands import (
  13. commands_dict,
  14. create_command,
  15. get_similar_commands,
  16. )
  17. try:
  18. # 'pip help' with no args is handled by pip.__init__.parseopt()
  19. cmd_name = args[0] # the command we need help for
  20. except IndexError:
  21. return SUCCESS
  22. if cmd_name not in commands_dict:
  23. guess = get_similar_commands(cmd_name)
  24. msg = [f'unknown command "{cmd_name}"']
  25. if guess:
  26. msg.append(f'maybe you meant "{guess}"')
  27. raise CommandError(" - ".join(msg))
  28. command = create_command(cmd_name)
  29. command.parser.print_help()
  30. return SUCCESS