Skip to content

Commit

Permalink
Added version flag to base tool
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoRoos committed Jan 31, 2025
1 parent 13dc810 commit 19648eb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ tc_make_release = "tctools.make_release.__main__:main_argv"
[tool.poetry-dynamic-versioning]
enable = true

[tool.poetry-dynamic-versioning.substitution]
files = ["src/tctools/__init__.py"]

# ----------- Other tools -----------

[tool.black]
Expand Down
5 changes: 5 additions & 0 deletions src/tctools/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from lxml import etree

from . import __version__

try:
import tomllib
except ImportError:
Expand Down Expand Up @@ -87,6 +89,9 @@ def get_argument_parser(cls) -> ArgumentParser:
@classmethod
def set_arguments(cls, parser):
"""Create application-specific arguments."""
parser.add_argument(
"--version", "-V", action="version", version="%(prog)s " + __version__
)
parser.add_argument(
"--dry",
help="Do not modify files on disk, only report changes to be made",
Expand Down
11 changes: 11 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from pathlib import Path
import sys
import subprocess

import pytest # noqa

Expand Down Expand Up @@ -48,6 +50,15 @@ def test_cli_arguments_default(self):
tool = MyTool()
assert tool.args.my_option == "default-text"

def test_cli_version(self, capsys):
with pytest.raises(SystemExit) as err:
_ = MyTool("--version")

assert err.type == SystemExit

message = capsys.readouterr().out
assert message

def test_config_file(self, tmp_path, monkeypatch):
conf_dir = tmp_path / "project"
work_dir = conf_dir / "subdir1" / "subdir2"
Expand Down

0 comments on commit 19648eb

Please sign in to comment.