Skip to content

Commit

Permalink
feat: add CLI interface
Browse files Browse the repository at this point in the history
  • Loading branch information
S1M0N38 committed Dec 28, 2024
1 parent f1e6097 commit 792d4e6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ This lib is so simple that it barely makes sense to create a standalone package

### CLI

- Convert requests stored as JSON files to markdown files
- Print request stored as JSON files to markdown representation to stdout

```
chat_completion_md --json pattern/to/*.json
chat_completion_md path/to/json/file.json
```

- Convert markdown files to chat completion requests and save them as JSON
- Print request stored as markdown files to JSON representation to stdout

```
chat_completion_md --md pattern/to/*.md
chat_completion_md path/to/md/file.md
```

### API
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Issues = "https://github.com/S1M0N38/chat-completion-md/issues"
Repository = "https://github.com/S1M0N38/chat-completion-md"
Changelog = "https://github.com/S1M0N38/chat-completion-md/blob/main/CHANGELOG.md"

[project.scripts]
chat_completion_md = "chat_completion_md.cli:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
Expand Down
34 changes: 34 additions & 0 deletions src/chat_completion_md/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sys
from pathlib import Path

from chat_completion_md import json_to_md, md_to_json


def main() -> None:
"""CLI entrypoint for chat-completion-md.
Convert between JSON and Markdown formats for chat completion requests:
- If input is a JSON file, outputs markdown to stdout
- If input is a Markdown file, outputs JSON to stdout
"""
if len(sys.argv) != 2:
print("Usage: chat_completion_md <file>", file=sys.stderr)
sys.exit(1)

file_path = Path(sys.argv[1])
if not file_path.exists():
print(f"Error: File {file_path} not found", file=sys.stderr)
sys.exit(1)

try:
content = file_path.read_text()
if file_path.suffix == ".json":
print(json_to_md(content))
elif file_path.suffix == ".md":
print(md_to_json(content))
else:
print(f"Error: Unsupported file type {file_path.suffix}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)

0 comments on commit 792d4e6

Please sign in to comment.