CLI 参考

CLI 参考

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

CLI 命令

命令 描述 示例
claude 启动交互式会话 claude
claude "query" 使用初始提示启动交互式会话 claude "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 --email [email protected]
claude auth logout 登出 claude auth logout
claude auth status 显示身份验证状态 claude auth status
claude agents 列出所有已配置的 subagents claude agents
claude mcp 配置 MCP 服务器 请参阅 MCP 文档
claude remote-control 启动 Remote Control 会话 claude remote-control

CLI 标志

常用标志

标志 描述 示例
--add-dir 添加其他工作目录 claude --add-dir ../apps ../lib
--agent 为当前会话指定代理 claude --agent my-custom-agent
--agents 动态定义自定义 subagents claude --agents '{"reviewer":{...}}'
--allowedTools 无需提示权限即可执行的工具 "Bash(git log *)" "Read"
--append-system-prompt 附加到默认系统提示 claude --append-system-prompt "Always use TypeScript"
--chrome 启用 Chrome 浏览器集成 claude --chrome
--continue, -c 继续最近的对话 claude --continue
--dangerously-skip-permissions 跳过所有权限提示 claude --dangerously-skip-permissions
--debug 启用调试模式 claude --debug "api,mcp"
--disable-slash-commands 禁用所有 skills 和命令 claude --disable-slash-commands
--disallowedTools 从模型上下文中删除工具 "Bash(git log *)" "Edit"
--fork-session 创建新的会话 ID claude --resume abc123 --fork-session
--from-pr 恢复链接到 GitHub PR 的会话 claude --from-pr 123
--ide 自动连接到 IDE claude --ide
--init 运行初始化 hooks claude --init
--model 设置模型 claude --model claude-sonnet-4-6
--output-format 指定输出格式 claude -p "query" --output-format json
--permission-mode 设置权限模式 claude --permission-mode plan
--print, -p 打印响应而不进入交互模式 claude -p "query"
--remote 创建网络会话 claude --remote "Fix the login bug"
--resume, -r 恢复特定会话 claude --resume auth-refactor
--session-id 使用特定的会话 ID claude --session-id "uuid"
--tools 限制可用的内置工具 claude --tools "Bash,Edit,Read"
--verbose 启用详细日志记录 claude --verbose
--version, -v 输出版本号 claude -v
--worktree, -w 在隔离的 worktree 中启动 claude -w feature-auth

Agents 标志格式

--agents 标志接受 JSON 对象定义 subagents:

1
2
3
4
5
6
7
8
9
10
11
12
{
"code-reviewer": {
"description": "Expert code reviewer. Use proactively after code changes.",
"prompt": "You are a senior code reviewer.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
},
"debugger": {
"description": "Debugging specialist for errors.",
"prompt": "You are an expert debugger."
}
}

支持字段

字段 必需 描述
description 何时应调用 subagent 的描述
prompt 指导 subagent 行为的系统提示
tools subagent 可以使用的工具数组
disallowedTools 明确拒绝的工具名称数组
model 要使用的模型:sonnetopushaikuinherit
skills 预加载的 skill 名称数组
mcpServers MCP servers 数组
maxTurns subagent 停止前的最大代理转数

系统提示标志

标志 行为 模式
--system-prompt 替换整个默认提示 交互 + 打印
--system-prompt-file 替换为文件内容 仅打印
--append-system-prompt 附加到默认提示 交互 + 打印
--append-system-prompt-file 附加文件内容到默认提示 仅打印

何时使用每个:

  • --system-prompt:完全控制 Claude 的系统提示

    1
    claude --system-prompt "You are a Python expert who only writes type-annotated code"
  • --system-prompt-file:从文件加载自定义提示

    1
    claude -p --system-prompt-file ./prompts/code-review.txt "Review this PR"
  • --append-system-prompt:添加特定指令同时保持默认功能

    1
    claude --append-system-prompt "Always use TypeScript and include JSDoc comments"
  • --append-system-prompt-file:从文件附加指令

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

输出格式

JSON 格式

1
claude -p "query" --output-format json

输出包含元数据(包括成本和持续时间)的消息的 JSON 数组。

流式 JSON 格式

1
claude -p "query" --output-format stream-json

在 Claude 处理请求时实时输出一系列 JSON 对象。

示例

基本查询

1
claude -p "Explain what this project does"

结构化输出

1
claude -p "List all API endpoints" --output-format json

管道数据

1
cat error.log | claude -p "Analyze this error" > analysis.txt

恢复会话

1
claude --resume auth-refactor "Continue with the tests"

并行 worktree

1
2
claude --worktree feature-auth
claude --worktree bugfix-login

本文档来自 Claude Code 官方文档

© 2026 Generative AI Discovery All Rights Reserved.
Theme by hiero