Claude Code 是 Anthropic 提供的智能体式编程工具(Agentic coding tool),以命令行界面(CLI)形式使用。它能够读取、编辑并运行代码,并具备完整的项目上下文,适合处理复杂的编码任务。
主要能力
- 读取、编辑和运行代码,具备完整项目上下文。
- 支持非交互式的 print 模式,可用于程序化调用。
- 可委派多文件项目、代码审查、重构等任务。
安装与检测 检测是否已安装: which claude # 应返回一个路径 claude --version
安装命令: npm install -g @anthropic-ai/claude-code
安装后需要认证:运行 claude auth login(使用 Anthropic 账户,或使用 --console 进行 API 计费)。
非交互式用法(print 模式) 从 Research-Claw 进行程序化调用时,使用 -p(print 模式):
基础非交互式执行
claude -p "Refactor the auth module to use async/await"
通过管道传入内容进行分析
cat src/utils.ts | claude -p "Find bugs in this file"
输出 JSON 以便解析
claude -p --output-format json "List all TODO comments in the project"
跳过权限提示(用于全自动流水线)
claude -p --dangerously-skip-permissions "Fix all lint errors"
预算与轮次限制
claude -p --max-turns 5 --max-budget-usd 2.00 "Add unit tests for the auth module"
模型选择
claude -p --model sonnet "Explain this codebase architecture"
关键参数
- -p, --print:非交互模式,运行任务、打印结果并退出。
- --model <model>:设置模型(sonnet、opus 或完整模型 ID)。
- --output-format <fmt>:text / json / stream-json。
- --max-turns <n>:限制智能体轮次(print 模式)。
- --max-budget-usd <n>:以美元为单位的支出上限(print 模式)。
- --dangerously-skip-permissions:跳过所有权限提示。
- --allowedTools <tools>:自动批准特定工具,例如 "Bash(git *)" "Read"。
- --tools <tools>:限制可用工具,例如 "Bash,Read,Edit"。
- --add-dir <path>:授予对额外目录的访问权限。
- --append-system-prompt <text>:追加到默认系统提示。
- -c, --continue:继续最近的对话。
- -r, --resume <id>:按 ID 或名称恢复会话。
- --effort <level>:low / medium / high / max。
Research-Claw exec 调用模式
委派复杂编码任务
exec("claude -p --dangerously-skip-permissions --max-turns 10 'Implement the feature described in TODO.md'")
带预算上限的只读分析
exec("claude -p --tools 'Read' --max-budget-usd 1.00 'Review code quality and list issues'")
输出 JSON 以获得结构化结果
exec("claude -p --output-format json 'List all exported functions in src/'")
文档
- CLI 参考:https://code.claude.com/docs/en/cli-reference
- 常见工作流:https://code.claude.com/docs/en/common-workflows