main_parser.py 2.8 KB

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