Menggunakan Headless CLI
Gunakan Cursor CLI dalam skrip dan alur kerja otomatisasi untuk analisis, pembuatan, dan pemfaktoran ulang kode.
Cara kerjanya
Gunakan mode cetak (-p, --print) untuk skrip dan otomatisasi noninteraktif.
Memodifikasi file dalam skrip
Gabungkan --print dengan --force (atau --yolo) untuk memodifikasi file melalui skrip:
# Aktifkan perubahan file dalam mode cetakagent -p --force "Refactor this code to use modern ES6+ syntax"# Tanpa --force, perubahan hanya diusulkan, tidak diterapkanagent -p "Add JSDoc comments to this file" # Tidak akan mengubah file# Pemrosesan batch dengan perubahan filefind src/ -name "*.js" | while read file; do agent -p --force "Add comprehensive JSDoc comments to $file"doneFlag --force memungkinkan agent mengubah file secara langsung tanpa
konfirmasi
Konfigurasi
Lihat Instalasi dan Autentikasi untuk informasi konfigurasi selengkapnya.
# Instal Cursor CLI (macOS, Linux, WSL)curl https://cursor.com/install -fsS | bash# Instal Cursor CLI (Windows PowerShell)irm 'https://cursor.com/install?win32=true' | iex# Atur kunci API untuk skripexport CURSOR_API_KEY=your_api_key_hereagent -p "Analyze this code"Contoh skrip
Gunakan format output yang berbeda sesuai kebutuhan skrip. Lihat Format output untuk detailnya.
Mencari dalam codebase
Secara default, --print menggunakan format text untuk menghasilkan respons bersih yang hanya berisi jawaban akhir:
#!/bin/bash# Pertanyaan sederhana tentang codebase - menggunakan format text secara defaultagent -p "What does this codebase do?"Code review otomatis
Gunakan --output-format json untuk analisis terstruktur:
#!/bin/bash# simple-code-review.sh - Skrip code review sederhanaecho "Starting code review..."# Tinjau perubahan terbaruagent -p --force --output-format text \ "Review the recent code changes and provide feedback on: - Code quality and readability - Potential bugs or issues - Security considerations - Best practices compliance Provide specific suggestions for improvement and write to review.txt"if [ $? -eq 0 ]; then echo "✅ Code review completed successfully"else echo "❌ Code review failed" exit 1fiMelacak kemajuan secara real-time
Gunakan --output-format stream-json untuk melacak kemajuan per pesan, atau tambahkan --stream-partial-output untuk melakukan streaming delta secara bertahap:
#!/bin/bash# stream-progress.sh - Lacak progres secara real-timeecho "🚀 Starting stream processing..."# Lacak progres secara real-timeaccumulated_text=""tool_count=0start_time=$(date +%s)agent -p --force --output-format stream-json --stream-partial-output \ "Analyze this project structure and create a summary report in analysis.txt" | \ while IFS= read -r line; do type=$(echo "$line" | jq -r '.type // empty') subtype=$(echo "$line" | jq -r '.subtype // empty') case "$type" in "system") if [ "$subtype" = "init" ]; then model=$(echo "$line" | jq -r '.model // "unknown"') echo "🤖 Using model: $model" fi ;; "assistant") # Hanya proses delta streaming (timestamp_ms tersedia, tanpa model_call_id). # Lewati flush yang di-buffer sebelum pemanggilan tool dan di akhir giliran. has_ts=$(echo "$line" | jq 'has("timestamp_ms")') has_mc=$(echo "$line" | jq 'has("model_call_id")') if [ "$has_ts" = "true" ] && [ "$has_mc" = "false" ]; then content=$(echo "$line" | jq -r '.message.content[0].text // empty') accumulated_text="$accumulated_text$content" printf "\r📝 Generating: %d chars" ${#accumulated_text} fi ;; "tool_call") if [ "$subtype" = "started" ]; then tool_count=$((tool_count + 1)) # Ambil informasi tool if echo "$line" | jq -e '.tool_call.writeToolCall' > /dev/null 2>&1; then path=$(echo "$line" | jq -r '.tool_call.writeToolCall.args.path // "unknown"') echo -e "\n🔧 Tool #$tool_count: Creating $path" elif echo "$line" | jq -e '.tool_call.readToolCall' > /dev/null 2>&1; then path=$(echo "$line" | jq -r '.tool_call.readToolCall.args.path // "unknown"') echo -e "\n📖 Tool #$tool_count: Reading $path" fi elif [ "$subtype" = "completed" ]; then # Ambil dan tampilkan hasil tool if echo "$line" | jq -e '.tool_call.writeToolCall.result.success' > /dev/null 2>&1; then lines=$(echo "$line" | jq -r '.tool_call.writeToolCall.result.success.linesCreated // 0') size=$(echo "$line" | jq -r '.tool_call.writeToolCall.result.success.fileSize // 0') echo " ✅ Created $lines lines ($size bytes)" elif echo "$line" | jq -e '.tool_call.readToolCall.result.success' > /dev/null 2>&1; then lines=$(echo "$line" | jq -r '.tool_call.readToolCall.result.success.totalLines // 0') echo " ✅ Read $lines lines" fi fi ;; "result") duration=$(echo "$line" | jq -r '.duration_ms // 0') end_time=$(date +%s) total_time=$((end_time - start_time)) echo -e "\n\n🎯 Completed in ${duration}ms (${total_time}s total)" echo "📊 Final stats: $tool_count tools, ${#accumulated_text} chars generated" ;; esac doneBekerja dengan gambar
Untuk mengirim gambar, file media, atau data biner lainnya kepada agent, sertakan path file dalam prompt Anda. Agent dapat membaca file apa pun melalui pemanggilan tool, termasuk gambar, video, dan format lainnya.
Menyertakan path file dalam prompt
Cukup sebutkan path file dalam teks prompt Anda. Agent akan otomatis membaca file saat diperlukan:
# Analisis gambaragent -p "Analyze this image and describe what you see: ./screenshot.png"# Memproses beberapa file mediaagent -p "Compare these two images and identify differences: ./before.png ./after.png"# Menggabungkan path file dengan instruksi teksagent -p "Review the code in src/app.ts and the design mockup in designs/homepage.png. Suggest improvements to match the design."Cara kerjanya
Saat menyertakan path file dalam prompt:
- Agent menerima prompt Anda dengan referensi path file
- Agent menggunakan pemanggilan tool untuk membaca file secara otomatis
- Gambar diproses secara transparan
- Anda dapat mereferensikan file menggunakan path relatif atau absolut
Contoh: Skrip analisis gambar
#!/bin/bash# analyze-image.sh - Menganalisis gambar menggunakan Headless CLIIMAGE_PATH="./screenshots/ui-mockup.png"agent -p --output-format json \ "Analyze this image and provide a detailed description: $IMAGE_PATH" | \ jq -r '.result'Contoh: Pemrosesan media batch
#!/bin/bash# process-media.sh - Memproses beberapa file mediafor image in images/*.png; do echo "Processing $image..." agent -p --output-format text \ "Describe what's in this image: $image" > "${image%.png}.description.txt"donePath file dapat bersifat relatif terhadap direktori kerja saat ini atau berupa path absolut. Agent akan membaca file melalui pemanggilan tool, jadi pastikan file tersebut ada dan dapat diakses dari lokasi tempat Anda menjalankan perintah.