distutils_args.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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):
  21. # type: (List[str]) -> Dict[str, str]
  22. """Parse provided arguments, returning an object that has the
  23. matched arguments.
  24. Any unknown arguments are ignored.
  25. """
  26. result = {}
  27. for arg in args:
  28. try:
  29. _, match = _distutils_getopt.getopt(args=[arg])
  30. except DistutilsArgError:
  31. # We don't care about any other options, which here may be
  32. # considered unrecognized since our option list is not
  33. # exhaustive.
  34. pass
  35. else:
  36. result.update(match.__dict__)
  37. return result