|
21 | 21 | NoCommandFoundError, |
22 | 22 | ) |
23 | 23 |
|
24 | | -logger = logging.getLogger(__name__) |
25 | | - |
26 | 24 |
|
27 | 25 | class ParseKwargs(argparse.Action): |
28 | 26 | """ |
@@ -612,6 +610,43 @@ def exit_code_from_str_or_skip(s: str) -> ExitCode | None: |
612 | 610 | ] |
613 | 611 |
|
614 | 612 |
|
| 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 | + |
615 | 650 | if TYPE_CHECKING: |
616 | 651 |
|
617 | 652 | class Args(argparse.Namespace): |
@@ -655,25 +690,9 @@ def main() -> None: |
655 | 690 |
|
656 | 691 | arguments = vars(args) |
657 | 692 | 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 | + ) |
677 | 696 |
|
678 | 697 | conf = config.read_cfg(args.config) |
679 | 698 | args = cast("Args", args) |
|
0 commit comments