1from distutils.errors import DistutilsArgError
2from distutils.fancy_getopt import FancyGetopt
3
4from pipenv.patched.notpip._internal.utils.typing import MYPY_CHECK_RUNNING
5
6if MYPY_CHECK_RUNNING:
7    from typing import Dict, List
8
9
10_options = [
11    ("exec-prefix=", None, ""),
12    ("home=", None, ""),
13    ("install-base=", None, ""),
14    ("install-data=", None, ""),
15    ("install-headers=", None, ""),
16    ("install-lib=", None, ""),
17    ("install-platlib=", None, ""),
18    ("install-purelib=", None, ""),
19    ("install-scripts=", None, ""),
20    ("prefix=", None, ""),
21    ("root=", None, ""),
22    ("user", None, ""),
23]
24
25
26# typeshed doesn't permit Tuple[str, None, str], see python/typeshed#3469.
27_distutils_getopt = FancyGetopt(_options)  # type: ignore
28
29
30def parse_distutils_args(args):
31    # type: (List[str]) -> Dict[str, str]
32    """Parse provided arguments, returning an object that has the
33    matched arguments.
34
35    Any unknown arguments are ignored.
36    """
37    result = {}
38    for arg in args:
39        try:
40            _, match = _distutils_getopt.getopt(args=[arg])
41        except DistutilsArgError:
42            # We don't care about any other options, which here may be
43            # considered unrecognized since our option list is not
44            # exhaustive.
45            pass
46        else:
47            result.update(match.__dict__)
48    return result
49