Skip to content

Commit 4c7d76a

Browse files
committed
feat: add CLI
1 parent ee7f9e3 commit 4c7d76a

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

README.md

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,29 @@
22

33
This is a Python script that generates API documentation from an OpenAPI specification file.
44

5-
## Requirements
5+
## Usage
66

7-
- Python 3.x
8-
- json
9-
- PyYAML
10-
- openapi-core
11-
- Jinja2
7+
`pip install openapi-markdown`
128

13-
## Development
9+
### CLI
1410

15-
`pip install -r requirement.txt`
11+
You can use `openapi2markdown` command as follows:
1612

17-
install a project in editble mode
18-
`pip install -e ./`
13+
```
14+
% openapi2markdown --help
15+
Usage: openapi2markdown [OPTIONS] INPUT_FILE [OUTPUT_FILE]
1916
20-
run tests
17+
Convert OpenAPI spec to Markdown documentation.
2118
22-
`python -m unittest`
19+
INPUT_FILE: Path to OpenAPI specification file (JSON or YAML) OUTPUT_FILE:
20+
Path where markdown file will be generated (optional, defaults to INPUT_FILE
21+
with .md extension)
2322
24-
## Usage
23+
Options:
24+
-t, --templates-dir DIRECTORY Custom templates directory path
25+
```
26+
27+
### Library
2528

2629
```python
2730
from openapi_markdown.generator import to_markdown
@@ -35,8 +38,26 @@ to_markdown(apiFile, outputFile)
3538
- If you want to use your own template, creates 'templates' directory and put [`api_doc_template.md.j2`](src/openapi_markdown/templates/api_doc_template.md.j2) file in it.
3639
- You can change templates directory by passing it as the 3rd argument of `to_markdown`.
3740

41+
## Development
42+
43+
### Requirements
44+
45+
- Python 3.x
46+
- json
47+
- PyYAML
48+
- openapi-core
49+
- Jinja2
50+
51+
`pip install -r requirement.txt`
52+
53+
install a project in editble mode
54+
`pip install -e ./`
55+
56+
run tests
57+
58+
`python -m unittest`
3859

39-
## Deploy
60+
### Deploy
4061

4162
```
4263
python3 -m pip install --upgrade twine

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ files = ["requirements.txt"]
2626
[project.urls]
2727
"Homepage" = "https://github.com/vrerv/openapi-markdown"
2828
"Bug Tracker" = "https://github.com/vrerv/openapi-markdown/issues"
29+
30+
[project.scripts]
31+
openapi2markdown = "openapi_markdown.bin.cli:main"

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PyYAML==6.0.2
22
openapi-core==0.19.*
3-
Jinja2==3.1.4
3+
Jinja2==3.1.*
4+
click==8.1.*
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Empty file is fine, just needs to exist

src/openapi_markdown/bin/cli.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#! /usr/bin/env python
2+
3+
import click
4+
from openapi_markdown.generator import to_markdown
5+
import os
6+
7+
@click.command()
8+
@click.argument('input_file', type=click.Path(exists=True))
9+
@click.argument('output_file', type=click.Path(), required=False)
10+
@click.option('--templates-dir', '-t',
11+
type=click.Path(exists=True, file_okay=False, dir_okay=True),
12+
help='Custom templates directory path')
13+
def main(input_file, output_file, templates_dir):
14+
"""Convert OpenAPI spec to Markdown documentation.
15+
16+
INPUT_FILE: Path to OpenAPI specification file (JSON or YAML)
17+
OUTPUT_FILE: Path where markdown file will be generated (optional, defaults to INPUT_FILE with .md extension)
18+
"""
19+
if output_file is None:
20+
output_file = os.path.splitext(input_file)[0] + '.md'
21+
22+
try:
23+
# Use default templates if templates_dir is not provided
24+
if templates_dir is None:
25+
to_markdown(input_file, output_file)
26+
else:
27+
to_markdown(input_file, output_file, templates_dir)
28+
click.echo(f"Successfully generated markdown documentation at {output_file}")
29+
except Exception as e:
30+
raise e
31+
32+
if __name__ == '__main__':
33+
main()

0 commit comments

Comments
 (0)