import argparse import os import re import pathlib def build_argument_parser(description: str) -> argparse.ArgumentParser: parser = argparse.ArgumentParser(description=description) parser.add_argument( "--data-dir", "-d", type=valid_directory, required=True, help="Data directory from 5etools", ) parser.add_argument( "--source-code", "-s", action="append", metavar="CODE", type=valid_source_code, help="Filter data to just the given sources, may be provided multiple times", ) return parser def valid_directory(path: str) -> pathlib.Path: if os.path.isdir(path): return pathlib.Path(os.path.realpath(path)) raise argparse.ArgumentTypeError(f"{path} is not a directory") def valid_source_code(code: str) -> str: if re.match(r"^[A-Z]+$", code) is None: raise argparse.ArgumentTypeError(f"{code} is not a valid data source code") return code