Skip to content

Commit c946bc0

Browse files
committed
refactor(cli): extract unknown args validation into a function
1 parent 6c8fad1 commit c946bc0

File tree

1 file changed

+40
-21
lines changed

1 file changed

+40
-21
lines changed

commitizen/cli.py

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
NoCommandFoundError,
2222
)
2323

24-
logger = logging.getLogger(__name__)
25-
2624

2725
class ParseKwargs(argparse.Action):
2826
"""
@@ -612,6 +610,43 @@ def exit_code_from_str_or_skip(s: str) -> ExitCode | None:
612610
]
613611

614612

613+
def _validate_unknown_args_and_parse_as_extra_cli_args(unknown_args: list[str]) -> str:
614+
"""Validate unknown arguments received from the command line and parse them as extra cli args.
615+
616+
The format of the extra cli args should be like this:
617+
```
618+
cz commit -- -s -n
619+
```
620+
621+
Raises:
622+
InvalidCommandArgumentError: if unknown arguments are found without -- separation
623+
InvalidCommandArgumentError: if unknown arguments are found before -- separator
624+
"""
625+
626+
# Raise error for extra-args without -- separation
627+
if "--" not in unknown_args:
628+
raise InvalidCommandArgumentError(
629+
f"Invalid commitizen arguments were found: `{' '.join(unknown_args)}`. "
630+
"Please use -- separator for extra git args"
631+
)
632+
633+
# Raise error for extra-args before --
634+
if unknown_args[0] != "--":
635+
pos = unknown_args.index("--")
636+
raise InvalidCommandArgumentError(
637+
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
638+
)
639+
640+
# Log warning for -- without any extra args
641+
if len(unknown_args) == 1:
642+
logger = logging.getLogger(__name__)
643+
logger.warning(
644+
"\nWARN: Incomplete commit command: received -- separator without any following git arguments\n"
645+
)
646+
647+
return " ".join(unknown_args[1:])
648+
649+
615650
if TYPE_CHECKING:
616651

617652
class Args(argparse.Namespace):
@@ -655,25 +690,9 @@ def main() -> None:
655690

656691
arguments = vars(args)
657692
if unknown_args:
658-
# Raise error for extra-args without -- separation
659-
if "--" not in unknown_args:
660-
raise InvalidCommandArgumentError(
661-
f"Invalid commitizen arguments were found: `{' '.join(unknown_args)}`. "
662-
"Please use -- separator for extra git args"
663-
)
664-
# Raise error for extra-args before --
665-
elif unknown_args[0] != "--":
666-
pos = unknown_args.index("--")
667-
raise InvalidCommandArgumentError(
668-
f"Invalid commitizen arguments were found before -- separator: `{' '.join(unknown_args[:pos])}`. "
669-
)
670-
# Log warning for -- without any extra args
671-
elif len(unknown_args) == 1:
672-
logger.warning(
673-
"\nWARN: Incomplete commit command: received -- separator without any following git arguments\n"
674-
)
675-
extra_args = " ".join(unknown_args[1:])
676-
arguments["extra_cli_args"] = extra_args
693+
arguments["extra_cli_args"] = (
694+
_validate_unknown_args_and_parse_as_extra_cli_args(unknown_args)
695+
)
677696

678697
conf = config.read_cfg(args.config)
679698
args = cast("Args", args)

0 commit comments

Comments
 (0)