@@ -612,6 +612,43 @@ def exit_code_from_str_or_skip(s: str) -> ExitCode | None:
612612 ]
613613
614614
615+ def _validate_unknown_args_and_parse_as_extra_cli_args (unknown_args : list [str ]) -> str :
616+ """Validate unknown arguments received from the command line and parse them as extra cli args.
617+
618+ The format of the extra cli args should be like this:
619+ ```
620+ cz commit -- -s -n
621+ ```
622+
623+ Raises:
624+ InvalidCommandArgumentError: if unknown arguments are found without -- separation
625+ InvalidCommandArgumentError: if unknown arguments are found before -- separator
626+ Warning: if -- separator is found without any following git arguments
627+ """
628+
629+ # Raise error for extra-args without -- separation
630+ if "--" not in unknown_args :
631+ raise InvalidCommandArgumentError (
632+ f"Invalid commitizen arguments were found: `{ ' ' .join (unknown_args )} `. "
633+ "Please use -- separator for extra git args"
634+ )
635+
636+ # Raise error for extra-args before --
637+ if unknown_args [0 ] != "--" :
638+ pos = unknown_args .index ("--" )
639+ raise InvalidCommandArgumentError (
640+ f"Invalid commitizen arguments were found before -- separator: `{ ' ' .join (unknown_args [:pos ])} `. "
641+ )
642+
643+ # Log warning for -- without any extra args
644+ if len (unknown_args ) == 1 :
645+ logger .warning (
646+ "\n WARN: Incomplete commit command: received -- separator without any following git arguments\n "
647+ )
648+
649+ return " " .join (unknown_args [1 :])
650+
651+
615652if TYPE_CHECKING :
616653
617654 class Args (argparse .Namespace ):
@@ -655,25 +692,9 @@ def main() -> None:
655692
656693 arguments = vars (args )
657694 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- "\n WARN: 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
695+ arguments ["extra_cli_args" ] = (
696+ _validate_unknown_args_and_parse_as_extra_cli_args (unknown_args )
697+ )
677698
678699 conf = config .read_cfg (args .config )
679700 args = cast ("Args" , args )
0 commit comments