diff --git a/openapi_generator_cli/__init__.py b/openapi_generator_cli/__init__.py index e4519b4..bf28fff 100644 --- a/openapi_generator_cli/__init__.py +++ b/openapi_generator_cli/__init__.py @@ -56,12 +56,17 @@ def run(args: list[str] | None = None) -> subprocess.CompletedProcess[bytes]: return subprocess.run(arguments, check=False) # noqa: S603 -def cli() -> None: - """Run the OpenAPI Generator CLI with the arguments provided on the command line.""" - args = [] - if len(sys.argv) > 1: - args = sys.argv[1:] - run(args) +def cli(argv: list[str] | None = None) -> None: + """Run the OpenAPI Generator CLI with the arguments provided on the command line. + + Args: + argv (list[str], optional): + The command-line arguments to pass to the OpenAPI Generator CLI. + If not provided, sys.argv is used. + + """ + args = (argv or sys.argv)[1:] + sys.exit(run(args).returncode) if __name__ == "__main__": diff --git a/tests/test_cli.py b/tests/test_cli.py index 60c160d..b9945dd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -2,12 +2,10 @@ import os import re -from typing import TYPE_CHECKING -from openapi_generator_cli import run +import pytest -if TYPE_CHECKING: - import pytest +from openapi_generator_cli import cli, run def test_cli_version(capfd: pytest.CaptureFixture[str]) -> None: @@ -45,3 +43,16 @@ def test_invalid_arg(capfd: pytest.CaptureFixture[str]) -> None: "Found unexpected parameters: [--invalid-arg-404]" in captured.err.split(os.linesep)[0] ) + + +def test_cli_invalid_arg(capfd: pytest.CaptureFixture[str]) -> None: + with pytest.raises(SystemExit) as exc_info: + cli(["openapi-generator-cli", "--invalid-arg-404"]) + assert exc_info.value.code == 1 + + captured = capfd.readouterr() + assert not captured.out + assert ( + "Found unexpected parameters: [--invalid-arg-404]" + in captured.err.split(os.linesep)[0] + )