main_parser.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """A single place for constructing and exposing the main parser
  2. """
  3. import os
  4. import sys
  5. from typing import List, Tuple
  6. from pip._internal.cli import cmdoptions
  7. from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
  8. from pip._internal.commands import commands_dict, get_similar_commands
  9. from pip._internal.exceptions import CommandError
  10. from pip._internal.utils.misc import get_pip_version, get_prog
  11. __all__ = ["create_main_parser", "parse_command"]
  12. def create_main_parser():
  13. # type: () -> ConfigOptionParser
  14. """Creates and returns the main parser for pip's CLI"""
  15. parser = ConfigOptionParser(
  16. usage="\n%prog <command> [options]",
  17. add_help_option=False,
  18. formatter=UpdatingDefaultsHelpFormatter(),
  19. name="global",
  20. prog=get_prog(),
  21. )
  22. parser.disable_interspersed_args()
  23. parser.version = get_pip_version()
  24. # add the general options
  25. gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser)
  26. parser.add_option_group(gen_opts)
  27. # so the help formatter knows
  28. parser.main = True # type: ignore
  29. # create command listing for description
  30. description = [""] + [
  31. f"{name:27} {command_info.summary}"
  32. for name, command_info in commands_dict.items()
  33. ]
  34. parser.description = "\n".join(description)
  35. return parser
  36. def parse_command(args):
  37. # type: (List[str]) -> Tuple[str, List[str]]
  38. parser = create_main_parser()
  39. # Note: parser calls disable_interspersed_args(), so the result of this
  40. # call is to split the initial args into the general options before the
  41. # subcommand and everything else.
  42. # For example:
  43. # args: ['--timeout=5', 'install', '--user', 'INITools']
  44. # general_options: ['--timeout==5']
  45. # args_else: ['install', '--user', 'INITools']
  46. general_options, args_else = parser.parse_args(args)
  47. # --version
  48. if general_options.version:
  49. sys.stdout.write(parser.version)
  50. sys.stdout.write(os.linesep)
  51. sys.exit()
  52. # pip || pip help -> print_help()
  53. if not args_else or (args_else[0] == "help" and len(args_else) == 1):
  54. parser.print_help()
  55. sys.exit()
  56. # the subcommand name
  57. cmd_name = args_else[0]
  58. if cmd_name not in commands_dict:
  59. guess = get_similar_commands(cmd_name)
  60. msg = [f'unknown command "{cmd_name}"']
  61. if guess:
  62. msg.append(f'maybe you meant "{guess}"')
  63. raise CommandError(" - ".join(msg))
  64. # all the args without the subcommand
  65. cmd_args = args[:]
  66. cmd_args.remove(cmd_name)
  67. return cmd_name, cmd_args