Skip to content

Commit a42800c

Browse files
committed
feat(cli): add cz --version back and add cz --report to separate them from cz version
1 parent 2cfb8c6 commit a42800c

17 files changed

+83
-15
lines changed

commitizen/cli.py

Lines changed: 24 additions & 0 deletions
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.defaults import DEFAULT_SETTINGS
1719
from commitizen.exceptions import (
1820
CommitizenException,
@@ -103,6 +105,16 @@ def __call__(
103105
"required": False,
104106
"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/",
105107
},
108+
{
109+
"name": ["-v", "--version"],
110+
"action": "store_true",
111+
"help": "Show the version of the installed commitizen",
112+
},
113+
{
114+
"name": ["--report"],
115+
"action": "store_true",
116+
"help": "Show system information for reporting bugs",
117+
},
106118
],
107119
"subcommands": {
108120
"title": "commands",
@@ -643,6 +655,18 @@ def main() -> None:
643655
parser.print_help(sys.stderr)
644656
raise ExpectedExit()
645657

658+
# TODO(bearomorphism): mark `cz version --commitizen` as deprecated after `cz version` feature is stable
659+
if "--version" in sys.argv:
660+
out.write(__version__)
661+
raise ExpectedExit()
662+
663+
# TODO(bearomorphism): mark `cz version --report` as deprecated after `cz version` feature is stable
664+
if "--report" in sys.argv:
665+
out.write(f"Commitizen Version: {__version__}")
666+
out.write(f"Python Version: {sys.version}")
667+
out.write(f"Operating System: {platform.system()}")
668+
raise ExpectedExit()
669+
646670
# This is for the command required constraint in 2.0
647671
try:
648672
args, unknown_args = parser.parse_known_args()

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,
@@ -55,6 +56,24 @@ def test_cz_with_arg_but_without_command(util: UtilFixture):
5556
assert "Command is required" in str(excinfo.value)
5657

5758

59+
def test_cz_with_version_arg(util: UtilFixture, capsys):
60+
"""Test that cz shows the version when --version is used."""
61+
with pytest.raises(ExpectedExit):
62+
util.run_cli("--version")
63+
out, _ = capsys.readouterr()
64+
assert __version__ in out
65+
66+
67+
def test_cz_with_report_arg(util: UtilFixture, capsys):
68+
"""Test that cz shows the report when --report is used."""
69+
with pytest.raises(ExpectedExit):
70+
util.run_cli("--report")
71+
out, _ = capsys.readouterr()
72+
assert "Commitizen Version:" in out
73+
assert "Python Version:" in out
74+
assert "Operating System:" in out
75+
76+
5877
def test_name(util: UtilFixture, capsys):
5978
util.run_cli("-n", "cz_jira", "example")
6079
out, _ = capsys.readouterr()
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
34
...
45
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
34
...
45
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from 'init', 'commit', 'c', 'ls', 'example', 'info', 'schema', 'bump', 'changelog', 'ch', 'check', 'version')
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
34
...
45
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
34
...
45
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from 'init', 'commit', 'c', 'ls', 'example', 'info', 'schema', 'bump', 'changelog', 'ch', 'check', 'version')
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
34
...
45
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
34
...
45
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from init, commit, c, ls, example, info, schema, bump, changelog, ch, check, version)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
34
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE]
1+
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2+
[--report]
23
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
34
cz: error: argument {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}: invalid choice: 'invalidCommand' (choose from init, commit, c, ls, example, info, schema, bump, changelog, ch, check, version)

0 commit comments

Comments
 (0)