Add sanity_check_tool/src/sanity_check_tool/cli.py

This commit is contained in:
Mika 2026-01-23 12:53:28 +00:00
parent dfd7a69c92
commit 6cf46d8513

View file

@ -0,0 +1,52 @@
import argparse
import json
import sys
from pathlib import Path
from sanity_check_tool.core import perform_sanity_checks
def main() -> None:
"""Parse command-line arguments and run sanity checks on the provided run data JSON file."""
parser = argparse.ArgumentParser(
description="Run sanity checks for experimental run data."
)
parser.add_argument(
"--input",
required=True,
help="Pfad zur JSON-Datei mit Run-Daten."
)
args = parser.parse_args()
input_path = Path(args.input)
if not input_path.exists() or not input_path.is_file():
print(json.dumps({"error": f"Input file not found: {input_path}"}, indent=2), file=sys.stderr)
sys.exit(1)
try:
with input_path.open("r", encoding="utf-8") as fh:
run_data = json.load(fh)
except json.JSONDecodeError as e:
print(json.dumps({"error": f"Invalid JSON input: {e}"}, indent=2), file=sys.stderr)
sys.exit(1)
if not isinstance(run_data, dict):
print(json.dumps({"error": "Run data must be a JSON object."}, indent=2), file=sys.stderr)
sys.exit(1)
required_fields = {"runs", "sanity_checks"}
missing = required_fields - run_data.keys()
if missing:
print(json.dumps({"error": f"Missing required fields: {', '.join(missing)}"}, indent=2), file=sys.stderr)
sys.exit(1)
results = perform_sanity_checks(run_data)
# Output results as formatted JSON string
json.dump(results, sys.stdout, indent=2)
sys.stdout.write("\n")
if __name__ == "__main__":
main()