Skip to content

Commit 92d028d

Browse files
Removed click dependency, since the built-in argparse module is sufficient (#22)
Also added tests for CLI argument parsing
1 parent e25300b commit 92d028d

File tree

4 files changed

+72
-25
lines changed

4 files changed

+72
-25
lines changed

.github/workflows/cicd.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
python -m pip install -U pip wheel
7474
- name: Build dist
7575
run: |
76-
python setup.py sdist bdist_wheel
76+
python setup.py sdist bdist_wheel bdist_egg
7777
- name: Publish package
7878
uses: pypa/[email protected]
7979
with:

pydantic2ts/cli/script.py

+45-21
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1+
import argparse
12
import importlib
23
import inspect
34
import json
45
import logging
56
import os
67
import shutil
78
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
910
from tempfile import mkdtemp
1011
from types import ModuleType
11-
from typing import Type, Dict, Any, List, Tuple
12+
from typing import Any, Dict, List, Tuple, Type
1213
from uuid import uuid4
1314

14-
import click
1515
from pydantic import BaseModel, Extra, create_model
1616

1717
try:
@@ -217,28 +217,52 @@ def generate_typescript_defs(
217217
logger.info(f"Saved typescript definitions to {output}.")
218218

219219

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:
237255
"""
238256
CLI entrypoint to run :func:`generate_typescript_defs`
239257
"""
240258
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+
)
242266

243267

244268
if __name__ == "__main__":

setup.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ def readme():
2020
]
2121

2222
install_requires = [
23-
"click",
2423
"pydantic",
2524
]
2625

2726
setup(
2827
name="pydantic-to-typescript",
29-
version="1.0.8",
28+
version="1.0.9",
3029
description="Convert pydantic models to typescript interfaces",
3130
license="MIT",
3231
long_description=readme(),

tests/test_script.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import os
2-
import sys
32
import subprocess
3+
import sys
4+
45
import pytest
6+
57
from pydantic2ts import generate_typescript_defs
8+
from pydantic2ts.cli.script import parse_cli_args
69

710

811
def _results_directory() -> str:
@@ -114,3 +117,24 @@ def test_error_if_invalid_module_path(tmpdir):
114117
generate_typescript_defs(
115118
"fake_module", tmpdir.join(f"fake_module_output.ts").strpath
116119
)
120+
121+
122+
def test_parse_cli_args():
123+
args_basic = parse_cli_args(["--module", "my_module.py", "--output", "myOutput.ts"])
124+
assert args_basic.module == "my_module.py"
125+
assert args_basic.output == "myOutput.ts"
126+
assert args_basic.exclude == []
127+
assert args_basic.json2ts_cmd == "json2ts"
128+
args_with_excludes = parse_cli_args(
129+
[
130+
"--module",
131+
"my_module.py",
132+
"--output",
133+
"myOutput.ts",
134+
"--exclude",
135+
"Foo",
136+
"--exclude",
137+
"Bar",
138+
]
139+
)
140+
assert args_with_excludes.exclude == ["Foo", "Bar"]

0 commit comments

Comments
 (0)