Skip to content

Commit 1e6afc2

Browse files
fix(cli): make --version and --report take precedence over subcommands
Top-level meta flags like --version and --report now take priority even when a subcommand is also provided (e.g., cz --version bump shows the version instead of running bump). The --report flag correctly defers to the version subcommand when used as cz version --report. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent bffea3d commit 1e6afc2

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

commitizen/cli.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -697,15 +697,19 @@ def main() -> None:
697697
raise NoCommandFoundError()
698698
raise e
699699

700+
if getattr(args, "version", False):
701+
out.write(__version__)
702+
raise ExpectedExit()
703+
704+
if getattr(args, "report", False) and (
705+
not hasattr(args, "func") or args.func is not commands.Version
706+
):
707+
out.write(f"Commitizen Version: {__version__}")
708+
out.write(f"Python Version: {sys.version}")
709+
out.write(f"Operating System: {platform.system()}")
710+
raise ExpectedExit()
711+
700712
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()
709713
raise NoCommandFoundError()
710714

711715
arguments = vars(args)

tests/test_cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def test_cz_with_version_short_arg(util: UtilFixture, capsys):
7272
assert __version__ in out
7373

7474

75+
def test_cz_version_flag_takes_precedence_over_subcommand(util: UtilFixture, capsys):
76+
"""Test that --version takes precedence even when a subcommand is given."""
77+
with pytest.raises(ExpectedExit):
78+
util.run_cli("--version", "bump")
79+
out, _ = capsys.readouterr()
80+
assert __version__ in out
81+
82+
7583
def test_cz_with_report_arg(util: UtilFixture, capsys):
7684
"""Test that cz shows the report when --report is used."""
7785
with pytest.raises(ExpectedExit):
@@ -82,6 +90,14 @@ def test_cz_with_report_arg(util: UtilFixture, capsys):
8290
assert "Operating System:" in out
8391

8492

93+
def test_cz_report_flag_takes_precedence_over_subcommand(util: UtilFixture, capsys):
94+
"""Test that --report takes precedence over non-version subcommands."""
95+
with pytest.raises(ExpectedExit):
96+
util.run_cli("--report", "bump")
97+
out, _ = capsys.readouterr()
98+
assert "Commitizen Version:" in out
99+
100+
85101
def test_name(util: UtilFixture, capsys):
86102
util.run_cli("-n", "cz_jira", "example")
87103
out, _ = capsys.readouterr()

0 commit comments

Comments
 (0)