---
src/tomcli/cli/_util.py | 9 +++++++++
src/tomcli/cli/get.py | 5 +++--
src/tomcli/cli/set.py | 3 ++-
tests/test_tomcli_get.py | 8 ++++++++
4 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/src/tomcli/cli/_util.py b/src/tomcli/cli/_util.py
index d9e305d..197a643 100644
--- a/src/tomcli/cli/_util.py
+++ b/src/tomcli/cli/_util.py
@@ -23,3 +23,12 @@ def _std_cm(path: str, dash_stream: IO[AnyStr], mode: str) -> Iterator[IO[AnyStr
def fatal(*args: object, returncode: int = 1) -> NoReturn:
print(*args, file=sys.stderr)
raise Exit(returncode)
+
+
+def version_cb(val: bool):
+ from tomcli import __version__ as ver
+
+ if not val:
+ return
+ print(ver)
+ raise Exit
diff --git a/src/tomcli/cli/get.py b/src/tomcli/cli/get.py
index cb95c6d..47f7a87 100644
--- a/src/tomcli/cli/get.py
+++ b/src/tomcli/cli/get.py
@@ -8,9 +8,9 @@ import sys
from collections.abc import Mapping, MutableMapping
from typing import Any, Optional
-from typer import Argument, Typer
+from typer import Argument, Option, Typer
-from tomcli.cli._util import _std_cm, fatal
+from tomcli.cli._util import _std_cm, fatal, version_cb
from tomcli.toml import Reader, Writer, dump, load
app = Typer(context_settings=dict(help_option_names=["-h", "--help"]))
@@ -39,6 +39,7 @@ def get(
selector: str = Argument("."),
reader: Optional[Reader] = None,
writer: Optional[Writer] = None,
+ _: Optional[bool] = Option(None, "--version", is_eager=True, callback=version_cb),
):
# Allow fallback if options are not passed
allow_fallback_r = not bool(reader)
diff --git a/src/tomcli/cli/set.py b/src/tomcli/cli/set.py
index f3ef45b..7ff4b20 100644
--- a/src/tomcli/cli/set.py
+++ b/src/tomcli/cli/set.py
@@ -20,7 +20,7 @@ else:
from typer import Argument, Context, Option, Typer
-from tomcli.cli._util import _std_cm, fatal
+from tomcli.cli._util import _std_cm, fatal, version_cb
from tomcli.toml import Reader, Writer, dump, load
app = Typer(context_settings=dict(help_option_names=["-h", "--help"]))
@@ -82,6 +82,7 @@ def callback(
),
reader: Optional[Reader] = None,
writer: Optional[Writer] = None,
+ _: Optional[bool] = Option(None, "--version", is_eager=True, callback=version_cb),
):
ctx.obj = ModderCtx(path, output or path, reader, writer)
diff --git a/tests/test_tomcli_get.py b/tests/test_tomcli_get.py
index 549d4b8..49526e0 100644
--- a/tests/test_tomcli_get.py
+++ b/tests/test_tomcli_get.py
@@ -70,3 +70,11 @@ build-backend = "hatchling.build"
]
assert ran.exit_code == 0
assert ran.stdout.strip() in valid
+
+
+def test_get_version():
+ from tomcli import __version__ as ver
+
+ ran = typer.testing.CliRunner().invoke(app, ["--version"])
+ assert ran.exit_code == 0
+ assert ran.stdout == ver + "\n"
--
2.41.0