跳转到主要内容
在脚本与自动化流程中使用 Cursor CLI,执行代码分析、生成与重构等任务。

工作原理

在非交互式脚本和自动化场景中使用 print 模式-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 标志会让 agent 在不需确认的情况下直接修改文件

设置

完整的设置说明请查看 InstallationAuthentication
# 安装 Cursor CLI
curl https://cursor.com/install -fsS | bash

# 为脚本设置 API 密钥  
export CURSOR_API_KEY=your_api_key_here
cursor-agent -p "分析这段代码"

示例脚本

根据不同的脚本需求选择相应的输出格式。详见 输出格式

搜索代码库

使用 --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