Skip to content

Commit bffea3d

Browse files
fix(cli): use argparse for top-level --version/-v and --report flags
Replace raw sys.argv checks with proper argparse-based handling by making subcommands optional. This fixes three issues: - cz version --report was intercepted by the top-level handler instead of routing to the Version command - cz -v did not work despite being registered as a shorthand for --version - cz version --report and cz version --commitizen now emit deprecation warnings directing users to use cz --report and cz --version instead Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent fa9331c commit bffea3d

9 files changed

Lines changed: 53 additions & 36 deletions

commitizen/cli.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __call__(
119119
],
120120
"subcommands": {
121121
"title": "commands",
122-
"required": True,
122+
"required": False,
123123
"commands": [
124124
{
125125
"name": ["init"],
@@ -689,18 +689,6 @@ def main() -> None:
689689
parser.print_help(sys.stderr)
690690
raise ExpectedExit()
691691

692-
# TODO(bearomorphism): mark `cz version --commitizen` as deprecated after `cz version` feature is stable
693-
if "--version" in sys.argv:
694-
out.write(__version__)
695-
raise ExpectedExit()
696-
697-
# TODO(bearomorphism): mark `cz version --report` as deprecated after `cz version` feature is stable
698-
if "--report" in sys.argv:
699-
out.write(f"Commitizen Version: {__version__}")
700-
out.write(f"Python Version: {sys.version}")
701-
out.write(f"Operating System: {platform.system()}")
702-
raise ExpectedExit()
703-
704692
# This is for the command required constraint in 2.0
705693
try:
706694
args, unknown_args = parser.parse_known_args()
@@ -709,6 +697,17 @@ def main() -> None:
709697
raise NoCommandFoundError()
710698
raise e
711699

700+
if not hasattr(args, "func"):
701+
if getattr(args, "version", False):
702+
out.write(__version__)
703+
raise ExpectedExit()
704+
if getattr(args, "report", False):
705+
out.write(f"Commitizen Version: {__version__}")
706+
out.write(f"Python Version: {sys.version}")
707+
out.write(f"Operating System: {platform.system()}")
708+
raise ExpectedExit()
709+
raise NoCommandFoundError()
710+
712711
arguments = vars(args)
713712
if unknown_args:
714713
# Raise error for extra-args without -- separation

commitizen/commands/version.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import platform
22
import sys
3+
import warnings
34
from typing import TypedDict
45

56
from packaging.version import InvalidVersion
@@ -45,6 +46,12 @@ def __init__(self, config: BaseConfig, arguments: VersionArgs) -> None:
4546

4647
def __call__(self) -> None:
4748
if self.arguments.get("report"):
49+
warnings.warn(
50+
"`cz version --report` is deprecated and will be removed in v5. "
51+
"Use `cz --report` instead.",
52+
DeprecationWarning,
53+
stacklevel=2,
54+
)
4855
out.write(f"Commitizen Version: {__version__}")
4956
out.write(f"Python Version: {sys.version}")
5057
out.write(f"Operating System: {platform.system()}")
@@ -54,6 +61,12 @@ def __call__(self) -> None:
5461
out.write(f"Installed Commitizen Version: {__version__}")
5562

5663
if self.arguments.get("commitizen"):
64+
warnings.warn(
65+
"`cz version --commitizen` is deprecated and will be removed in v5. "
66+
"Use `cz --version` instead.",
67+
DeprecationWarning,
68+
stacklevel=2,
69+
)
5770
out.write(__version__)
5871
return
5972

tests/commands/test_version_command.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,23 @@ def test_version_no_arguments_shows_commitizen_version(config, capsys):
296296
commands.Version(config, {})()
297297
captured = capsys.readouterr()
298298
assert captured.out.strip() == __version__
299+
300+
301+
def test_version_report_emits_deprecation_warning(config, capsys):
302+
with pytest.warns(
303+
DeprecationWarning,
304+
match=r"`cz version --report` is deprecated.*Use `cz --report` instead",
305+
):
306+
commands.Version(config, {"report": True})()
307+
captured = capsys.readouterr()
308+
assert f"Commitizen Version: {__version__}" in captured.out
309+
310+
311+
def test_version_commitizen_emits_deprecation_warning(config, capsys):
312+
with pytest.warns(
313+
DeprecationWarning,
314+
match=r"`cz version --commitizen` is deprecated.*Use `cz --version` instead",
315+
):
316+
commands.Version(config, {"commitizen": True})()
317+
captured = capsys.readouterr()
318+
assert __version__ in captured.out

tests/test_cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ def test_cz_with_version_arg(util: UtilFixture, capsys):
6464
assert __version__ in out
6565

6666

67+
def test_cz_with_version_short_arg(util: UtilFixture, capsys):
68+
"""Test that cz shows the version when -v is used."""
69+
with pytest.raises(ExpectedExit):
70+
util.run_cli("-v")
71+
out, _ = capsys.readouterr()
72+
assert __version__ in out
73+
74+
6775
def test_cz_with_report_arg(util: UtilFixture, capsys):
6876
"""Test that cz shows the report when --report is used."""
6977
with pytest.raises(ExpectedExit):
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2-
[--report]
3-
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
4-
...
5-
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2-
[--report]
3-
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
4-
...
5-
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +0,0 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2-
[--report]
3-
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
4-
...
5-
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2-
[--report]
3-
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
4-
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
usage: cz [-h] [--config CONFIG] [--debug] [-n NAME] [-nr NO_RAISE] [-v]
2-
[--report]
3-
{init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version} ...
4-
cz: error: the following arguments are required: {init,commit,c,ls,example,info,schema,bump,changelog,ch,check,version}

0 commit comments

Comments
 (0)