Skip to content

Commit f5c382c

Browse files
fix(cli): error on unknown args without a subcommand
When invoked with unknown arguments and no subcommand (e.g. `cz --invalid-arg`), the parser silently fell through to `NoCommandFoundError` because `parse_known_args()` collects unknown args without erroring. Re-run `parser.parse_args()` in that path so argparse's `unrecognized arguments` error is surfaced to stderr, then convert the `SystemExit` into `NoCommandFoundError` to keep the existing exit-code contract. Regenerate the empty `test_invalid_command_py_*___invalid_arg_.txt` regression baselines accordingly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 549fbcf commit f5c382c

6 files changed

Lines changed: 30 additions & 0 deletions

commitizen/cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,13 @@ def main() -> None:
702702
raise ExpectedExit()
703703

704704
if not hasattr(args, "func"):
705+
if unknown_args:
706+
try:
707+
parser.parse_args()
708+
except SystemExit as e:
709+
if e.code == 2:
710+
raise NoCommandFoundError()
711+
raise e
705712
if getattr(args, "report", False):
706713
out.write(f"Commitizen Version: {__version__}")
707714
out.write(f"Python Version: {sys.version}")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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: unrecognized arguments: --invalid-arg
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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: unrecognized arguments: --invalid-arg
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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: unrecognized arguments: --invalid-arg
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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: unrecognized arguments: --invalid-arg
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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: unrecognized arguments: --invalid-arg

0 commit comments

Comments
 (0)