Skip to content

Commit 3c1295a

Browse files
committed
feat(cli): add cz --version back and add cz --report to separate them from cz version
1 parent aa82b98 commit 3c1295a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

commitizen/cli.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import logging
5+
import platform
56
import sys
67
from copy import deepcopy
78
from functools import partial
@@ -13,6 +14,7 @@
1314
from decli import cli
1415

1516
from commitizen import commands, config, out, version_schemes
17+
from commitizen.__version__ import __version__
1618
from commitizen.exceptions import (
1719
CommitizenException,
1820
ExitCode,
@@ -102,6 +104,16 @@ def __call__(
102104
"required": False,
103105
"help": "comma separated error codes that won't raise error, e.g: cz -nr 1,2,3 bump. See codes at https://commitizen-tools.github.io/commitizen/exit_codes/",
104106
},
107+
{
108+
"name": ["-v", "--version"],
109+
"action": "store_true",
110+
"help": "Show the version of the installed commitizen",
111+
},
112+
{
113+
"name": ["--report"],
114+
"action": "store_true",
115+
"help": "Show system information for reporting bugs",
116+
},
105117
],
106118
"subcommands": {
107119
"title": "commands",
@@ -629,13 +641,27 @@ class Args(argparse.Namespace):
629641

630642

631643
def main() -> None:
644+
sys.excepthook = commitizen_excepthook
645+
632646
parser: argparse.ArgumentParser = cli(data)
633647
argcomplete.autocomplete(parser)
634648
# Show help if no arg provided
635649
if len(sys.argv) == 1:
636650
parser.print_help(sys.stderr)
637651
raise ExpectedExit()
638652

653+
# TODO(bearomorphism): mark `cz version --commitizen` as deprecated after `cz version` feature is stable
654+
if "--version" in sys.argv:
655+
out.write(__version__)
656+
raise ExpectedExit()
657+
658+
# TODO(bearomorphism): mark `cz version --report` as deprecated after `cz version` feature is stable
659+
if "--report" in sys.argv:
660+
out.write(f"Commitizen Version: {__version__}")
661+
out.write(f"Python Version: {sys.version}")
662+
out.write(f"Operating System: {platform.system()}")
663+
raise ExpectedExit()
664+
639665
# This is for the command required constraint in 2.0
640666
try:
641667
args, unknown_args = parser.parse_known_args()
@@ -673,7 +699,6 @@ def main() -> None:
673699
elif not conf.path:
674700
conf.update({"name": "cz_conventional_commits"})
675701

676-
sys.excepthook = commitizen_excepthook
677702
if args.debug:
678703
logging.getLogger("commitizen").setLevel(logging.DEBUG)
679704
sys.excepthook = partial(sys.excepthook, debug=True)

tests/test_cli.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pytest_mock import MockFixture
99

1010
from commitizen import cli
11+
from commitizen.__version__ import __version__
1112
from commitizen.exceptions import (
1213
ConfigFileNotFound,
1314
ExpectedExit,
@@ -37,6 +38,24 @@ def test_cz_with_arg_but_without_command(util: UtilFixture):
3738
assert "Command is required" in str(excinfo.value)
3839

3940

41+
def test_cz_with_version_arg(util: UtilFixture, capsys):
42+
"""Test that cz shows the version when --version is used."""
43+
with pytest.raises(ExpectedExit):
44+
util.run_cli("--version")
45+
out, _ = capsys.readouterr()
46+
assert __version__ in out
47+
48+
49+
def test_cz_with_report_arg(util: UtilFixture, capsys):
50+
"""Test that cz shows the report when --report is used."""
51+
with pytest.raises(ExpectedExit):
52+
util.run_cli("--report")
53+
out, _ = capsys.readouterr()
54+
assert "Commitizen Version:" in out
55+
assert "Python Version:" in out
56+
assert "Operating System:" in out
57+
58+
4059
def test_name(util: UtilFixture, capsys):
4160
util.run_cli("-n", "cz_jira", "example")
4261
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)