Verwende die Cursor-CLI in Skripten und Automations-Workflows für Codeanalyse, -generierung und Refactoring.

So funktioniert’s

Verwende den Print-Modus (-p, --print) für nicht-interaktive Skripte und Automatisierung.

Dateiänderungen in Skripten

Kombiniere --print mit --force, um Dateien in Skripten zu ändern:
# Dateiänderungen im Print-Modus aktivieren
cursor-agent -p --force "Refactor this code to use modern ES6+ syntax"

# Ohne --force werden Änderungen nur vorgeschlagen, nicht übernommen
cursor-agent -p "Add JSDoc comments to this file"  # Nimmt keine Änderungen an Dateien vor

# Batch-Verarbeitung mit tatsächlichen Dateiänderungen
find src/ -name "*.js" | while read file; do
  cursor-agent -p --force "Add comprehensive JSDoc comments to $file"
done
Der --force-Schalter erlaubt dem Agenten, direkte Dateiänderungen ohne Bestätigung vorzunehmen

Setup

Sieh dir Installation und Authentifizierung für vollständige Setup-Details an.
# Cursor CLI installieren
curl https://cursor.com/install -fsS | bash

# API-Schlüssel für Skripte setzen
export CURSOR_API_KEY=your_api_key_here
cursor-agent -p "Analyze this code"

Beispielskripte

Verwende unterschiedliche Ausgabeformate für verschiedene Skriptanforderungen. Details findest du unter Ausgabeformat.

Codebase durchsuchen

Verwende --output-format text für gut lesbare Antworten:
#!/bin/bash
# Einfache Frage zur Codebase

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

Automatisiertes Code-Review

Verwende --output-format json für strukturierte Analysen:
#!/bin/bash
# simple-code-review.sh - Einfaches Code-Review-Skript

echo "Starting code review..."

# Letzte Änderungen überprüfen
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

Fortschritt in Echtzeit verfolgen

Verwende --output-format stream-json für Fortschrittsverfolgung in Echtzeit:
#!/bin/bash
# stream-progress.sh - Fortschritt in Echtzeit verfolgen

echo "🚀 Starting stream processing..."

# Fortschritt in Echtzeit verfolgen
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 "🤖 Verwende Modell: $model"
        fi
        ;;
        
      "assistant")
        # Streaming-Text-Deltas sammeln
        content=$(echo "$line" | jq -r '.message.content[0].text // empty')
        accumulated_text="$accumulated_text$content"
        
        # Live-Fortschritt anzeigen
        printf "\r📝 Generiere: %d Zeichen" ${#accumulated_text}
        ;;
        
      "tool_call")
        if [ "$subtype" = "started" ]; then
          tool_count=$((tool_count + 1))
          
          # Tool-Informationen extrahieren
          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: Erstelle $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: Lese $path"
          fi
          
        elif [ "$subtype" = "completed" ]; then
          # Tool-Ergebnisse extrahieren und anzeigen
          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 "   ✅ $lines Zeilen erstellt ($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 "   ✅ $lines Zeilen gelesen"
          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🎯 Abgeschlossen in ${duration}ms (${total_time}s insgesamt)"
        echo "📊 Abschlussstatistik: $tool_count Tools, ${#accumulated_text} Zeichen generiert"
        ;;
    esac
  done