Skip to content

Commit e351aa3

Browse files
committed
feat: show version info #13
1 parent 269941c commit e351aa3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/commitlint/cli.py

+30
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,37 @@
1717
import sys
1818
from typing import List
1919

20+
import pkg_resources # type: ignore
21+
2022
from .commitlint import check_commit_message, remove_comments
2123
from .exceptions import CommitlintException
2224
from .git_helpers import get_commit_message_of_hash, get_commit_messages_of_hash_range
2325
from .messages import VALIDATION_SUCCESSFUL
2426

2527

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+
2651
def get_args() -> argparse.Namespace:
2752
"""
2853
Parse CLI arguments for checking if a commit message.
@@ -37,6 +62,11 @@ def get_args() -> argparse.Namespace:
3762
description="Check if a commit message follows the conventional commit format."
3863
)
3964

65+
# version
66+
parser.add_argument(
67+
"--version", action="version", version=get_version_info(parser.prog)
68+
)
69+
4070
# for commit message check
4171
group = parser.add_mutually_exclusive_group(required=True)
4272
group.add_argument(

0 commit comments

Comments
 (0)