From a0beb6ae95e8cbce690562afc145ffdbf197a81d Mon Sep 17 00:00:00 2001 From: waldo121 <17034517+waldo121@users.noreply.github.com> Date: Wed, 29 Apr 2026 09:30:15 -0400 Subject: [PATCH] feat: add --version flag --- docs/docs/cli-reference.md | 9 +++++++++ oracletrace/cli.py | 10 ++++++++++ tests/test_cli.py | 9 +++++++++ 3 files changed, 28 insertions(+) diff --git a/docs/docs/cli-reference.md b/docs/docs/cli-reference.md index a0f3766..5bc8a55 100644 --- a/docs/docs/cli-reference.md +++ b/docs/docs/cli-reference.md @@ -15,6 +15,7 @@ oracletrace [--json OUTPUT.json] [--csv OUTPUT.csv] [--compare BASELINE oracletrace [--ignore REGEX [REGEX ...]] oracletrace [--top NUMBER] oracletrace [--compare BASELINE.json] [--fail-on-regression] [--threshold PERCENT] [--only-regressions] +oracletrace --version ``` ## Required argument @@ -117,6 +118,14 @@ Limit the summary table output to a maximum number of results. oracletrace my_app.py --top 10 ``` +### `--version` + +Prints version information and exit with a zero code. + +```bash +oracletrace --version +``` + ## Exit behavior OracleTrace returns a non-zero exit code when: diff --git a/oracletrace/cli.py b/oracletrace/cli.py index a531166..55aa3fe 100644 --- a/oracletrace/cli.py +++ b/oracletrace/cli.py @@ -11,10 +11,13 @@ from argparse import ArgumentParser, Namespace from pathlib import Path from dataclasses import asdict +from importlib.metadata import version def main() -> int: + module_name = __name__.split(".")[0] parser: ArgumentParser = ArgumentParser( + prog=module_name, description="OracleTrace - Lightweight execution tracer for Python projects" ) parser.add_argument("target", help="Python script to trace") @@ -48,6 +51,13 @@ def main() -> int: action="store_true", help="Hide functions which didn't run slower than baseline. Use with --compare" ) + + parser.add_argument( + "--version", + action="version", + help="Print version information then exit with a zero code", + version=f"%(prog)s {version(module_name)}" + ) args: Namespace = parser.parse_args() target: str = args.target diff --git a/tests/test_cli.py b/tests/test_cli.py index 6f2532c..b8e3f17 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -427,3 +427,12 @@ def test_main_shows_only_regressions(monkeypatch, tmp_path, trace_data, baseline assert f" total_time: {baseline_trace_data.functions[0].total_time:.4f}s → {trace_data.functions[0].total_time:.4f}s" in captured.out assert f"(+{102.2:.2f}%)\n" in captured.out assert f"{trace_data.functions[1].name}\n" not in captured.out + +def test_main_prints_version_exits_0(monkeypatch, trace_data, capsys): + module_name = "oracletrace" + with pytest.raises(SystemExit): + exit_code = _run_cli(monkeypatch,["oracletrace","--version",]) + + captured = capsys.readouterr() + assert exit_code == 0 + assert f"{module_name} {importlib.metadata.version(module_name)}" in captured.out