76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
"""
|
|
cURL to Code Converter
|
|
|
|
A CLI tool that converts cURL commands into code snippets in various programming languages.
|
|
"""
|
|
|
|
import argparse
|
|
import sys
|
|
from curl_to_code_converter.parser import CurlParser
|
|
from curl_to_code_converter.generators import CodeGenerators
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Convert cURL commands to code snippets in various languages",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog="""
|
|
Examples:
|
|
%(prog)s 'curl https://api.example.com/data'
|
|
%(prog)s 'curl -X POST https://api.example.com -d "{\"key\":\"value\"}"' -l python
|
|
%(prog)s 'curl -H "Authorization: Bearer token" https://api.example.com' -l javascript
|
|
"""
|
|
)
|
|
parser.add_argument(
|
|
"curl_command",
|
|
help="The cURL command to convert"
|
|
)
|
|
parser.add_argument(
|
|
"-l", "--language",
|
|
choices=["python", "javascript", "go", "ruby", "php", "java"],
|
|
default="python",
|
|
help="Target programming language (default: python)"
|
|
)
|
|
parser.add_argument(
|
|
"-o", "--output",
|
|
help="Output file path (optional)"
|
|
)
|
|
parser.add_argument(
|
|
"-v", "--verbose",
|
|
action="store_true",
|
|
help="Show verbose output"
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
try:
|
|
curl_parser = CurlParser(args.curl_command)
|
|
parsed_data = curl_parser.parse()
|
|
|
|
if args.verbose:
|
|
print("Parsed cURL command:")
|
|
print(f" URL: {parsed_data.url}")
|
|
print(f" Method: {parsed_data.method}")
|
|
print(f" Headers: {parsed_data.headers}")
|
|
print(f" Data: {parsed_data.data}")
|
|
print(f" Auth: {parsed_data.auth}")
|
|
print()
|
|
|
|
generators = CodeGenerators()
|
|
code = generators.generate(parsed_data, args.language)
|
|
|
|
if args.output:
|
|
with open(args.output, 'w') as f:
|
|
f.write(code)
|
|
print(f"Code written to {args.output}")
|
|
else:
|
|
print(code)
|
|
|
|
except Exception as e:
|
|
print(f"Error: {e}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|