Fix using validators set in __manifest__.py

E.g. you can use the validator 'is_file' to check if a config is a valid file
This commit is contained in:
Patrick Robertson
2025-02-04 13:36:05 +01:00
parent a873e56b87
commit b301f60ea3
3 changed files with 20 additions and 5 deletions

View File

@@ -1,7 +1,19 @@
# used as validators for config values.
# used as validators for config values. Should raise an exception if the value is invalid.
from pathlib import Path
import argparse
def example_validator(value):
return "example" in value
if "example" not in value:
raise argparse.ArgumentTypeError(f"{value} is not a valid value for this argument")
return value
def positive_number(value):
return value > 0
if value < 0:
raise argparse.ArgumentTypeError(f"{value} is not a positive number")
return value
def valid_file(value):
if not Path(value).is_file():
raise argparse.ArgumentTypeError(f"File '{value}' does not exist.")
return value