~gotmax23/tomcli

tomcli: Add --version argument v1 PROPOSED

Maxwell G: 1
 Add --version argument

 4 files changed, 22 insertions(+), 3 deletions(-)
#1056328 main.yml success
#1056329 mockbuild-epel9.yml failed
#1056330 mockbuild-f37.yml failed
#1056331 mockbuild.yml failed
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~gotmax23/tomcli/patches/44584/mbox | git am -3
Learn more about email & git

[PATCH tomcli] Add --version argument Export this patch

---
 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
tomcli/patches: FAILED in 4m20s

[Add --version argument][0] from [Maxwell G][1]

[0]: https://lists.sr.ht/~gotmax23/tomcli/patches/44584
[1]: mailto:maxwell@gtmx.me

✗ #1056331 FAILED  tomcli/patches/mockbuild.yml       https://builds.sr.ht/~gotmax23/job/1056331
✗ #1056329 FAILED  tomcli/patches/mockbuild-epel9.yml https://builds.sr.ht/~gotmax23/job/1056329
✗ #1056330 FAILED  tomcli/patches/mockbuild-f37.yml   https://builds.sr.ht/~gotmax23/job/1056330
✓ #1056328 SUCCESS tomcli/patches/main.yml            https://builds.sr.ht/~gotmax23/job/1056328