代码示例库
这是教程的「弹药库」——70+ 个能直接复制粘贴跑起来的示例。每条给三样东西:场景(什么时候用)、命令或 prompt(照抄就行)、说明(为什么这么写)。
命令与参数以官方 docs.claude.com 文档为准。涉及社区技巧的,标注来源。
1. 原生安装
Section titled “1. 原生安装”# macOS / Linux / WSLcurl -fsSL https://claude.ai/install.sh | bash说明:官方推荐方式,独立运行时,不依赖 Node。
2. Windows 安装
Section titled “2. Windows 安装”irm https://claude.ai/install.ps1 | iex说明:Powerhell 一行装好。
3. npm 全局安装
Section titled “3. npm 全局安装”npm install -g @anthropic-ai/claude-code说明:需要 Node.js 18+。装完用 claude doctor 体检。
4. 启动交互会话
Section titled “4. 启动交互会话”cd your-projectclaude说明:在项目根目录启动,Claude 会自动读取 CLAUDE.md 和 .claude/settings.json。
5. 一次性 headless 查询
Section titled “5. 一次性 headless 查询”claude -p "解释 src/auth.ts 里的 login 函数在做什么"说明:-p 是 print 模式,跑完即退,适合脚本和管道。结果走 stdout。
代码生成与修改
Section titled “代码生成与修改”6. 从需求生成新文件
Section titled “6. 从需求生成新文件”在 src/utils/ 下新建 format.ts,导出一个 formatPrice(cents: number) 函数,把分转成人民币字符串,如 1234 → "¥12.34",补齐单元测试。说明:给出文件名、函数签名、输入输出示例三要素,Claude 不会跑偏。
7. 按描述修改现有函数
Section titled “7. 按描述修改现有函数”@src/api/user.ts 把 getUserById 改成返回 Promise<User | null>,查不到时返回 null 而不是抛异常,同时更新调用方。说明:用 @ 引用文件,明确「改什么 + 连带改什么」。
8. 批量加注释
Section titled “8. 批量加注释”给 src/lib/ 下所有 .ts 文件加上 TSDoc 注释,描述每个导出函数的参数和返回值,不要改逻辑。说明:限定「不改逻辑」,避免顺手重构。
9. 实现接口骨架
Section titled “9. 实现接口骨架”@src/types/index.ts 根据 PaymentGateway 接口,在 src/gateways/stripe.ts 里实现一个 StripeGateway 类。说明:以接口为契约生成实现,类型对得上。
10. 生成 React 组件
Section titled “10. 生成 React 组件”在 src/components/ 下建一个 UserCard.tsx,接收 User 对象,展示头像、姓名、邮箱,用 Tailwind 做卡片样式,支持暗色模式。11. 把回调改成 async/await
Section titled “11. 把回调改成 async/await”@src/api/fetch.ts 把 readFileCallback 里用 fs.readFile 的回调风格改成 async/await,并用 try/catch 处理错误。12. 生成脚手架
Section titled “12. 生成脚手架”claude -p "在当前目录初始化一个最小 Vite + React + TypeScript 项目,装好依赖"13. 加输入校验
Section titled “13. 加输入校验”@src/routes/login.ts 给 POST /login 加上 zod 校验:email 合法、password 至少 8 位,校验失败返回 400。14. 生成 OpenAPI 客户端
Section titled “14. 生成 OpenAPI 客户端”@openapi.yaml 根据这个规范,在 src/client/ 下生成一个类型安全的 TypeScript 客户端,用 fetch。15. 多文件联动改动
Section titled “15. 多文件联动改动”把 User 类型里的 name 字段拆成 firstName 和 lastName,更新所有用到 name 的地方,包括组件、接口、测试。16. 解释报错
Section titled “16. 解释报错”claude -p "运行 npm test 报了 'Cannot find module',帮我看怎么修"17. 定位崩溃根因
Section titled “17. 定位崩溃根因”运行 npm start 后访问 /users 报 500,日志在 @logs/error.log,结合 src/routes/users.ts 定位根因并给出修复。说明:把日志文件用 @ 喂进去,比贴文本更省 token。
18. 修依赖冲突
Section titled “18. 修依赖冲突”npm install 失败,提示 peer dependency 冲突,帮我分析 package.json 并给出版本调整方案。19. 跑测试看失败
Section titled “19. 跑测试看失败”运行 npm test -- --grep auth,把失败的用例修好,不要改测试本身。说明:明确「改实现不改测试」,避免 Claude 把断言放宽了事。
20. 修复类型错误
Section titled “20. 修复类型错误”tsc 报了 12 个类型错误,逐个修复,保持原有行为不变。21. 排查无限循环
Section titled “21. 排查无限循环”@src/utils/retry.ts 这个 retry 函数在 503 时会无限重试,帮我加上最大重试次数和指数退避。22. 内存泄漏
Section titled “22. 内存泄漏”@src/server/sse.ts 怀疑这里有事件监听器没清理导致内存泄漏,帮我排查并修复。23. 死锁定位
Section titled “23. 死锁定位”@src/db/pool.ts 并发查询时偶发死锁,帮我分析事务顺序并给出加锁顺序建议。24. 解析日志找异常
Section titled “24. 解析日志找异常”claude -p "分析 logs/app.log 里过去一小时的 ERROR 和 WARN,按频次归类"25. 环境变量问题
Section titled “25. 环境变量问题”应用启动报 'DATABASE_URL is not defined',但 .env 里明明有,帮我排查 dotenv 加载顺序。26. 提取函数
Section titled “26. 提取函数”@src/api/handlers.ts 把 handleRequest 里那段解析 query string 的逻辑提取成 parseQuery 函数。27. 消除重复
Section titled “27. 消除重复”@src/services/ 三个文件里都有相似的分页逻辑,提取成一个 paginate 工具函数放到 src/utils/pagination.ts。28. 简化条件
Section titled “28. 简化条件”@src/utils/price.ts 这个 calculatePrice 里有 4 层嵌套 if,用早返回和卫语句压平。29. 重命名
Section titled “29. 重命名”把 src/ 下所有 usr 相关命名改成 user,包括变量、函数、文件名、import,保证测试通过。30. 提取类型
Section titled “30. 提取类型”@src/api/handlers.ts 把手写的响应对象结构提取成 Response<T> 类型,复用到各处。31. 拆大文件
Section titled “31. 拆大文件”@src/index.ts 这个文件有 1200 行,按职责拆成 router.ts、middleware.ts、handlers.ts 三个文件。32. 引入策略模式
Section titled “32. 引入策略模式”@src/shipping/ 把按地区算运费的 switch 改成策略模式,每个地区一个 Calculator 类。33. 函数参数对象化
Section titled “33. 函数参数对象化”@src/api/search.ts search 函数有 6 个参数,改成接收一个 SearchOptions 对象。34. 消除魔法数
Section titled “34. 消除魔法数”@src/config/ 把散落的超时、重试次数等魔法数提取成命名常量。35. 委托给子代理重构
Section titled “35. 委托给子代理重构”用 subagent 把 src/legacy/ 整个模块迁移到新 API,分文件改,每改完跑测试。36. 生成单元测试
Section titled “36. 生成单元测试”@src/utils/format.ts 给 formatPrice 写单元测试,覆盖正常、零、负数、大数四种情况,用 vitest。37. 补覆盖率
Section titled “37. 补覆盖率”claude -p "看 coverage 报告,给 src/api/ 下覆盖率低于 60% 的文件补测试"38. 写集成测试
Section titled “38. 写集成测试”@src/api/user.ts 给 GET /users/:id 写集成测试,用 supertest 起内存服务器,mock 数据库。39. 生成测试夹具
Section titled “39. 生成测试夹具”在 tests/fixtures/ 下生成一组 User 测试数据,包含正常、边界、非法三种。40. Mock 外部依赖
Section titled “40. Mock 外部依赖”@src/services/email.ts 给 sendEmail 写测试,用 mock 拦截实际发信,断言调用参数。41. TDD 流程
Section titled “41. TDD 流程”先给 src/utils/token.ts 的 generateToken 写失败测试,再实现让它通过。42. 快照测试
Section titled “42. 快照测试”@src/components/UserCard.tsx 给它加一个 vitest 快照测试,覆盖默认 props。43. 参数化测试
Section titled “43. 参数化测试”@src/utils/validate.ts 把 testValidate 里重复的 case 改成 it.each 参数化。44. 跑特定测试
Section titled “44. 跑特定测试”claude -p "跑 vitest 里所有 auth 相关测试,失败的全报上来"45. 修 flaky 测试
Section titled “45. 修 flaky 测试”tests/order.test.ts 偶发失败,怀疑有计时依赖,帮我排查并加 wait 或固定时间。Git 操作
Section titled “Git 操作”46. 生成 commit message
Section titled “46. 生成 commit message”git diff --cached | claude -p "按 Conventional Commits 写中文 commit message"47. 审查未提交改动
Section titled “47. 审查未提交改动”git diff | claude -p "审查这段改动,指出风险和遗漏的测试"48. 写 PR 描述
Section titled “48. 写 PR 描述”git log main..HEAD | claude -p "基于这些提交写一份 PR 描述,含改动摘要和测试说明"49. 解析 git log
Section titled “49. 解析 git log”git log --oneline -50 | claude -p "把最近 50 次提交归类成功能、修复、重构三类,统计占比"50. 解决冲突
Section titled “50. 解决冲突”帮我解决 rebase 时的冲突,保留两边意图,说明每处取舍。51. 拆分大提交
Section titled “51. 拆分大提交”git diff HEAD~1 | claude -p "这次提交改了三个无关功能,建议怎么拆成三次提交"52. 生成 changelog
Section titled “52. 生成 changelog”git log v1.0.0..v1.1.0 | claude -p "按 Keep a Changelog 格式生成这版变更日志"53. 查找引入 bug 的提交
Section titled “53. 查找引入 bug 的提交”git log -p -S "deprecatedFn" -- src/ | claude -p "找出哪次提交引入了 deprecatedFn 的调用"54. 写分支名
Section titled “54. 写分支名”claude -p "根据当前 staged diff 生成一个 kebab-case 分支名"55. review 一个 PR
Section titled “55. review 一个 PR”gh pr diff 123 | claude -p "审查 PR #123,重点看安全、性能、测试覆盖"56. 生成 README
Section titled “56. 生成 README”基于 package.json 和 src/ 的入口,给这个项目写一份 README,含安装、用法、脚本说明。57. API 文档
Section titled “57. API 文档”@src/api/ 扫描所有路由处理函数,生成一份 API 文档,按 REST 路径分组。58. 架构图
Section titled “58. 架构图”@src/ 梳理这个项目的模块依赖关系,用 Mermaid 画一张架构图。59. 内联文档转外链
Section titled “59. 内联文档转外链”@docs/notes.md 把这份散乱的笔记整理成结构化的 docs/guide.md,分章节加目录。60. 变更日志
Section titled “60. 变更日志”git log --oneline v2.0.0..HEAD | claude -p "生成 v2.1.0 的 changelog,分 Added/Fixed/Changed"Hooks 配置
Section titled “Hooks 配置”61. 保存前自动格式化
Section titled “61. 保存前自动格式化”{ "hooks": { "PostToolUse": [ { "matcher": "Edit|Write", "hooks": [{ "type": "command", "command": "npx prettier --write $CLAUDE_FILE_PATHS" }] } ] }}说明:PostToolUse 在工具调用后触发,matcher 用正则匹配工具名。
62. 提交前跑 lint
Section titled “62. 提交前跑 lint”{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "if echo $CLAUDE_TOOL_INPUT | grep -q 'git commit'; then npm run lint; fi" } ] } ] }}63. 阻止编辑生产配置
Section titled “63. 阻止编辑生产配置”{ "hooks": { "PreToolUse": [ { "matcher": "Edit|Write", "hooks": [ { "type": "command", "command": "echo $CLAUDE_FILE_PATHS | grep -q 'prod.env' && { echo '禁止改生产配置'; exit 2; } || exit 0" } ] } ] }}说明:exit 2 阻止工具执行,反馈给 Claude。
64. 会话开始打印信息
Section titled “64. 会话开始打印信息”{ "hooks": { "SessionStart": [ { "hooks": [{ "type": "command", "command": "echo '欢迎回来,记得先 /clear'" }] } ] }}65. 通知子代理完成
Section titled “65. 通知子代理完成”{ "hooks": { "Stop": [ { "hooks": [{ "type": "command", "command": "osascript -e 'display notification \"任务完成\" with title \"Claude Code\"'" }] } ] }}66. 生成提交后测试
Section titled “66. 生成提交后测试”{ "hooks": { "PostToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "echo $CLAUDE_TOOL_INPUT | grep -q 'git commit' && npm test || true" } ] } ] }}67. 禁止 Bash 直接 rm -rf
Section titled “67. 禁止 Bash 直接 rm -rf”{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "echo $CLAUDE_TOOL_INPUT | grep -qE 'rm -rf /' && { echo '危险删除'; exit 2; } || exit 0" } ] } ] }}68. 编辑后更新 ctags
Section titled “68. 编辑后更新 ctags”{ "hooks": { "PostToolUse": [ { "matcher": "Edit|Write", "hooks": [{ "type": "command", "command": "ctags -R src/ &" }] } ] }}69. 文件名检查
Section titled “69. 文件名检查”{ "hooks": { "PreToolUse": [ { "matcher": "Write", "hooks": [ { "type": "command", "command": "echo $CLAUDE_FILE_PATHS | grep -qE '[A-Z]' && { echo '文件名用小写连字符'; exit 2; } || exit 0" } ] } ] }}70. 记录工具调用日志
Section titled “70. 记录工具调用日志”{ "hooks": { "PostToolUse": [ { "matcher": ".*", "hooks": [{ "type": "command", "command": "echo \"$(date) $CLAUDE_TOOL_NAME\" >> .claude/tool.log" }] } ] }}MCP 集成
Section titled “MCP 集成”71. 添加 GitHub MCP
Section titled “71. 添加 GitHub MCP”claude mcp add github -- npx -y @modelcontextprotocol/server-github说明:claude mcp add <name> -- <command> 是标准格式。
72. 添加文件系统 MCP
Section titled “72. 添加文件系统 MCP”claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem /Users/me/code73. 添加 PostgreSQL MCP
Section titled “73. 添加 PostgreSQL MCP”claude mcp add postgres -- npx -y @modelcontextprotocol/server-postgres "postgresql://localhost/mydb"74. 用 .mcp.json 配置
Section titled “74. 用 .mcp.json 配置”{ "mcpServers": { "github": { "command": "npx", "args": ["-y", "@modelcontextprotocol/server-github"] } }}说明:放仓库根目录,团队共享。
75. 列出已装 MCP
Section titled “75. 列出已装 MCP”claude mcp list76. 查看某 MCP 详情
Section titled “76. 查看某 MCP 详情”claude mcp get github77. 移除 MCP
Section titled “77. 移除 MCP”claude mcp remove github78. 用 Puppeteer 抓页面
Section titled “78. 用 Puppeteer 抓页面”claude mcp add puppeteer -- npx -y @modelcontextprotocol/server-puppeteer用 puppeteer MCP 打开 https://example.com,截一张首屏图,存到 /tmp/shot.png。79. Slack 查消息
Section titled “79. Slack 查消息”用 slack MCP 搜索 #engineering 频道最近提到 "deploy failed" 的消息,总结成一份事故清单。80. 数据库查询
Section titled “80. 数据库查询”用 postgres MCP 查最近 7 天注册但没下单的用户,给出 SQL 和结果。弹药库守则:示例不是终点,是起点。抄一条改成你的场景,跑通了再沉淀进 Skill 模板库。要查技巧看 技巧集,要解疑看 FAQ 大全。🚀