| `claude auto-mode` | 查看 auto 模式分类器配置 |
Print 模式深入解析
结构化 JSON 输出
terminal(command="claude -p 'Analyze auth.py for security issues' --output-format json --max-turns 5", workdir="/project", timeout=120)
返回一个包含以下内容的 JSON 对象:
{
"type": "result",
"subtype": "success",
"result": "The analysis text...",
"session_id": "75e2167f-...",
"num_turns": 3,
"total_cost_usd": 0.0787,
"duration_ms": 10276,
"stop_reason": "end_turn",
"terminal_reason": "completed",
"usage": { "input_tokens": 5, "output_tokens": 603, ... },
"modelUsage": { "claude-sonnet-4-6": { "costUSD": 0.078, "contextWindow": 200000 } }
}
关键字段:session_id 用于恢复会话,num_turns 为代理循环次数,total_cost_usd 用于支出追踪,subtype 用于成功/错误检测(success、error_max_turns、error_budget)。
流式 JSON 输出
如需实时 token 流式输出,请使用 stream-json 搭配 --verbose:
terminal(command="claude -p 'Write a summary' --output-format stream-json --verbose --include-partial-messages", timeout=60)
返回以换行分隔的 JSON 事件。用 jq 过滤出实时文本:
claude -p "Explain X" --output-format stream-json --verbose --include-partial-messages |
jq -rj 'select(.type == "stream_event" and .event.delta.type? == "text_delta") | .event.delta.text'
流事件包括 system/api_retry,带有 attempt、max_retries 和 error 字段(例如 rate_limit、billing_error)。
双向流式传输
如需实时输入和输出双向流式传输:
claude -p "task" --input-format stream-json --output-format stream-json --replay-user-messages
--replay-user-messages 会将用户消息重新输出到 stdout 以作确认。
管道输入
# Pipe a file for analysis
terminal(command="cat src/auth.py | claude -p 'Review this code for bugs' --max-turns 1", timeout=60)
# Pipe multiple files
terminal(command="cat src/*.py | claude -p 'Find all TODO comments' --max-turns 1", timeout=60)
# Pipe command output
terminal(command="git diff HEAD~3 | claude -p 'Summarize these changes' --max-turns 1", timeout=60)
用于结构化提取的 JSON Schema
terminal(command="claude -p 'List all functions in src/' --output-format json --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}' --max-turns 5", workdir="/project", timeout=90)
从 JSON 结果中解析 structured_output。Claude 会在返回前根据 schema 验证输出。
会话延续
# Start a task
terminal(command="claude -p 'Start refactoring the database layer' --output-format json --max-turns 10 > /tmp/session.json", workdir="/project", timeout=180)
# Resume with session ID
terminal(command="claude -p 'Continue and add connection pooling' --resume $(cat /tmp/session.json | python3 -c 'import json,sys; print(json.load(sys.stdin)["session_id"])') --max-turns 5", workdir="/project", timeout=120)
# Or resume the most recent session in the same directory
terminal(command="claude -p 'What did you do last time?' --continue --max-turns 1", workdir="/project", timeout=30)
# Fork a session (new ID, keeps history)
terminal(command="claude -p 'Try a different approach' --resume --fork-session --max-turns 10", workdir="/project", timeout=120)
用于 CI/脚本的 Bare 模式
terminal(command="claude --bare -p 'Run all tests and report failures' --allowedTools 'Read,Bash' --max-turns 10", workdir="/project", timeout=180)
--bare 会跳过 hooks、插件、MCP 发现和 CLAUDE.md 加载。启动速度最快。需要 ANTHROPIC_API_KEY(跳过 OAuth)。
如需在 bare 模式下选择性加载上下文:
| 要加载的内容 | 标志 |
| 系统提示词追加内容 | `--append-system-prompt "text"` or `--append-system-prompt-file path` |
| 设置 | `--settings ` |
| MCP 服务器 | `--mcp-config ` |
| 自定义代理 | `--agents ''` |
过载时的回退模型
terminal(command="claude -p 'task' --fallback-model haiku --max-turns 5", timeout=90)
当默认模型过载时自动回退到指定模型(仅限 print 模式)。
完整 CLI 标志参考
会话与环境
| 标志 | 效果 |
| `-p, --print` | 非交互式一次性模式(完成后退出) |
| `-c, --continue` | 继续当前目录中最近的一次对话 |
| `-r, --resume ` | 按 ID 或名称恢复指定会话(无 ID 时显示交互式选择器) |
| `--fork-session` | 恢复时创建新的会话 ID,而不是复用原 ID |
| `--session-id ` | 为对话使用指定的 UUID |
| `--no-session-persistence` | 不将会话保存到磁盘(仅限 print 模式) |
| `--add-dir ` | 授予 Claude 访问其他工作目录的权限 |
| `-w, --worktree [name]` | 在 `.claude/worktrees/` 下的独立 git worktree 中运行 |
| `--tmux` | 为 worktree 创建 tmux 会话(需要 `--worktree`) |
| `--ide` | 启动时自动连接有效的 IDE |
| `--chrome` / `--no-chrome` | 启用/禁用 Chrome 浏览器集成(用于 Web 测试) |
| `--from-pr [number]` | 恢复与指定 GitHub PR 关联的会话 |
| `--file ` | 启动时要下载的文件资源(格式:`file_id:relative_path`) |
模型与性能
| 标志 | 效果 |
| `--model ` | 模型选择:`sonnet`、`opus`、`haiku` 或完整名称如 `claude-sonnet-4-6` |
| `--effort ` | 推理深度:`low`、`medium`、`high`、`max`、`auto` | Both |
| `--max-turns ` | 限制代理循环次数(仅限 print 模式;防止失控) |
| `--max-budget-usd ` | 以美元为单位限制 API 支出(仅限 print 模式) |
| `--fallback-model ` | 默认模型过载时自动回退(仅限 print 模式) |
| `--betas ` | API 请求中包含的 Beta 头(仅限 API 密钥用户) |
权限与安全
| 标志 | 效果 |
| `--dangerously-skip-permissions` | 自动批准所有工具使用(文件写入、bash、网络等) |
| `--allow-dangerously-skip-permissions` | 将绕过权限作为*选项*启用,但默认不开启 |
| `--permission-mode ` | `default`、`acceptEdits`、`plan`、`auto`、`dontAsk`、`bypassPermissions` |
| `--allowedTools ` | 白名单指定工具(逗号或空格分隔) |
| `--disallowedTools ` | 黑名单指定工具 |
| `--tools ` | 覆盖内置工具集(`""` = 无,`"default"` = 全部,或工具名称) |
输出与输入格式
| 标志 | 效果 |
| `--output-format ` | `text`(默认)、`json`(单个结果对象)、`stream-json`(换行分隔) |
| `--input-format ` | `text`(默认)或 `stream-json`(实时流式输入) |
| `--json-schema ` | 强制输出符合 schema 的结构化 JSON |
| `--verbose` | 完整的逐轮输出 |
| `--include-partial-messages` | 包含到达的部分消息块(stream-json + print) |
| `--replay-user-messages` | 将用户消息重新输出到 stdout(stream-json 双向) |
系统提示词与上下文
| 标志 | 效果 |
| `--append-system-prompt ` | **追加**到默认系统提示词(保留内置能力) |
| `--append-system-prompt-file ` | 将文件内容**追加**到默认系统提示词 |
| `--system-prompt ` | **替换**整个系统提示词(通常建议改用 --append) |
| `--system-prompt-file ` | 用文件内容**替换**系统提示词 |
| `--bare` | 跳过 hooks、插件、MCP 发现、CLAUDE.md、OAuth(启动最快) |
| `--agents ''` | 以 JSON 动态定义自定义子代理 |
| `--mcp-config ` | 从 JSON 文件加载 MCP 服务器(可重复使用) |
| `--strict-mcp-config` | 只使用 `--mcp-config` 提供的 MCP 服务器,忽略所有其他 MCP 配置 |
| `--settings ` | 从 JSON 文件或内联 JSON 加载额外设置 |
| `--setting-sources ` | 逗号分隔的加载来源:`user`、`project`、`local` |
| `--plugin-dir ` | 仅为本会话从目录加载插件 |
| `--disable-slash-commands` | 禁用所有 skills/斜杠命令 |
调试
| 标志 | 效果 |
| `-d, --debug [filter]` | 启用调试日志,可选类别过滤器(例如 `"api,hooks"`、`"!1p,!file"`) |
| `--debug-file ` | 将调试日志写入文件(隐式启用调试模式) |
代理团队
| 标志 | 效果 |
| `--teammate-mode ` | 代理团队的显示方式:`auto`、`in-process` 或 `tmux` |
| `--brief` | 启用 `SendUserMessage` 工具用于代理与用户通信 |
--allowedTools / --disallowedTools 的工具名称语法
Read # All file reading
Edit # File editing (existing files)
Write # File creation (new files)
Bash # All shell commands
Bash(git *) # Only git commands
Bash(git commit *) # Only git commit commands
Bash(npm run lint:*) # Pattern matching with wildcards
WebSearch # Web search capability
WebFetch # Web page fetching
mcp____ # Specific MCP tool
设置与配置
设置优先级(从高到低)
CLI 标志 — 覆盖一切
本地项目:.claude/settings.local.json(个人,已被 gitignore)
项目:.claude/settings.json(团队共享,纳入 git 跟踪)
用户:~/.claude/settings.json(全局)
设置中的权限配置
{
"permissions": {
"allow": ["Bash(npm run lint:*)", "WebSearch", "Read"],
"ask": ["Write(*.ts)", "Bash(git push*)"],
"deny": ["Read(.env)", "Bash(rm -rf *)"]
}
}
记忆文件(CLAUDE.md)层级
全局:~/.claude/CLAUDE.md — 应用于所有项目
项目:./CLAUDE.md — 项目特定上下文(纳入 git 跟踪)
本地:.claude/CLAUDE.local.md — 个人项目覆盖(已被 gitignore)
在交互式模式中使用 # 前缀可快速添加到记忆:# Always use 2-space indentation。
交互式会话:斜杠命令
会话与上下文
| 命令 | 用途 |
| `/help` | 显示所有命令(包括自定义和 MCP 命令) |
| `/compact [focus]` | 压缩上下文以节省 token;CLAUDE.md 在压缩后保留。例如 `/compact focus on auth logic` |
| `/clear` | 清空对话历史,重新开始 |
| `/context` | 以彩色网格可视化上下文占用情况,并附优化建议 |
| `/cost` | 查看 token 用量,含按模型和缓存命中的细分 |
| `/resume` | 切换到或恢复其他会话 |
| `/rewind` | 回退到对话或代码的先前检查点 |
| `/btw ` | 提出附带问题而不增加上下文开销 |
| `/status` | 显示版本、连接状态和会话信息 |
| `/todos` | 列出对话中跟踪的行动事项 |
| `/exit` or `Ctrl+D` | 结束会话 |
开发与审查
| 命令 | 用途 |
| `/review` | 对当前更改请求代码审查 |
| `/security-review` | 对当前更改执行安全分析 |
| `/plan [description]` | 进入 Plan 模式,自动开始任务规划 |
| `/loop [interval]` | 在会话中安排周期性任务 |
| `/batch` | 为大型并行更改自动创建 worktree(5-30 个) |
配置 & Tools
| 命令 | 用途 |
| `/model [model]` | 在会话中切换模型(用方向键调整 effort) |
| `/effort [level]` | 设置推理努力程度:`low`、`medium`、`high`、`max` 或 `auto` |
| `/init` | 为项目记忆创建 CLAUDE.md 文件 |
| `/memory` | 打开 CLAUDE.md 进行编辑 |
| `/config` | 打开交互式设置配置 |
| `/permissions` | 查看/更新工具权限 |
| `/agents` | 管理专用子代理 |
| `/mcp` | 管理 MCP 服务器的交互式界面 |
| `/add-dir` | 添加额外的工作目录(对 monorepo 很有用) |
| `/usage` | 显示计划限额和速率限制状态 |
| `/voice` | 启用按住说话的语音模式(支持 20 种语言;按住空格键录音,松开发送) |
| `/release-notes` | 交互式选择查看版本发布说明 |
自定义斜杠命令
创建 .claude/commands/.md(项目共享)或 ~/.claude/commands/.md(个人):
# .claude/commands/deploy.md
Run the deploy pipeline:
1. Run all tests
2. Build the Docker image
3. Push to registry
4. Update the $ARGUMENTS environment (default: staging)
用法:/deploy production — $ARGUMENTS 会被替换为用户输入。
Skills(自然语言调用)
与斜杠命令(手动调用)不同,.claude/skills/ 中的 skills 是 markdown 指南,当任务匹配时 Claude 会通过自然语言自动调用:
# .claude/skills/database-migration.md
When asked to create or modify database migrations:
1. Use Alembic for migration generation
2. Always create a rollback function
3. Test migrations against a local database copy
交互式会话:键盘快捷键
常规控制
| 按键 | 操作 |
| `Ctrl+C` | 取消当前输入或生成 |
| `Ctrl+D` | 退出会话 |
| `Ctrl+R` | 反向搜索命令历史 |
| `Ctrl+B` | 将正在运行的任务转入后台 |
| `Ctrl+V` | 将图片粘贴到对话中 |
| `Ctrl+O` | 转录模式 — 查看 Claude 的思考过程 |
| `Ctrl+G` or `Ctrl+X Ctrl+E` | 在外部编辑器中打开提示词 |
| `Esc Esc` | 回退对话或代码状态 / 生成摘要 |
模式切换
| 按键 | 操作 |
| `Shift+Tab` | 循环切换权限模式(Normal → Auto-Accept → Plan) |