17
17
import sys
18
18
from typing import List
19
19
20
+ import pkg_resources # type: ignore
21
+
20
22
from .commitlint import check_commit_message , remove_comments
21
23
from .exceptions import CommitlintException
22
24
from .git_helpers import get_commit_message_of_hash , get_commit_messages_of_hash_range
23
25
from .messages import VALIDATION_SUCCESSFUL
24
26
25
27
28
+ def get_version_info (package_name : str ) -> str :
29
+ """
30
+ Returns the version information for the specified package.
31
+
32
+ Args:
33
+ package_name (str): The name of the package to retrieve version
34
+ information for.
35
+
36
+ Returns:
37
+ str: A string containing the version information if available,
38
+ or a message indicating that the version information is not available.
39
+
40
+ Example:
41
+ >>> get_version_info('commitlint')
42
+ commitlint 0.2.1
43
+ """
44
+ try :
45
+ version = pkg_resources .get_distribution (package_name ).version
46
+ return f"{ package_name } { version } "
47
+ except pkg_resources .DistributionNotFound :
48
+ return "Version information not available"
49
+
50
+
26
51
def get_args () -> argparse .Namespace :
27
52
"""
28
53
Parse CLI arguments for checking if a commit message.
@@ -37,6 +62,11 @@ def get_args() -> argparse.Namespace:
37
62
description = "Check if a commit message follows the conventional commit format."
38
63
)
39
64
65
+ # version
66
+ parser .add_argument (
67
+ "--version" , action = "version" , version = get_version_info (parser .prog )
68
+ )
69
+
40
70
# for commit message check
41
71
group = parser .add_mutually_exclusive_group (required = True )
42
72
group .add_argument (
0 commit comments