コード解析、生成、修正を自動化するCursor CLIスクリプトの書き方を学ぼう
-p, --print
)を使用してください。
--print
と--force
を組み合わせて使用します:
# プリントモードでファイル変更を有効にする
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
フラグを使用すると、エージェントは確認なしで直接ファイルを変更できるようになります# Cursor CLIをインストール
curl https://cursor.com/install -fsS | bash
# スクリプト用のAPIキーを設定
export CURSOR_API_KEY=your_api_key_here
cursor-agent -p "Analyze this code"
--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}秒)"
echo "📊 最終統計: $tool_countツール、${#accumulated_text}文字生成"
;;
esac
done
このページは役に立ちましたか?