在脚本和自动化工作流中使用 Cursor CLI,进行代码分析、生成和重构。
工作原理
将 print 模式(-p, --print)用于非交互式脚本和自动化。
在脚本中修改文件
在脚本中将--print与--force(或--yolo)结合使用来修改文件:
# 在打印模式下启用文件修改agent -p --force "Refactor this code to use modern ES6+ syntax"# 不使用 --force 时,仅提议更改而不应用agent -p "Add JSDoc comments to this file" # 不会修改文件# 批量处理并实际修改文件find src/ -name "*.js" | while read file; do agent -p --force "Add comprehensive JSDoc comments to $file"done--force标志允许代理在无需确认的情况下直接修改文件
设置
完整的设置说明请参阅 安装 和 身份验证。
# Install Cursor CLI (macOS, Linux, WSL)curl https://cursor.com/install -fsS | bash# 安装 Cursor 命令行界面(Windows PowerShell)irm 'https://cursor.com/install?win32=true' | iex# Set API key for scriptsexport CURSOR_API_KEY=your_api_key_hereagent -p "Analyze this code"示例脚本
可根据不同脚本需求选择不同的输出格式。详情参见 输出格式。
搜索代码库
默认情况下,--print使用text格式,返回仅包含最终答案的简洁输出:
#!/bin/bash# 简单的代码库问题 - 默认使用文本格式agent -p "What does this codebase do?"自动化代码审查
使用--output-format json进行结构化分析:
#!/bin/bash# simple-code-review.sh - 基础代码审查脚本echo "开始代码审查..."# 审查最近的更改agent -p --force --output-format text \ "审查最近的代码更改并提供以下反馈: - 代码质量和可读性 - 潜在的 bug 或问题 - 安全性考虑 - 最佳实践合规性 提供具体的改进建议并写入 review.txt"if [ $? -eq 0 ]; then echo "✅ 代码审查已完成"else echo "❌ 代码审查失败" exit 1fi实时进度跟踪
使用--output-format stream-json进行消息级别进度跟踪,或添加--stream-partial-output以增量流式传输变更内容:
#!/bin/bash# stream-progress.sh - 实时跟踪进度echo "🚀 开始流式处理..."# 实时跟踪进度accumulated_text=""tool_count=0start_time=$(date +%s)agent -p --force --output-format stream-json --stream-partial-output \ "分析项目结构并在 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") # 仅处理流式增量(存在 timestamp_ms 且无 model_call_id)。 # 跳过工具调用前及轮次结束时的缓冲刷新。 has_ts=$(echo "$line" | jq 'has("timestamp_ms")') has_mc=$(echo "$line" | jq 'has("model_call_id")') if [ "$has_ts" = "true" ] && [ "$has_mc" = "false" ]; then content=$(echo "$line" | jq -r '.message.content[0].text // empty') accumulated_text="$accumulated_text$content" printf "\r📝 生成中: %d 字符" ${#accumulated_text} fi ;; "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处理图像
要向 agent 发送图像、媒体文件或其他二进制数据,请在提示词中包含文件路径。agent 可以通过工具调用读取任意文件,包括图像、视频等各种格式。
在提示中包含文件路径
只需在你的提示中引用文件路径。Agent 会在需要时自动读取这些文件:
# Analyze an imageagent -p "Analyze this image and describe what you see: ./screenshot.png"# Process multiple media filesagent -p "Compare these two images and identify differences: ./before.png ./after.png"# 结合文件路径与文本指令agent -p "查看 src/app.ts 中的代码和 designs/homepage.png 中的设计稿,提出改进建议以匹配设计。"工作原理
当你在提示中包含文件路径时:
- Agent 会接收包含这些文件路径引用的提示
- Agent 通过工具调用自动读取这些文件
- 图像会被自动处理
- 你可以使用相对路径或绝对路径来引用文件
示例:图像分析脚本
#!/bin/bash# analyze-image.sh - 使用无头 CLI 分析图像IMAGE_PATH="./screenshots/ui-mockup.png"agent -p --output-format json \ "分析此图像并提供详细说明: $IMAGE_PATH" | \ jq -r '.result'示例:批量处理媒体
#!/bin/bash# process-media.sh - 批量处理媒体文件for image in images/*.png; do echo "正在处理 $image..." agent -p --output-format text \ "描述图像内容: $image" > "${image%.png}.description.txt"done文件路径可以是相对于当前工作目录的相对路径,也可以是绝对路径。 Agent 会通过工具调用来读取文件,因此请确保这些文件存在, 并且可以在你运行命令的位置访问到它们。
《动手学PyTorch建模与应用:从深度学习到大模型》是一本从零基础上手深度学习和大模型的PyTorch实战指南。全书共11章,前6章涵盖深度学习基础,包括张量运算、神经网络原理、数据预处理及卷积神经网络等;后5章进阶探讨图像、文本、音频建模技术,并结合Transformer架构解析大语言模型的开发实践。书中通过房价预测、图像分类等案例讲解模型构建方法,每章附有动手练习题,帮助读者巩固实战能力。内容兼顾数学原理与工程实现,适配PyTorch框架最新技术发展趋势。