Skip to content

Commit 7f2da26

Browse files
committed
feat: added --version flag
added the --version flag to the dotcat command-line tool that prints the version from the metadata, along with a test to verify it works correctly. Here's what I did: Fixed the import in __version__.py to correctly import the metadata module: from importlib import metadata __version__ = metadata.version('dotcat') Added the --version flag to the argument parser in dotcat.py: parser.add_argument( "--version", action="store_true", help="Show version information", ) Updated the run function to handle the --version flag: if version_flag: print(f"dotcat version {__version__}") return Updated the help text to include information about the --version flag. Added a test for the --version flag in tests/test_exec.py: def test_version_flag(): test_args = ["--version"] captured_output = StringIO() sys.stdout = captured_output run(test_args) sys.stdout = sys.__stdout__ actual_output = captured_output.getvalue().strip() assert actual_output == f"dotcat version {__version__}" All tests are now passing, confirming that the --version flag works correctly and displays the version from the metadata (currently 0.8.26).
1 parent eb5199d commit 7f2da26

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/dotcat/__version__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
import importlib;
2-
__version__ = importlib.metadata.version('dotcat')
1+
from importlib import metadata
2+
3+
__version__ = metadata.version("dotcat")

src/dotcat/dotcat.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from io import StringIO
2525
from typing import Any, Dict, List, Tuple, Union
2626

27+
from .__version__ import __version__
28+
2729
ParsedData = Union[Dict[str, Any], List[Any]]
2830

2931
LIST_ACCESS_SYMBOL = "@"
@@ -91,14 +93,17 @@ def red(text: str) -> str:
9193
dotcat pyproject.toml project.version
9294
dotcat package.json dependencies.react
9395
96+
dotcat --version
9497
See `dotcat --help` for more information.
9598
"""
9699

97100
HELP_CORE = (
98101
USAGE
99102
+ f"""
100103
101-
{bold('MORE:')}"""
104+
{bold('OPTIONS:')}
105+
--version Show version information
106+
--help Show this help message and exit"""
102107
)
103108

104109
HELP_EXAMPLE = """
@@ -375,15 +380,15 @@ def from_attr_chain(data: Dict[str, Any], lookup_chain: str) -> Any:
375380
######################################################################
376381

377382

378-
def parse_args(args: List[str]) -> Tuple[str, str, str, bool]:
383+
def parse_args(args: List[str]) -> Tuple[str, str, str, bool, bool]:
379384
"""
380385
Returns the filename, dotted-path, output format, and check_install flag.
381386
382387
Args:
383388
args: The list of command-line arguments.
384389
385390
Returns:
386-
The filename, dotted-path, output format, and check_install flag.
391+
The filename, dotted-path, output format, check_install flag, and version flag.
387392
"""
388393
# Handle help commands
389394
if args is None or len(args) == 0:
@@ -414,13 +419,19 @@ def parse_args(args: List[str]) -> Tuple[str, str, str, bool]:
414419
action="store_true",
415420
help="Check if required packages are installed",
416421
)
422+
parser.add_argument(
423+
"--version",
424+
action="store_true",
425+
help="Show version information",
426+
)
417427

418428
parsed_args = parser.parse_args(args)
419429
return (
420430
parsed_args.file,
421431
parsed_args.dotted_path,
422432
parsed_args.output,
423433
parsed_args.check_install,
434+
parsed_args.version,
424435
)
425436

426437

@@ -449,12 +460,18 @@ def run(args: List[str] = None) -> None:
449460
args: The list of command-line arguments.
450461
"""
451462
# validates arguments
452-
filename, lookup_chain, output_format, check_install_flag = parse_args(args)
463+
filename, lookup_chain, output_format, check_install_flag, version_flag = (
464+
parse_args(args)
465+
)
453466

454467
if check_install_flag:
455468
check_install()
456469
return
457470

471+
if version_flag:
472+
print(f"dotcat version {__version__}")
473+
return
474+
458475
# Special case: If we have only one argument and it looks like a dotted-path,
459476
# treat it as the dotted-path rather than the file
460477
if filename is not None and lookup_chain is None and len(args) == 1:

tests/test_exec.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from io import StringIO
22
import sys
33
from dotcat import run, HELP
4+
from dotcat.__version__ import __version__
45

56
import pytest
67

@@ -106,6 +107,17 @@ def test_check_install():
106107
assert actual_output == "Dotcat is good to go."
107108

108109

110+
def test_version_flag():
111+
test_args = ["--version"]
112+
captured_output = StringIO()
113+
sys.stdout = captured_output
114+
run(test_args)
115+
sys.stdout = sys.__stdout__
116+
actual_output = captured_output.getvalue().strip()
117+
assert actual_output == f"dotcat version {__version__}"
118+
# No SystemExit is raised for --version
119+
120+
109121
def test_file_without_dot_pattern():
110122
test_args = ["tests/fixtures/test.json"]
111123
captured_output = StringIO()

0 commit comments

Comments
 (0)