# 4.8. filename/filepath validator for argparse

Sample Code:

      from argparse import ArgumentParser

      from pathvalidate.argparse import validate_filename_arg, validate_filepath_arg

      parser = ArgumentParser()
      parser.add_argument("--filename", type=validate_filename_arg)
      parser.add_argument("--filepath", type=validate_filepath_arg)
      options = parser.parse_args()

      if options.filename:
          print(f"filename: {options.filename}")

      if options.filepath:
          print(f"filepath: {options.filepath}")

Output:

      $ ./examples/argparse_validate.py --filename eg
      filename: eg
      $ ./examples/argparse_validate.py --filename e?g
      usage: argparse_validate.py [-h] [--filename FILENAME] [--filepath FILEPATH]
      argparse_validate.py: error: argument --filename: [PV1100] invalid characters found: invalids=(':'), value='e:g', platform=Windows

Note

`validate_filepath_arg` consider `platform` as of `"auto"` if the input is an absolute file path.

# 4.9. filename/filepath sanitizer for argparse

Sample Code:

      from argparse import ArgumentParser

      from pathvalidate.argparse import sanitize_filename_arg, sanitize_filepath_arg

      parser = ArgumentParser()
      parser.add_argument("--filename", type=sanitize_filename_arg)
      parser.add_argument("--filepath", type=sanitize_filepath_arg)
      options = parser.parse_args()

      if options.filename:
          print("filename: {}".format(options.filename))

      if options.filepath:
          print("filepath: {}".format(options.filepath))

Output:

      $ ./examples/argparse_sanitize.py --filename e/g
      filename: eg

Note

`sanitize_filepath_arg` is set platform as `"auto"`.