1818
1919from datamodel_code_generator import DataModelType
2020from datamodel_code_generator .__main__ import Exit , main
21+ from datamodel_code_generator .arguments import arg_parser
2122from datamodel_code_generator .util import PYDANTIC_V2
2223from tests .conftest import (
2324 AssertFileContent ,
@@ -133,6 +134,34 @@ def _assert_exit_code(return_code: Exit, expected_exit: Exit, context: str) -> N
133134 pytest .fail (f"Expected exit code { expected_exit !r} , got { return_code !r} \n { context } " )
134135
135136
137+ def _get_valid_cli_options () -> frozenset [str ]:
138+ """Get all valid CLI option names from arg_parser."""
139+ valid_options : set [str ] = set ()
140+ for action in arg_parser ._actions :
141+ valid_options .update (action .option_strings )
142+ return frozenset (valid_options )
143+
144+
145+ _VALID_CLI_OPTIONS = _get_valid_cli_options ()
146+
147+
148+ def _validate_extra_args (extra_args : Sequence [str ] | None ) -> None :
149+ """Validate that all option-like arguments in extra_args are valid CLI options."""
150+ if extra_args is None :
151+ return
152+ invalid_args : list [str ] = [
153+ arg
154+ for arg in extra_args
155+ if (
156+ (arg .startswith ("--" ) and "=" not in arg )
157+ or (arg .startswith ("-" ) and not arg .startswith ("--" ) and len (arg ) == 2 )
158+ )
159+ and arg not in _VALID_CLI_OPTIONS
160+ ]
161+ if invalid_args : # pragma: no cover
162+ pytest .fail (f"Invalid CLI options in extra_args: { invalid_args } . Valid options: { sorted (_VALID_CLI_OPTIONS )} " )
163+
164+
136165def _extend_args (
137166 args : list [str ],
138167 * ,
@@ -148,6 +177,7 @@ def _extend_args(
148177 args .extend (["--output" , str (output_path )])
149178 if input_file_type is not None :
150179 args .extend (["--input-file-type" , input_file_type ])
180+ _validate_extra_args (extra_args )
151181 if extra_args is not None :
152182 args .extend (extra_args )
153183
0 commit comments