Skip to content

Commit

Permalink
Add output arg
Browse files Browse the repository at this point in the history
  • Loading branch information
wctsai20002 committed Jul 29, 2024
1 parent a8db624 commit d8c6bfa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,24 @@ repository2prompt [OPTIONS] INPUT_PATH
Options:
- `-t, --template PATH`: Path to a custom Jinja2 template file
- `-f, --format [markdown|json|text|split]`: Output format (default: markdown)
- `-o, --output PATH`: Output file path (default: repository_name.{format})
- `--help`: Show help message

Example:

```bash
repository2prompt https://github.com/octocat/octocat.github.io -f json
# Convert a GitHub repository to JSON format and save to a specific file
repository2prompt https://github.com/octocat/Hello-World -f json -o output.json

# Convert a local directory to markdown and save with default name
repository2prompt /path/to/local/repo -f markdown

# Use a custom template and save the output to a specific file
repository2prompt https://github.com/user/repo -t custom_template.j2 -o custom_output.md
```

If no output file is specified, the result will be saved in the current directory with the name `repository_name_prompt.{extension}`, where `{extension}` is the chosen output format.

### Python API

You can also use Repository2Prompt in your Python code:
Expand Down
28 changes: 24 additions & 4 deletions repository2prompt/cli.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
import argparse
import os
from .repository2prompt import Repository2Prompt
from .config import CONFIG

def get_file_extension(format):
extension_map = {
'markdown': 'md',
'json': 'json',
'text': 'txt',
'split': 'json'
}
return extension_map.get(format, format)

def main():
parser = argparse.ArgumentParser(description="Convert GitHub repositories or local directories into LLM prompts.")
parser.add_argument("input_path", help="GitHub repository URL or path to local directory")
parser.add_argument("-t", "--template", help="Path to custom Jinja2 template file")
parser.add_argument("-f", "--format", choices=CONFIG['supported_output_formats'],
default=CONFIG['default_output_format'],
help=f"Output format (default: {CONFIG['default_output_format']})")
parser.add_argument("-o", "--output", help="Output file path (default: repository_name.{extension})")
args = parser.parse_args()

converter = Repository2Prompt(args.input_path, args.template, args.format)
result = converter.process()

if result:
print(result)

# Determine output file name
if args.output:
output_file = args.output
else:
print("Failed to generate prompt due to errors.")
repo_name = os.path.basename(args.input_path.rstrip('/'))
extension = get_file_extension(args.format)
output_file = f"{repo_name}_prompt.{extension}"

# Write result to file
with open(output_file, 'w', encoding='utf-8') as f:
f.write(result)

print(f"Output written to: {output_file}")

if __name__ == "__main__":
main()

0 comments on commit d8c6bfa

Please sign in to comment.