1 """A single place for constructing and exposing the main parser
7 from typing
import List
, Optional
, Tuple
9 from pip
._internal
.build_env
import get_runnable_pip
10 from pip
._internal
.cli
import cmdoptions
11 from pip
._internal
.cli
.parser
import ConfigOptionParser
, UpdatingDefaultsHelpFormatter
12 from pip
._internal
.commands
import commands_dict
, get_similar_commands
13 from pip
._internal
.exceptions
import CommandError
14 from pip
._internal
.utils
.misc
import get_pip_version
, get_prog
16 __all__
= ["create_main_parser", "parse_command"]
19 def create_main_parser() -> ConfigOptionParser
:
20 """Creates and returns the main parser for pip's CLI"""
22 parser
= ConfigOptionParser(
23 usage
="\n%prog <command> [options]",
24 add_help_option
=False,
25 formatter
=UpdatingDefaultsHelpFormatter(),
29 parser
.disable_interspersed_args()
31 parser
.version
= get_pip_version()
33 # add the general options
34 gen_opts
= cmdoptions
.make_option_group(cmdoptions
.general_group
, parser
)
35 parser
.add_option_group(gen_opts
)
37 # so the help formatter knows
38 parser
.main
= True # type: ignore
40 # create command listing for description
41 description
= [""] + [
42 f
"{name:27} {command_info.summary}"
43 for name
, command_info
in commands_dict
.items()
45 parser
.description
= "\n".join(description
)
50 def identify_python_interpreter(python
: str) -> Optional
[str]:
51 # If the named file exists, use it.
52 # If it's a directory, assume it's a virtual environment and
53 # look for the environment's Python executable.
54 if os
.path
.exists(python
):
55 if os
.path
.isdir(python
):
56 # bin/python for Unix, Scripts/python.exe for Windows
57 # Try both in case of odd cases like cygwin.
58 for exe
in ("bin/python", "Scripts/python.exe"):
59 py
= os
.path
.join(python
, exe
)
60 if os
.path
.exists(py
):
65 # Could not find the interpreter specified
69 def parse_command(args
: List
[str]) -> Tuple
[str, List
[str]]:
70 parser
= create_main_parser()
72 # Note: parser calls disable_interspersed_args(), so the result of this
73 # call is to split the initial args into the general options before the
74 # subcommand and everything else.
76 # args: ['--timeout=5', 'install', '--user', 'INITools']
77 # general_options: ['--timeout==5']
78 # args_else: ['install', '--user', 'INITools']
79 general_options
, args_else
= parser
.parse_args(args
)
82 if general_options
.python
and "_PIP_RUNNING_IN_SUBPROCESS" not in os
.environ
:
83 # Re-invoke pip using the specified Python interpreter
84 interpreter
= identify_python_interpreter(general_options
.python
)
85 if interpreter
is None:
87 f
"Could not locate Python interpreter {general_options.python}"
96 # Set a flag so the child doesn't re-invoke itself, causing
98 os
.environ
["_PIP_RUNNING_IN_SUBPROCESS"] = "1"
101 proc
= subprocess
.run(pip_cmd
)
102 returncode
= proc
.returncode
103 except (subprocess
.SubprocessError
, OSError) as exc
:
104 raise CommandError(f
"Failed to run pip under {interpreter}: {exc}")
108 if general_options
.version
:
109 sys
.stdout
.write(parser
.version
)
110 sys
.stdout
.write(os
.linesep
)
113 # pip || pip help -> print_help()
114 if not args_else
or (args_else
[0] == "help" and len(args_else
) == 1):
118 # the subcommand name
119 cmd_name
= args_else
[0]
121 if cmd_name
not in commands_dict
:
122 guess
= get_similar_commands(cmd_name
)
124 msg
= [f
'unknown command "{cmd_name}"']
126 msg
.append(f
'maybe you meant "{guess}"')
128 raise CommandError(" - ".join(msg
))
130 # all the args without the subcommand
132 cmd_args
.remove(cmd_name
)
134 return cmd_name
, cmd_args