distutils_args.py 1.2 KB

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