Skip to content

Claude Code CLI 参考手册


一、CLI 命令

命令描述示例
claude启动交互式 REPLclaude
claude "query"带初始提示启动 REPLclaude "explain this project"
claude -p "query"SDK 查询后退出claude -p "explain this function"
cat fileclaude -p "query"处理管道输入内容
claude -c继续当前目录最近对话claude -c
claude -c -p "query"通过 SDK 继续对话claude -c -p "Check for type errors"
claude -r "<session>" "query"按 ID/名称恢复会话claude -r "auth-refactor" "Finish this PR"
claude update更新至最新版本claude update
claude mcp配置 MCP 服务器详见 Claude Code MCP 文档

二、CLI 标志

通过以下标志可自定义 Claude Code 运行行为:

标志描述示例
--add-dir添加工作目录(自动验证路径有效性)claude --add-dir ../apps ../lib
--agent指定会话代理(覆盖默认 agent 设置)claude --agent my-custom-agent
--agents以 JSON 定义自定义子代理claude --agents '{"reviewer":{"description":"Reviews code","prompt":"You are a code reviewer"}}'
--allowedTools免权限提示的可用工具(限制工具用 --tools"Bash(git log:*)" "Bash(git diff:*)" "Read"
--append-system-prompt追加内容到默认系统提示(交互/打印模式均生效)claude --append-system-prompt "Always use TypeScript"
--betasAPI 请求附加 Beta 标头(仅限 API 密钥用户)claude --betas interleaved-thinking
--chrome启用 Chrome 浏览器集成(网络自动化/测试)claude --chrome
--continue, -c加载当前目录最近对话claude --continue
--dangerously-skip-permissions跳过权限提示(谨慎操作)claude --dangerously-skip-permissions
--debug启用调试模式,支持类别过滤(如 "api,hooks"claude --debug "api,mcp"
--disallowedTools禁用指定工具(从上下文移除)"Bash(git log:*)" "Bash(git diff:*)" "Edit"
--fallback-model默认模型过载时自动切换(仅打印模式)claude -p --fallback-model sonnet "query"
--fork-session恢复会话时生成新 ID(搭配 --resume/--continueclaude --resume abc123 --fork-session
--ide自动连接可用 IDEclaude --ide
--include-partial-messages输出包含部分流事件(需搭配 --print--output-format=stream-jsonclaude -p --output-format stream-json --include-partial-messages "query"
--input-format打印模式输入格式(可选 text/stream-jsonclaude -p --output-format json --input-format stream-json
--json-schema输出符合 JSON Schema 的验证结果(仅打印模式)claude -p --json-schema '{"type":"object","properties":{...}}' "query"
--max-turns限制代理轮次(仅打印模式,超限报错退出)claude -p --max-turns 3 "query"
--mcp-config从 JSON 文件/字符串加载 MCP 配置claude --mcp-config ./mcp.json
--model指定会话模型(支持别名 sonnet/opus 或完整名称)claude --model claude-sonnet-4-5-20250929
--no-chrome禁用 Chrome 集成claude --no-chrome
--output-format打印模式输出格式(可选 text/json/stream-jsonclaude -p "query" --output-format json
--permission-mode按指定权限模式启动claude --permission-mode plan
--permission-prompt-tool非交互模式指定权限提示处理工具claude -p --permission-prompt-tool mcp_auth_tool "query"
--plugin-dir加载指定目录插件(可重复使用)claude --plugin-dir ./my-plugins
--print, -p打印响应后退出(非交互模式)claude -p "query"
--resume, -r按 ID/名称恢复会话,或唤起交互式选择器claude --resume auth-refactor
--session-id指定会话 ID(需为有效 UUID)claude --session-id "550e8400-e29b-41d4-a716-446655440000"
--setting-sources指定加载的设置源(逗号分隔 user/project/localclaude --setting-sources user,project
--settings加载自定义 JSON 配置文件/字符串claude --settings ./settings.json
--strict-mcp-config仅使用 --mcp-config 配置,忽略其他 MCP 设置claude --strict-mcp-config --mcp-config ./mcp.json
--system-prompt替换默认系统提示(交互/打印模式均生效)claude --system-prompt "You are a Python expert"
--system-prompt-file从文件加载系统提示(替换默认,仅打印模式)claude -p --system-prompt-file ./custom-prompt.txt "query"
--tools限制可用内置工具("" 禁用全部,"default" 启用全部)claude --tools "Bash,Edit,Read"
--verbose启用详细日志,展示完整逐轮输出claude --verbose
--version, -v输出版本号claude -v

提示--output-format json 标志非常适合脚本和自动化场景,可直接编程解析 Claude 响应结果。


三、扩展说明

3.1 代理标志格式

--agents 标志接收 JSON 对象,用于定义一个或多个自定义子代理。每个子代理需配置唯一名称作为键,值为包含以下字段的对象:

字段必填描述
description描述子代理的适用场景
prompt定义子代理行为的系统提示
tools子代理专属工具列表(如 ["Read", "Edit"],省略则继承全部工具)
model子代理使用模型(支持 sonnet/opus/haiku,省略则用默认模型)

示例

bash
claude --agents '{
  "code-reviewer": {
    "description": "Expert code reviewer. Use proactively after code changes.",
    "prompt": "You are a senior code reviewer. Focus on code quality, security, and best practices.",
    "tools": ["Read", "Grep", "Glob", "Bash"],
    "model": "sonnet"
  },
  "debugger": {
    "description": "Debugging specialist for errors and test failures.",
    "prompt": "You are an expert debugger. Analyze errors, identify root causes, and provide fixes."
  }
}'

系统提示标志

Claude Code 提供 3 种系统提示自定义方式,满足不同使用需求:

标志行为适用模式典型用例
--system-prompt替换默认系统提示交互+打印完全自定义 Claude 行为指令
--system-prompt-file从文件加载提示并替换仅打印团队共享提示模板、版本控制
--append-system-prompt追加内容到默认提示交互+打印保留默认功能,添加个性化指令

使用场景与示例

  1. --system-prompt:完全接管系统提示,清空默认指令

    bash
    claude --system-prompt "You are a Python expert who only writes type-annotated code"
  2. --system-prompt-file:从文件读取提示,适合标准化场景

    bash
    claude -p --system-prompt-file ./prompts/code-review.txt "Review this PR"
  3. --append-system-prompt:保留默认功能,追加定制要求(推荐大多数场景使用)

    bash
    claude --append-system-prompt "Always use TypeScript and include JSDoc comments"

注意--system-prompt--system-prompt-file 互斥,不可同时使用。
提示:优先使用 --append-system-prompt,既能保留 Claude Code 内置能力,又能满足定制需求;仅需完全自定义时,再使用另外两个标志。

打印模式(-p)的详细用法(输出格式、流式传输、程序化集成等),参考 SDK 文档

AI 思考中...

Claude Code 钩子

Claude Code 控制与模式

基于 VitePress 构建,部署于 GitHub Pages