argparse.py 970 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """
  2. .. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
  3. """
  4. from argparse import ArgumentTypeError
  5. from ._filename import sanitize_filename, validate_filename
  6. from ._filepath import sanitize_filepath, validate_filepath
  7. from .error import ValidationError
  8. def validate_filename_arg(value: str) -> str:
  9. if not value:
  10. return ""
  11. try:
  12. validate_filename(value)
  13. except ValidationError as e:
  14. raise ArgumentTypeError(e)
  15. return value
  16. def validate_filepath_arg(value: str) -> str:
  17. if not value:
  18. return ""
  19. try:
  20. validate_filepath(value, platform="auto")
  21. except ValidationError as e:
  22. raise ArgumentTypeError(e)
  23. return value
  24. def sanitize_filename_arg(value: str) -> str:
  25. if not value:
  26. return ""
  27. return sanitize_filename(value)
  28. def sanitize_filepath_arg(value: str) -> str:
  29. if not value:
  30. return ""
  31. return sanitize_filepath(value, platform="auto")