Skip to content

Documentation Index

Fetch the complete documentation index at: https://code.claude.com/docs/llms.txt Use this file to discover all available pages before exploring further.

CLI 参考

Claude Code 命令行界面的完整参考,包括命令和标志。

CLI 命令

命令描述示例
claude启动交互式 REPLclaude
claude "query"使用初始提示启动 REPLclaude "explain this project"
claude -p "query"通过 SDK 查询,然后退出claude -p "explain this function"
cat file | claude -p "query"处理管道内容cat logs.txt | claude -p "explain"
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 auth login登录到您的 Anthropic 账户claude auth login
claude auth logout从您的 Anthropic 账户登出claude auth logout
claude auth status显示身份验证状态claude auth status
claude agents列出所有已配置的 subagents,按来源分组claude agents
claude mcp配置 Model Context Protocol (MCP) 服务器请参阅 Claude Code MCP 文档
claude remote-control启动 Remote Control 会话以从 Claude.ai 或 Claude 应用控制 Claude Code,同时在本地运行。请参阅 Remote Control 了解标志claude remote-control

CLI 标志

使用这些命令行标志自定义 Claude Code 的行为:

标志描述示例
--add-dir添加额外的工作目录供 Claude 访问(验证每个路径是否存在为目录)claude --add-dir ../apps ../lib
--agent为当前会话指定代理(覆盖 agent 设置)claude --agent my-custom-agent
--agents通过 JSON 动态定义自定义 subagents(请参阅下面的格式)claude --agents '{"reviewer":{"description":"Reviews code","prompt":"You are a code reviewer"}}'
--allow-dangerously-skip-permissions启用权限绕过作为选项,而不立即激活它。允许与 --permission-mode 组合(谨慎使用)claude --permission-mode plan --allow-dangerously-skip-permissions
--allowedTools无需提示权限即可执行的工具。请参阅 权限规则语法 了解模式匹配。要限制可用的工具,请改用 --tools"Bash(git log *)" "Bash(git diff *)" "Read"
--append-system-prompt将自定义文本附加到默认系统提示的末尾(在交互和打印模式中都有效)claude --append-system-prompt "Always use TypeScript"
--append-system-prompt-file从文件加载其他系统提示文本并附加到默认提示(仅打印模式)claude -p --append-system-prompt-file ./extra-rules.txt "query"
--betas要包含在 API 请求中的 Beta 标头(仅限 API 密钥用户)claude --betas interleaved-thinking
--chrome启用 Chrome 浏览器集成以进行网络自动化和测试claude --chrome
--continue, -c加载当前目录中最近的对话claude --continue
--dangerously-skip-permissions跳过所有权限提示(谨慎使用)claude --dangerously-skip-permissions
--debug启用调试模式,可选类别过滤(例如,"api,hooks""!statsig,!file"claude --debug "api,mcp"
--disable-slash-commands为此会话禁用所有 skills 和 slash commandsclaude --disable-slash-commands
--disallowedTools从模型的上下文中删除且无法使用的工具"Bash(git log *)" "Bash(git diff *)" "Edit"
--fallback-model当默认模型过载时启用自动回退到指定模型(仅打印模式)claude -p --fallback-model sonnet "query"
--fork-session恢复时创建新的会话 ID,而不是重用原始 ID(与 --resume--continue 一起使用)claude --resume abc123 --fork-session
--from-pr恢复链接到特定 GitHub PR 的会话。接受 PR 编号或 URL。通过 gh pr create 创建时会自动链接会话claude --from-pr 123
--ide如果恰好有一个有效的 IDE 可用,则在启动时自动连接到 IDEclaude --ide
--init运行初始化 hooks 并启动交互模式claude --init
--init-only运行初始化 hooks 并退出(无交互会话)claude --init-only
--include-partial-messages在输出中包含部分流事件(需要 --print--output-format=stream-jsonclaude -p --output-format stream-json --include-partial-messages "query"
--input-format为打印模式指定输入格式(选项:textstream-jsonclaude -p --output-format json --input-format stream-json
--json-schema在代理完成其工作流后获得与 JSON Schema 匹配的验证 JSON 输出(仅打印模式,请参阅 结构化输出claude -p --json-schema '{"type":"object","properties":{...}}' "query"
--maintenance运行维护 hooks 并退出claude --maintenance
--max-budget-usd在停止前在 API 调用上花费的最大美元金额(仅打印模式)claude -p --max-budget-usd 5.00 "query"
--max-turns限制代理转数(仅打印模式)。达到限制时以错误退出。默认无限制claude -p --max-turns 3 "query"
--mcp-config从 JSON 文件或字符串加载 MCP 服务器(以空格分隔)claude --mcp-config ./mcp.json
--model为当前会话设置模型,使用最新模型的别名(sonnetopus)或模型的完整名称claude --model claude-sonnet-4-6
--no-chrome为此会话禁用 Chrome 浏览器集成claude --no-chrome
--no-session-persistence禁用会话持久化,以便会话不会保存到磁盘且无法恢复(仅打印模式)claude -p --no-session-persistence "query"
--output-format为打印模式指定输出格式(选项:textjsonstream-jsonclaude -p "query" --output-format json
--permission-mode以指定的 权限模式 开始claude --permission-mode plan
--permission-prompt-tool指定 MCP 工具以在非交互模式下处理权限提示claude -p --permission-prompt-tool mcp_auth_tool "query"
--plugin-dir仅为此会话从目录加载插件(可重复)claude --plugin-dir ./my-plugins
--print, -p打印响应而不进入交互模式(请参阅 Agent SDK 文档 了解程序化使用详情)claude -p "query"
--remote在 claude.ai 上创建新的 网络会话,提供任务描述claude --remote "Fix the login bug"
--resume, -r按 ID 或名称恢复特定会话,或显示交互式选择器以选择会话claude --resume auth-refactor
--session-id为对话使用特定的会话 ID(必须是有效的 UUID)claude --session-id "550e8400-e29b-41d4-a716-446655440000"
--setting-sources要加载的设置源的逗号分隔列表(userprojectlocalclaude --setting-sources user,project
--settings设置 JSON 文件的路径或 JSON 字符串以加载其他设置claude --settings ./settings.json
--strict-mcp-config仅使用 --mcp-config 中的 MCP 服务器,忽略所有其他 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"
--teleport在本地终端中恢复 网络会话claude --teleport
--teammate-mode设置 代理团队 队友的显示方式:auto(默认)、in-processtmux。请参阅 设置代理团队claude --teammate-mode in-process
--tools限制 Claude 可以使用的内置工具(在交互和打印模式中都有效)。使用 "" 禁用所有,"default" 表示全部,或工具名称如 "Bash,Edit,Read"claude --tools "Bash,Edit,Read"
--verbose启用详细日志记录,显示完整的逐轮输出(有助于在打印和交互模式中调试)claude --verbose
--version, -v输出版本号claude -v
--worktree, -w在隔离的 git worktree 中启动 Claude,位于 <repo>/.claude/worktrees/<name>。如果未给出名称,则自动生成claude -w feature-auth

Agents 标志格式

--agents 标志接受一个 JSON 对象,该对象定义一个或多个自定义 subagents。每个 subagent 需要一个唯一的名称(作为键)和一个具有以下字段的定义对象:

字段必需描述
description自然语言描述何时应调用 subagent
prompt指导 subagent 行为的系统提示
toolssubagent 可以使用的特定工具数组,例如 ["Read", "Edit", "Bash"]。如果省略,则继承所有工具。支持 Task(agent_type) 语法
disallowedTools为此 subagent 明确拒绝的工具名称数组
model要使用的模型别名:sonnetopushaikuinherit。如果省略,默认为 inherit
skills要预加载到 subagent 上下文中的 skill 名称数组
mcpServers此 subagent 的 MCP 服务器 数组。每个条目是服务器名称字符串或 {name: config} 对象
maxTurnssubagent 停止前的最大代理转数

示例:

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."
  }
}'

有关创建和使用 subagents 的更多详情,请参阅 subagents 文档

系统提示标志

Claude Code 提供四个标志用于自定义系统提示,每个标志服务于不同的目的:

标志行为模式用例
--system-prompt替换整个默认提示交互 + 打印完全控制 Claude 的行为和指令
--system-prompt-file替换为文件内容仅打印从文件加载提示以实现可重现性和版本控制
--append-system-prompt附加到默认提示交互 + 打印添加特定指令,同时保持默认 Claude Code 行为
--append-system-prompt-file附加文件内容到默认提示仅打印从文件加载其他指令,同时保持默认值

何时使用每个:

  • --system-prompt:当您需要完全控制 Claude 的系统提示时使用。这会删除所有默认 Claude Code 指令,为您提供一个空白板。

    bash
    claude --system-prompt "You are a Python expert who only writes type-annotated code"
  • --system-prompt-file:当您想从文件加载自定义提示时使用,对于团队一致性或版本控制的提示模板很有用。

    bash
    claude -p --system-prompt-file ./prompts/code-review.txt "Review this PR"
  • --append-system-prompt:当您想添加特定指令同时保持 Claude Code 的默认功能完整时使用。这是大多数用例的最安全选项。

    bash
    claude --append-system-prompt "Always use TypeScript and include JSDoc comments"
  • --append-system-prompt-file:当您想从文件附加指令同时保持 Claude Code 的默认值时使用。对于版本控制的添加很有用。

    bash
    claude -p --append-system-prompt-file ./prompts/style-rules.txt "Review this PR"

--system-prompt--system-prompt-file 互斥。附加标志可以与任一替换标志一起使用。

对于大多数用例,建议使用 --append-system-prompt--append-system-prompt-file,因为它们保留 Claude Code 的内置功能,同时添加您的自定义要求。仅当您需要完全控制系统提示时才使用 --system-prompt--system-prompt-file

另请参阅

OpenClaw 自动报告系统