Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main_parser.py 2.7KB

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