help.py 1.1 KB

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