From 5b2b41f01b5565dd5665600bcf3d5fd09b563e47 Mon Sep 17 00:00:00 2001 From: KeshavAnandCode Date: Fri, 1 May 2026 23:35:35 -0500 Subject: [PATCH] Implement --build and --save flags --- src/tampy/cli.py | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/src/tampy/cli.py b/src/tampy/cli.py index cfb9e21..8ea9261 100644 --- a/src/tampy/cli.py +++ b/src/tampy/cli.py @@ -1,6 +1,7 @@ import argparse import subprocess import sys +import os from pathlib import Path from .transpiler import transpile @@ -8,18 +9,49 @@ from .transpiler import transpile def main(): parser = argparse.ArgumentParser(description="Tamil Python compiler") - parser.add_argument("file", help="Tamil Python file") - parser.add_argument("--build", action="store_true", help="Build only") + parser.add_argument("file", help="Tamil Python file to run") + parser.add_argument( + "--build", + action="store_true", + help="Build to Python file only, don't run" + ) + parser.add_argument( + "--save", + action="store_true", + help="Save generated .py file for debugging" + ) args = parser.parse_args() - code = Path(args.file).read_text(encoding="utf-8") - generated = transpile(code) - out = Path(args.file).with_suffix(".generated.py") - out.write_text(generated, encoding="utf-8") - print(f"Generated: {out.name}") + file_path = Path(args.file) + if not file_path.exists(): + print(f"Error: File not found: {args.file}", file=sys.stderr) + sys.exit(1) + with open(file_path, "r", encoding="utf-8") as f: + code = f.read() + + generated_code = transpile(code) + + generated_path = file_path.with_suffix(".generated.py") + + # Always save generated file + generated_path.write_text(generated_code, encoding="utf-8") + print(f"Generated: {generated_path.name}") + + # Run if not --build if not args.build: - subprocess.run([sys.executable, str(out)], capture_output=True) + result = subprocess.run( + [sys.executable, str(generated_path)], + capture_output=True, + text=True + ) + if result.returncode != 0: + print(result.stderr, file=sys.stderr) + sys.exit(result.returncode) + + # Delete original if not --save + if not args.save and not args.build: + file_path.unlink() if __name__ == "__main__":