メインコンテンツへスキップ
コード解析・生成・リファクタリングのタスク向けに、スクリプトや自動化ワークフローで Cursor CLI を使おう。

仕組み

非対話型のスクリプトや自動化には print mode (-p, --print) を使おう。

スクリプトでのファイル変更

スクリプト内でファイルを変更するなら、--print--force を組み合わせよう:
# print モードでファイルの変更を有効化
cursor-agent -p --force "このコードをモダンな ES6+ 構文にリファクタリングして"

# --force を付けない場合、変更は提案のみで適用されない
cursor-agent -p "このファイルに JSDoc コメントを追加して"  # ファイルは変更されない

# 実際にファイルを書き換えるバッチ処理
find src/ -name "*.js" | while read file; do
  cursor-agent -p --force "$file に網羅的な JSDoc コメントを追加して"
done
--force フラグを使うと、エージェントが確認なしでファイルを直接変更できる

セットアップ

詳細なセットアップ手順は、InstallationAuthentication を参照してね。
# Cursor CLI をインストール
curl https://cursor.com/install -fsS | bash

# スクリプト用のAPIキーを設定
export CURSOR_API_KEY=your_api_key_here
cursor-agent -p "このコードを分析して"

サンプルスクリプト

スクリプトの用途に合わせて出力形式を使い分けよう。詳しくは Output format を参照してね。

コードベースを検索する

読みやすい出力には --output-format text を使おう:
#!/bin/bash
# コードベースに関する簡単な質問

cursor-agent -p --output-format text "このコードベースは何をするの?"

自動コードレビュー

構造化された分析には --output-format json を使おう:
#!/bin/bash
# simple-code-review.sh - 基本的なコードレビュー用スクリプト

echo "コードレビューを開始..."

# 直近の変更をレビュー
cursor-agent -p --force --output-format text \
  "直近のコード変更をレビューして、次の点についてフィードバックして:
  - コード品質と可読性
  - バグや問題の可能性
  - セキュリティ面での考慮事項
  - ベストプラクティスの遵守状況

  改善のための具体的な提案をして、review.txt に書き込んで"

if [ $? -eq 0 ]; then
  echo "✅ コードレビューが正常に完了"
else
  echo "❌ コードレビューに失敗"
  exit 1
fi

リアルタイム進捗のトラッキング

リアルタイムで進捗を追跡するには、--output-format stream-json を使ってね:
#!/bin/bash
# stream-progress.sh - リアルタイムで進捗を追跡

echo "🚀 ストリーム処理を開始..."

# リアルタイムで進捗を追跡
accumulated_text=""
tool_count=0
start_time=$(date +%s)

cursor-agent -p --force --output-format stream-json \
  "このプロジェクトの構成を分析し、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 "🤖 使用モデル: $model"
        fi
        ;;
        
      "assistant")
        # ストリーミング中のテキスト差分を蓄積
        content=$(echo "$line" | jq -r '.message.content[0].text // empty')
        accumulated_text="$accumulated_text$content"
        
        # 進捗をライブ表示
        printf "\r📝 生成中: %d 文字" ${#accumulated_text}
        ;;
        
      "tool_call")
        if [ "$subtype" = "started" ]; then
          tool_count=$((tool_count + 1))
          
          # ツール情報を抽出
          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_count: $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_count: $path を読み取り"
          fi
          
        elif [ "$subtype" = "completed" ]; then
          # ツールの結果を抽出して表示
          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 行を作成($size バイト)"
          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 行を読み取り"
          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🎯 完了: ${duration}ms(合計 ${total_time}s)"
        echo "📊 最終統計: ツール $tool_count 個、生成文字数 ${#accumulated_text}"
        ;;
    esac
  done
I