4.10. filename/filepath validator for click¶
- Sample Code
import click from pathvalidate.click import validate_filename_arg, validate_filepath_arg @click.command() @click.option("--filename", callback=validate_filename_arg) @click.option("--filepath", callback=validate_filepath_arg) def cli(filename, filepath): if filename: click.echo("filename: {}".format(filename)) if filepath: click.echo("filepath: {}".format(filepath)) if __name__ == "__main__": cli()
- Output
$ ./examples/click_validate.py --filename ab filename: ab $ ./examples/click_validate.py --filepath e?g Usage: click_validate.py [OPTIONS] Error: Invalid value for "--filepath": invalid char found: invalids=('?'), value='e?g', reason=INVALID_CHARACTER, target-platform=Windows
4.11. filename/filepath sanitizer for click¶
- Sample Code
import click from pathvalidate.click import sanitize_filename_arg, sanitize_filepath_arg @click.command() @click.option("--filename", callback=sanitize_filename_arg) @click.option("--filepath", callback=sanitize_filepath_arg) def cli(filename, filepath): if filename: click.echo("filename: {}".format(filename)) if filepath: click.echo("filepath: {}".format(filepath)) if __name__ == "__main__": cli()
- Output
$ ./examples/click_sanitize.py --filename a/b filename: ab