Skip to content

Commit c2c7c78

Browse files
committed
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 <[email protected]>
1 parent 2e1dc98 commit c2c7c78

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ ignore = [
114114
]
115115

116116
[tool.ruff.lint.per-file-ignores]
117+
# Top-level CLIs should be allowed to print if needed
118+
"src/dvsim/cli/**.py" = ["T201"]
117119
"tests/**.py" = [
118120
# Checks for boolean passed as positional argument which is useful in the
119121
# main codebase for clarity. However it's common to assert against a boolean

src/dvsim/cli/admin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
"""DVSim CLI main entry point."""
66

7+
from importlib.metadata import version
78
from pathlib import Path
89

910
import click
1011

1112

1213
@click.group()
14+
@click.version_option(version("dvsim"))
1315
def cli() -> None:
1416
"""DVSim Administration tool.
1517

src/dvsim/cli/run.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import subprocess
2929
import sys
3030
import textwrap
31+
from importlib.metadata import version
3132
from pathlib import Path
3233

3334
from dvsim.flow.factory import make_cfg
@@ -43,9 +44,6 @@
4344
from dvsim.logging import configure_logging, log
4445
from dvsim.utils import TS_FORMAT, TS_FORMAT_LONG, Timer, rm_path, run_cmd_with_timeout
4546

46-
# TODO: add dvsim_cfg.hjson to retrieve this info
47-
version = 0.1
48-
4947
# The different categories that can be passed to the --list argument.
5048
_LIST_CATEGORIES = ["build_modes", "run_modes", "tests", "regressions"]
5149

@@ -298,7 +296,7 @@ def parse_args():
298296

299297
parser.add_argument("cfg", metavar=cfg_metavar, help="""Configuration hjson file.""")
300298

301-
parser.add_argument("--version", action="store_true", help="Print version and exit")
299+
parser.add_argument("--version", action="version", version=version("dvsim"))
302300

303301
parser.add_argument(
304302
"--tool",
@@ -794,9 +792,6 @@ def parse_args():
794792

795793
args = parser.parse_args()
796794

797-
if args.version:
798-
sys.exit()
799-
800795
# Check conflicts
801796
# interactive and remote, r
802797
if args.interactive and args.remote:

0 commit comments

Comments
 (0)