comparison env/lib/python3.9/site-packages/pip/_internal/cli/main_parser.py @ 0:4f3585e2f14b draft default tip

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