Initial commit: Add curl-to-code-cli project

This commit is contained in:
2026-01-30 12:39:22 +00:00
parent 33f8dc5e41
commit f4f1cb42ff

124
.curl_to_code/main.py Normal file
View File

@@ -0,0 +1,124 @@
import typer
from rich.console import Console
from rich.syntax import Syntax
from .parser import parse_curl_command
from .generators import LANGUAGE_MAP
app = typer.Typer(
name="curl-to-code",
help="Convert curl commands to code in various programming languages",
add_completion=False,
)
console = Console()
def generate_code(curl_command: str, language: str) -> str:
"""Generate code from curl command for specified language."""
parsed = parse_curl_command(curl_command)
generator = LANGUAGE_MAP.get(language, LANGUAGE_MAP["python"])
return generator(parsed)
@app.command("main")
def main(
curl_cmd: str = typer.Argument(
...,
help="The curl command to convert (wrap in quotes)",
),
language: str = typer.Option(
"python",
"--lang",
"-l",
help="Target programming language",
case_sensitive=False,
),
no_highlight: bool = typer.Option(
False,
"--no-highlight",
"-n",
help="Disable syntax highlighting",
),
) -> None:
"""Convert a curl command to code in the specified language."""
if language.lower() not in LANGUAGE_MAP:
console.print(f"[red]Error: Unknown language '{language}'[/red]")
console.print(f"Available languages: {', '.join(LANGUAGE_MAP.keys())}")
raise typer.Exit(1)
try:
code = generate_code(curl_cmd, language.lower())
if no_highlight:
console.print(code)
else:
lang_map = {
"python": "python",
"py": "python",
"javascript": "javascript",
"js": "javascript",
"go": "go",
"rust": "rust",
"php": "php",
}
syntax = Syntax(code, lang_map.get(language.lower(), "text"), theme="monokai")
console.print(syntax)
except Exception as e:
console.print(f"[red]Error parsing curl command: {e}[/red]")
raise typer.Exit(1)
@app.command("interactive")
def interactive(
language: str = typer.Option(
"python",
"--lang",
"-l",
help="Target programming language",
case_sensitive=False,
),
) -> None:
"""Enter interactive mode for converting curl commands."""
console.print("[bold cyan]curl-to-code Interactive Mode[/bold cyan]")
console.print("Paste your curl command below and press Enter.")
console.print("Type :q or Ctrl+C to exit.")
console.print("")
while True:
try:
user_input = typer.prompt("\nEnter curl command (or :q to quit)")
if user_input.strip() in (":q", ":quit", "exit"):
console.print("Goodbye!")
break
if not user_input.strip():
continue
if language.lower() not in LANGUAGE_MAP:
console.print(f"[red]Error: Unknown language '{language}'[/red]")
continue
code = generate_code(user_input, language.lower())
console.print("")
syntax = Syntax(code, language.lower(), theme="monokai")
console.print(syntax)
console.print("\n[dim]---[/dim]")
except KeyboardInterrupt:
console.print("\nGoodbye!")
break
except Exception as e:
console.print(f"[red]Error: {e}[/red]")
@app.command("languages")
def languages() -> None:
"""List supported programming languages."""
console.print("[bold]Supported Languages:[/bold]")
for lang in LANGUAGE_MAP.keys():
console.print(f" - {lang}")
if __name__ == "__main__":
app()