|
| 1 | +import argparse |
1 | 2 | import importlib
|
2 | 3 | import inspect
|
3 | 4 | import json
|
4 | 5 | import logging
|
5 | 6 | import os
|
6 | 7 | import shutil
|
7 | 8 | import sys
|
8 |
| -from importlib.util import spec_from_file_location, module_from_spec |
| 9 | +from importlib.util import module_from_spec, spec_from_file_location |
9 | 10 | from tempfile import mkdtemp
|
10 | 11 | from types import ModuleType
|
11 |
| -from typing import Type, Dict, Any, List, Tuple |
| 12 | +from typing import Any, Dict, List, Tuple, Type |
12 | 13 | from uuid import uuid4
|
13 | 14 |
|
14 |
| -import click |
15 | 15 | from pydantic import BaseModel, Extra, create_model
|
16 | 16 |
|
17 | 17 | try:
|
@@ -217,28 +217,52 @@ def generate_typescript_defs(
|
217 | 217 | logger.info(f"Saved typescript definitions to {output}.")
|
218 | 218 |
|
219 | 219 |
|
220 |
| -@click.command() |
221 |
| -@click.option( |
222 |
| - "--module", |
223 |
| - help="name or filepath of the python module. Discoverable submodules will also be checked", |
224 |
| -) |
225 |
| -@click.option( |
226 |
| - "--output", help="name of the file the typescript definitions should be written to" |
227 |
| -) |
228 |
| -@click.option( |
229 |
| - "--exclude", |
230 |
| - multiple=True, |
231 |
| - help="name of a pydantic model which should be omitted from the results. This option can be defined multiple times", |
232 |
| -) |
233 |
| -@click.option("--json2ts-cmd", default="json2ts") |
234 |
| -def main( |
235 |
| - module: str, output: str, exclude: Tuple[str], json2ts_cmd: str = "json2ts" |
236 |
| -) -> None: |
| 220 | +def parse_cli_args(args: List[str] = None) -> argparse.Namespace: |
| 221 | + """ |
| 222 | + Parses the command-line arguments passed to pydantic2ts. |
| 223 | + """ |
| 224 | + parser = argparse.ArgumentParser( |
| 225 | + prog="pydantic2ts", |
| 226 | + description=main.__doc__, |
| 227 | + formatter_class=argparse.RawTextHelpFormatter, |
| 228 | + ) |
| 229 | + parser.add_argument( |
| 230 | + "--module", |
| 231 | + help="name or filepath of the python module.\n" |
| 232 | + "Discoverable submodules will also be checked.", |
| 233 | + ) |
| 234 | + parser.add_argument( |
| 235 | + "--output", |
| 236 | + help="name of the file the typescript definitions should be written to.", |
| 237 | + ) |
| 238 | + parser.add_argument( |
| 239 | + "--exclude", |
| 240 | + action="append", |
| 241 | + default=[], |
| 242 | + help="name of a pydantic model which should be omitted from the results.\n" |
| 243 | + "This option can be defined multiple times.", |
| 244 | + ) |
| 245 | + parser.add_argument( |
| 246 | + "--json2ts-cmd", |
| 247 | + dest="json2ts_cmd", |
| 248 | + default="json2ts", |
| 249 | + help="path to the json-schema-to-typescript executable.\n" "(default: json2ts)", |
| 250 | + ) |
| 251 | + return parser.parse_args(args) |
| 252 | + |
| 253 | + |
| 254 | +def main() -> None: |
237 | 255 | """
|
238 | 256 | CLI entrypoint to run :func:`generate_typescript_defs`
|
239 | 257 | """
|
240 | 258 | logging.basicConfig(level=logging.DEBUG, format="%(asctime)s %(message)s")
|
241 |
| - return generate_typescript_defs(module, output, exclude, json2ts_cmd) |
| 259 | + args = parse_cli_args() |
| 260 | + return generate_typescript_defs( |
| 261 | + args.module, |
| 262 | + args.output, |
| 263 | + tuple(args.exclude), |
| 264 | + args.json2ts_cmd, |
| 265 | + ) |
242 | 266 |
|
243 | 267 |
|
244 | 268 | if __name__ == "__main__":
|
|
0 commit comments