Gunakan Cursor CLI dalam skrip dan alur kerja otomatis untuk tugas analisis, pembuatan, dan refaktorisasi kode.

Cara kerjanya

Gunakan print mode (-p, --print) untuk scripting noninteraktif dan otomasi.

Modifikasi file di dalam skrip

Gabungkan --print dengan --force untuk memodifikasi file dalam skrip:
# Aktifkan modifikasi file dalam print mode
cursor-agent -p --force "Refactor kode ini untuk menggunakan sintaks ES6+ modern"

# Tanpa --force, perubahan hanya diusulkan, tidak diterapkan
cursor-agent -p "Tambahkan komentar JSDoc ke file ini"  # Tidak akan memodifikasi file

# Pemrosesan batch dengan perubahan file yang benar-benar diterapkan
find src/ -name "*.js" | while read file; do
  cursor-agent -p --force "Tambahkan komentar JSDoc yang komprehensif ke $file"
done
Flag --force memungkinkan agen melakukan perubahan langsung pada file tanpa konfirmasi

Penyiapan

Lihat Instalasi dan Autentikasi untuk detail penyiapan lengkap.
# Instal Cursor CLI
curl https://cursor.com/install -fsS | bash

# Atur kunci API untuk skrip
export CURSOR_API_KEY=your_api_key_here
cursor-agent -p "Analisis kode ini"

Contoh skrip

Gunakan format output yang berbeda sesuai kebutuhan skrip. Lihat Format output untuk detailnya.

Mencari di codebase

Gunakan --output-format text untuk respons yang mudah dibaca:
#!/bin/bash
# Pertanyaan sederhana tentang codebase

cursor-agent -p --output-format text "What does this codebase do?"

Tinjauan kode otomatis

Gunakan --output-format json untuk analisis terstruktur:
#!/bin/bash
# simple-code-review.sh - Skrip dasar untuk code review

echo "Starting code review..."

# Tinjau perubahan terbaru
cursor-agent -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 1
fi

Pelacakan progres real-time

Gunakan --output-format stream-json untuk pelacakan progres real-time:
#!/bin/bash
# stream-progress.sh - Lacak progres secara real-time

echo "🚀 Starting stream processing..."

# Lacak progres secara real-time
accumulated_text=""
tool_count=0
start_time=$(date +%s)

cursor-agent -p --force --output-format stream-json \
  "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")
        # Akumulasi delta teks streaming
        content=$(echo "$line" | jq -r '.message.content[0].text // empty')
        accumulated_text="$accumulated_text$content"
        
        # Tampilkan progres langsung
        printf "\r📝 Generating: %d chars" ${#accumulated_text}
        ;;
        
      "tool_call")
        if [ "$subtype" = "started" ]; then
          tool_count=$((tool_count + 1))
          
          # Ekstrak 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
          # Ekstrak 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
  done