Skip to content

Commit 6dd9c28

Browse files
koxudaxigithub-actions[bot]pre-commit-ci[bot]
authored
Add --generate-cli-command option to generate CLI command from pyproject.toml (#2583)
* Add --generate-cli-command option to generate CLI from pyproject.toml * docs: update command help in README 🤖 Generated by GitHub Actions * Improve typing * Add unitest for _format_cli_value * Add unitest for _format_cli_value * update unitest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * update unitest --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2ec6e2c commit 6dd9c28

18 files changed

Lines changed: 305 additions & 0 deletions

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,8 @@ General options:
538538
--debug show debug message (require "debug". `$ pip install ''datamodel-code-
539539
generator[debug]''`)
540540
--disable-warnings disable warnings
541+
--generate-cli-command
542+
Generate CLI command from pyproject.toml configuration and exit
541543
--generate-pyproject-config
542544
Generate pyproject.toml configuration from the provided CLI
543545
arguments and exit

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,8 @@ General options:
530530
--debug show debug message (require "debug". `$ pip install ''datamodel-code-
531531
generator[debug]''`)
532532
--disable-warnings disable warnings
533+
--generate-cli-command
534+
Generate CLI command from pyproject.toml configuration and exit
533535
--generate-pyproject-config
534536
Generate pyproject.toml configuration from the provided CLI
535537
arguments and exit

src/datamodel_code_generator/__main__.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,18 @@
6060
# Options that should be excluded from pyproject.toml config generation
6161
EXCLUDED_CONFIG_OPTIONS: frozenset[str] = frozenset({
6262
"generate_pyproject_config",
63+
"generate_cli_command",
6364
"version",
6465
"help",
6566
"debug",
6667
"no_color",
6768
"disable_warnings",
6869
})
6970

71+
BOOLEAN_OPTIONAL_OPTIONS: frozenset[str] = frozenset({
72+
"use_specialized_enum",
73+
})
74+
7075

7176
class Exit(IntEnum):
7277
"""Exit reasons."""
@@ -487,6 +492,36 @@ def generate_pyproject_config(args: Namespace) -> str:
487492
return "\n".join(lines) + "\n"
488493

489494

495+
def _format_cli_value(value: str | list[str]) -> str:
496+
"""Format a value for CLI argument."""
497+
if isinstance(value, list):
498+
return " ".join(f'"{v}"' if " " in v else v for v in value)
499+
return f'"{value}"' if " " in value else value
500+
501+
502+
def generate_cli_command(config: dict[str, TomlValue]) -> str:
503+
"""Generate CLI command from pyproject.toml configuration."""
504+
parts: list[str] = ["datamodel-codegen"]
505+
506+
for key, value in sorted(config.items()):
507+
if key in EXCLUDED_CONFIG_OPTIONS:
508+
continue
509+
510+
cli_key = key.replace("_", "-")
511+
512+
if isinstance(value, bool):
513+
if value:
514+
parts.append(f"--{cli_key}")
515+
elif key in BOOLEAN_OPTIONAL_OPTIONS:
516+
parts.append(f"--no-{cli_key}")
517+
elif isinstance(value, list):
518+
parts.extend((f"--{cli_key}", _format_cli_value(cast("list[str]", value))))
519+
else:
520+
parts.extend((f"--{cli_key}", _format_cli_value(str(value))))
521+
522+
return " ".join(parts) + "\n"
523+
524+
490525
def main(args: Sequence[str] | None = None) -> Exit: # noqa: PLR0911, PLR0912, PLR0915
491526
"""Execute datamodel code generation from command-line arguments."""
492527
# add cli completion support
@@ -510,6 +545,17 @@ def main(args: Sequence[str] | None = None) -> Exit: # noqa: PLR0911, PLR0912,
510545

511546
pyproject_config = _get_pyproject_toml_config(Path.cwd())
512547

548+
if namespace.generate_cli_command:
549+
if not pyproject_config:
550+
print( # noqa: T201
551+
"No [tool.datamodel-codegen] section found in pyproject.toml",
552+
file=sys.stderr,
553+
)
554+
return Exit.ERROR
555+
command_output = generate_cli_command(pyproject_config)
556+
print(command_output) # noqa: T201
557+
return Exit.OK
558+
513559
try:
514560
config = Config.parse_obj(pyproject_config)
515561
config.merge_args(namespace)

src/datamodel_code_generator/arguments.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,12 @@ def start_section(self, heading: str | None) -> None:
664664
default=None,
665665
help="Generate pyproject.toml configuration from the provided CLI arguments and exit",
666666
)
667+
general_options.add_argument(
668+
"--generate-cli-command",
669+
action="store_true",
670+
default=None,
671+
help="Generate CLI command from pyproject.toml configuration and exit",
672+
)
667673
general_options.add_argument(
668674
"--version",
669675
action="store_true",
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
datamodel-codegen --input schema.yaml
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
datamodel-codegen --input schema.yaml --strict-types str int
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
datamodel-codegen --input schema.yaml --no-use-specialized-enum
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
datamodel-codegen --input schema.yaml --snake-case-field
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
datamodel-codegen --input schema.yaml --output model.py
2+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
datamodel-codegen --collapse-root-models --snake-case-field --use-annotated
2+

0 commit comments

Comments
 (0)