From ef769c3af35728cd78154f73f83252933f041cb5 Mon Sep 17 00:00:00 2001 From: Alex Jones Date: Fri, 30 Jan 2026 12:51:18 +0000 Subject: [PATCH] fix: fix/add --version option to the CLIs Rather than using the default non-configured hardcoded version, use `importlib.metadata` to get version information about the dvsim package itself, as that is what most users will be expecting. Make sure that for the main `run` argparse CLI we can use `--version` without needing to supply a HJSON configuration file. Add the `--version` flag to the `admin` click CLI also. Signed-off-by: Alex Jones --- pyproject.toml | 2 ++ src/dvsim/cli/admin.py | 2 ++ src/dvsim/cli/run.py | 9 ++------- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 9dfb68aa..c1ddfb78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -114,6 +114,8 @@ ignore = [ ] [tool.ruff.lint.per-file-ignores] +# Top-level CLIs should be allowed to print if needed +"src/dvsim/cli/**.py" = ["T201"] "tests/**.py" = [ # Checks for boolean passed as positional argument which is useful in the # main codebase for clarity. However it's common to assert against a boolean diff --git a/src/dvsim/cli/admin.py b/src/dvsim/cli/admin.py index 0ced939c..be30c6a2 100644 --- a/src/dvsim/cli/admin.py +++ b/src/dvsim/cli/admin.py @@ -4,12 +4,14 @@ """DVSim CLI main entry point.""" +from importlib.metadata import version from pathlib import Path import click @click.group() +@click.version_option(version("dvsim")) def cli() -> None: """DVSim Administration tool. diff --git a/src/dvsim/cli/run.py b/src/dvsim/cli/run.py index 7a7f7555..30113dbd 100644 --- a/src/dvsim/cli/run.py +++ b/src/dvsim/cli/run.py @@ -28,6 +28,7 @@ import subprocess import sys import textwrap +from importlib.metadata import version from pathlib import Path from dvsim.flow.factory import make_cfg @@ -43,9 +44,6 @@ from dvsim.logging import configure_logging, log from dvsim.utils import TS_FORMAT, TS_FORMAT_LONG, Timer, rm_path, run_cmd_with_timeout -# TODO: add dvsim_cfg.hjson to retrieve this info -version = 0.1 - # The different categories that can be passed to the --list argument. _LIST_CATEGORIES = ["build_modes", "run_modes", "tests", "regressions"] @@ -298,7 +296,7 @@ def parse_args(): parser.add_argument("cfg", metavar=cfg_metavar, help="""Configuration hjson file.""") - parser.add_argument("--version", action="store_true", help="Print version and exit") + parser.add_argument("--version", action="version", version=version("dvsim")) parser.add_argument( "--tool", @@ -794,9 +792,6 @@ def parse_args(): args = parser.parse_args() - if args.version: - sys.exit() - # Check conflicts # interactive and remote, r if args.interactive and args.remote: