Skip to content

Commit 794874e

Browse files
koxudaxipre-commit-ci[bot]github-actions[bot]
authored
Add auto-generated CLI reference documentation from test cases (#2673)
* docs: add initial CLI documentation structure and update related files * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs: exclude CLI reference from codespell checks * docs: remove redundant code in CLI documentation coverage test * docs: add coverage markers to functions and classes in CLI-related files * docs: enhance CLI documentation generation and coverage reporting * docs: update CLI reference documentation 🤖 Generated by GitHub Actions * docs: add CLI documentation check job to GitHub Actions workflow * docs: add test for get_canonical_option and refine help text validation * docs: improve CLI argument formatting and fix test cases * docs: fix CLI usage examples for consistency and correctness * docs: enhance CLI documentation generation error reporting and add formatter kwargs * docs: update formatter kwargs path and add empty JSON file * docs: enhance error reporting in CLI documentation generation * docs: update CLI usage examples for consistency and correctness --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 2148938 commit 794874e

60 files changed

Lines changed: 19186 additions & 121 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/cli-docs.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: Update CLI Docs
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- 'tests/main/**'
8+
- 'tests/test_main_kr.py'
9+
- 'src/datamodel_code_generator/cli_options.py'
10+
- 'scripts/build_cli_docs.py'
11+
pull_request:
12+
branches: [main]
13+
paths:
14+
- 'tests/main/**'
15+
- 'tests/test_main_kr.py'
16+
- 'src/datamodel_code_generator/cli_options.py'
17+
- 'scripts/build_cli_docs.py'
18+
19+
permissions:
20+
contents: write
21+
22+
jobs:
23+
update-cli-docs:
24+
if: github.event_name == 'push' || !github.event.pull_request.head.repo.fork
25+
runs-on: ubuntu-latest
26+
steps:
27+
- uses: actions/checkout@v4
28+
with:
29+
fetch-depth: 0
30+
ref: ${{ github.head_ref || github.ref }}
31+
token: ${{ secrets.PAT }}
32+
- name: Install the latest version of uv
33+
uses: astral-sh/setup-uv@v5
34+
- name: Install tox
35+
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
36+
- name: Setup environment
37+
run: tox run -vv --notest --skip-missing-interpreters false -e cli-docs
38+
env:
39+
UV_PYTHON_PREFERENCE: "only-managed"
40+
- name: Collect CLI doc metadata
41+
run: .tox/cli-docs/bin/pytest --collect-cli-docs -p no:xdist -q
42+
- name: Build CLI docs
43+
run: .tox/cli-docs/bin/python scripts/build_cli_docs.py
44+
- name: Commit and push if changed
45+
run: |
46+
git config user.name "github-actions[bot]"
47+
git config user.email "github-actions[bot]@users.noreply.github.com"
48+
git add docs/cli-reference/
49+
git diff --staged --quiet || git commit -m "docs: update CLI reference documentation
50+
51+
🤖 Generated by GitHub Actions"
52+
git push

.github/workflows/codespell.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ jobs:
2727
uses: codespell-project/actions-codespell@v2
2828
with:
2929
exclude_file: CODE_OF_CONDUCT.md
30+
skip: ./docs/cli-reference

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ coverage.xml
55
.coverage
66
.idea/
77
.vscode/
8+
site/
9+
tests/cli_doc/.cli_doc_collection.json

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repos:
3131
- id: codespell
3232
additional_dependencies:
3333
- tomli
34-
exclude: "^tests/|^CODE_OF_CONDUCT.md"
34+
exclude: "^tests/|^CODE_OF_CONDUCT.md|^docs/cli-reference/"
3535
- repo: local
3636
hooks:
3737
- id: readme

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ This method needs the [http extra option](#http-extra-option)
329329

330330
## All Command Options
331331

332+
> 📖 **For detailed documentation with examples, see the [CLI Reference](https://koxudaxi.github.io/datamodel-code-generator/cli-reference/).**
333+
332334
The `datamodel-codegen` command:
333335

334336
<!-- start command help -->

docs/cli-reference/base-options.md

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
# Base Options
2+
3+
## Options
4+
5+
| Option | Description |
6+
|--------|-------------|
7+
| [`--encoding`](#encoding) | Specify character encoding for input and output files. |
8+
| [`--input`](#input) | Specify the input schema file path. |
9+
| [`--input-file-type`](#input-file-type) | Specify the input file type for code generation. |
10+
| [`--output`](#output) | Specify the destination path for generated Python code. |
11+
| [`--url`](#url) | Fetch schema from URL with custom HTTP headers. |
12+
13+
---
14+
15+
## `--encoding` {#encoding}
16+
17+
Specify character encoding for input and output files.
18+
19+
The `--encoding` flag sets the character encoding used when reading
20+
the schema file and writing the generated Python code. This is useful
21+
for schemas containing non-ASCII characters (e.g., Japanese, Chinese).
22+
Default is utf-8.
23+
24+
!!! tip "Usage"
25+
26+
```bash
27+
datamodel-codegen --input schema.json --encoding utf-8 # (1)!
28+
```
29+
30+
1. :material-arrow-left: `--encoding` - the option documented here
31+
32+
??? example "Input Schema"
33+
34+
```json
35+
{
36+
"$schema": "http://json-schema.org/draft-07/schema#",
37+
"title": "日本語Model",
38+
"description": "モデルの説明文",
39+
"type": "object",
40+
"properties": {
41+
"名前": {
42+
"type": "string",
43+
"description": "ユーザー名"
44+
},
45+
"年齢": {
46+
"type": "integer"
47+
}
48+
}
49+
}
50+
```
51+
52+
??? example "Output"
53+
54+
```python
55+
# generated by datamodel-codegen:
56+
# filename: encoding_test.json
57+
# timestamp: 2019-07-26T00:00:00+00:00
58+
59+
from __future__ import annotations
60+
61+
from typing import Optional
62+
63+
from pydantic import BaseModel, Field
64+
65+
66+
class 日本語Model(BaseModel):
67+
名前: Optional[str] = Field(None, description='ユーザー名')
68+
年齢: Optional[int] = None
69+
```
70+
71+
---
72+
73+
## `--input` {#input}
74+
75+
Specify the input schema file path.
76+
77+
The `--input` flag specifies the path to the schema file (JSON Schema,
78+
OpenAPI, GraphQL, etc.). Multiple input files can be specified to merge
79+
schemas. Required unless using `--url` to fetch schema from a URL.
80+
81+
!!! tip "Usage"
82+
83+
```bash
84+
datamodel-codegen --input schema.json --input pet_simple.json --output output.py # (1)!
85+
```
86+
87+
1. :material-arrow-left: `--input` - the option documented here
88+
89+
??? example "Input Schema"
90+
91+
```json
92+
{
93+
"$schema": "http://json-schema.org/draft-07/schema#",
94+
"title": "Pet",
95+
"type": "object",
96+
"properties": {
97+
"id": {
98+
"type": "integer"
99+
},
100+
"name": {
101+
"type": "string"
102+
},
103+
"tag": {
104+
"type": "string"
105+
}
106+
}
107+
}
108+
```
109+
110+
??? example "Output"
111+
112+
```python
113+
# generated by datamodel-codegen:
114+
# filename: pet_simple.json
115+
# timestamp: 2019-07-26T00:00:00+00:00
116+
117+
from __future__ import annotations
118+
119+
from typing import Optional
120+
121+
from pydantic import BaseModel
122+
123+
124+
class Pet(BaseModel):
125+
id: Optional[int] = None
126+
name: Optional[str] = None
127+
tag: Optional[str] = None
128+
```
129+
130+
---
131+
132+
## `--input-file-type` {#input-file-type}
133+
134+
Specify the input file type for code generation.
135+
136+
The `--input-file-type` flag explicitly sets the input format when it cannot
137+
be auto-detected from the file extension. Supported types: openapi, jsonschema,
138+
json, yaml, csv, graphql.
139+
140+
!!! tip "Usage"
141+
142+
```bash
143+
datamodel-codegen --input schema.json --input-file-type json # (1)!
144+
```
145+
146+
1. :material-arrow-left: `--input-file-type` - the option documented here
147+
148+
??? example "Input Schema"
149+
150+
```json
151+
{
152+
"Pet": {
153+
"name": "dog",
154+
"age": 2
155+
}
156+
}
157+
```
158+
159+
??? example "Output"
160+
161+
```python
162+
# generated by datamodel-codegen:
163+
# filename: pet.json
164+
# timestamp: 2019-07-26T00:00:00+00:00
165+
166+
from __future__ import annotations
167+
168+
from pydantic import BaseModel
169+
170+
171+
class Pet(BaseModel):
172+
name: str
173+
age: int
174+
175+
176+
class Model(BaseModel):
177+
Pet: Pet
178+
```
179+
180+
---
181+
182+
## `--output` {#output}
183+
184+
Specify the destination path for generated Python code.
185+
186+
The `--output` flag specifies where to write the generated Python code.
187+
It can be either a file path (single-file output) or a directory path
188+
(multi-file output for modular schemas). If omitted, the generated code
189+
is written to stdout.
190+
191+
!!! tip "Usage"
192+
193+
```bash
194+
datamodel-codegen --input schema.json --input pet_simple.json --output output.py # (1)!
195+
```
196+
197+
1. :material-arrow-left: `--output` - the option documented here
198+
199+
??? example "Input Schema"
200+
201+
```json
202+
{
203+
"$schema": "http://json-schema.org/draft-07/schema#",
204+
"title": "Pet",
205+
"type": "object",
206+
"properties": {
207+
"id": {
208+
"type": "integer"
209+
},
210+
"name": {
211+
"type": "string"
212+
},
213+
"tag": {
214+
"type": "string"
215+
}
216+
}
217+
}
218+
```
219+
220+
??? example "Output"
221+
222+
```python
223+
# generated by datamodel-codegen:
224+
# filename: pet_simple.json
225+
# timestamp: 2019-07-26T00:00:00+00:00
226+
227+
from __future__ import annotations
228+
229+
from typing import Optional
230+
231+
from pydantic import BaseModel
232+
233+
234+
class Pet(BaseModel):
235+
id: Optional[int] = None
236+
name: Optional[str] = None
237+
tag: Optional[str] = None
238+
```
239+
240+
---
241+
242+
## `--url` {#url}
243+
244+
Fetch schema from URL with custom HTTP headers.
245+
246+
The `--url` flag specifies a remote URL to fetch the schema from instead of
247+
a local file. The `--http-headers` flag adds custom HTTP headers to the request,
248+
useful for authentication (e.g., Bearer tokens) or custom API requirements.
249+
Format: `HeaderName:HeaderValue`.
250+
251+
!!! tip "Usage"
252+
253+
```bash
254+
datamodel-codegen --input schema.json --url https://api.example.com/schema.json --http-headers "Authorization:Bearer token" # (1)!
255+
```
256+
257+
1. :material-arrow-left: `--url` - the option documented here
258+
259+
??? example "Input Schema"
260+
261+
```json
262+
{
263+
"$schema": "http://json-schema.org/draft-07/schema#",
264+
"title": "Pet",
265+
"type": "object",
266+
"properties": {
267+
"id": {
268+
"type": "integer"
269+
},
270+
"name": {
271+
"type": "string"
272+
},
273+
"tag": {
274+
"type": "string"
275+
}
276+
}
277+
}
278+
```
279+
280+
??? example "Output"
281+
282+
```python
283+
# generated by datamodel-codegen:
284+
# filename: https://api.example.com/schema.json
285+
# timestamp: 2019-07-26T00:00:00+00:00
286+
287+
from __future__ import annotations
288+
289+
from typing import Optional
290+
291+
from pydantic import BaseModel
292+
293+
294+
class Pet(BaseModel):
295+
id: Optional[int] = None
296+
name: Optional[str] = None
297+
tag: Optional[str] = None
298+
```
299+
300+
---
301+

0 commit comments

Comments
 (0)