cli-reference
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCLI Reference
CLI 参考文档
Complete reference for Claude Code command-line interface.
Claude Code命令行界面的完整参考文档。
When to Use
适用场景
- "What CLI flags are available?"
- "How do I use headless mode?"
- "Claude in automation/CI/CD"
- "Output format options"
- "System prompt via CLI"
- "How do I spawn agents properly?"
- "有哪些可用的CLI参数?"
- "如何使用Headless Mode?"
- "Claude在自动化/CI/CD中的应用"
- "输出格式选项"
- "通过CLI设置系统提示词"
- "如何正确生成Agent?"
Core Commands
核心命令
| Command | Description | Example |
|---|---|---|
| Start interactive REPL | |
| REPL with initial prompt | |
| Headless mode (SDK) | |
| Process piped content | |
| Continue most recent | |
| Continue via SDK | |
| Resume session | |
| Update version | |
| Configure MCP servers | See MCP docs |
| 命令 | 描述 | 示例 |
|---|---|---|
| 启动交互式REPL | |
| 带初始提示词的REPL | |
| Headless Mode(SDK模式) | |
| 处理管道输入内容 | |
| 继续最近的会话 | |
| 通过SDK继续会话 | |
| 恢复指定会话 | |
| 更新版本 | |
| 配置MCP服务器 | 参见MCP文档 |
Session Control
会话控制
| Flag | Description | Example |
|---|---|---|
| Load most recent conversation | |
| Resume session by ID/name | |
| Use specific UUID | |
| Create new session on resume | |
| 参数 | 描述 | 示例 |
|---|---|---|
| 加载最近的对话 | |
| 通过ID/名称恢复会话 | |
| 使用指定UUID | |
| 恢复会话时创建新会话 | |
Headless Mode (Critical for Agents)
Headless Mode(对Agent至关重要)
| Flag | Description | Example |
|---|---|---|
| Non-interactive, exit after | |
| | |
| Limit agentic turns | |
| Full turn-by-turn output | |
| Skip permission prompts | |
| Include streaming events | |
| Input format (text/stream-json) | |
| 参数 | 描述 | 示例 |
|---|---|---|
| 非交互式模式,执行后退出 | |
| 可选值: | |
| 限制Agent交互轮次 | |
| 输出完整的逐轮交互内容 | |
| 跳过权限提示 | |
| 包含流式事件 | |
| 输入格式(text/stream-json) | |
Tool Control
工具控制
| Flag | Description | Example |
|---|---|---|
| Auto-approve these tools | |
| Block these tools | |
| Only allow these tools | |
| 参数 | 描述 | 示例 |
|---|---|---|
| 自动批准指定工具 | |
| 阻止指定工具 | |
| 仅允许使用指定工具 | |
Subagent Definition (--agents flag)
子Agent定义(--agents参数)
Define custom subagents inline via JSON:
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 and security.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
},
"debugger": {
"description": "Debugging specialist for errors and test failures.",
"prompt": "You are an expert debugger. Analyze errors and provide fixes."
}
}'通过JSON内联定义自定义子Agent:
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 and security.",
"tools": ["Read", "Grep", "Glob", "Bash"],
"model": "sonnet"
},
"debugger": {
"description": "Debugging specialist for errors and test failures.",
"prompt": "You are an expert debugger. Analyze errors and provide fixes."
}
}'Agent Fields
Agent字段
| Field | Required | Description |
|---|---|---|
| Yes | When to invoke this agent |
| Yes | System prompt for behavior |
| No | Allowed tools (inherits all if omitted) |
| No | |
| 字段 | 是否必填 | 描述 |
|---|---|---|
| 是 | 何时调用该Agent的说明 |
| 是 | 定义Agent行为的系统提示词 |
| 否 | 允许使用的工具(省略则继承所有工具) |
| 否 | 可选值: |
Key Insight
关键要点
When Lead uses Task tool, it auto-spawns from these definitions. No manual spawn needed.
当主Agent使用Task工具时,会自动从这些定义中生成子Agent,无需手动生成。
System Prompt Customization
系统提示词自定义
| Flag | Behavior | Modes |
|---|---|---|
| Replace entire prompt | Interactive + Print |
| Replace from file | Print only |
| Append to default (recommended) | Interactive + Print |
Use for most cases - preserves Claude Code capabilities.
--append-system-prompt| 参数 | 行为 | 适用模式 |
|---|---|---|
| 替换整个系统提示词 | 交互式+非交互式模式 |
| 从文件中替换系统提示词 | 仅非交互式模式 |
| 追加到默认提示词后(推荐使用) | 交互式+非交互式模式 |
大多数场景推荐使用 - 可保留Claude Code的核心功能。
--append-system-promptModel Selection
模型选择
| Flag | Description | Example |
|---|---|---|
| Set model for session | |
| Fallback if default overloaded | |
Aliases: , ,
sonnetopushaiku| 参数 | 描述 | 示例 |
|---|---|---|
| 为会话设置模型 | |
| 默认模型过载时的备用模型 | |
别名:、、
sonnetopushaikuMCP Configuration
MCP配置
| Flag | Description | Example |
|---|---|---|
| Load MCP servers from JSON | |
| Only use these MCP servers | |
| 参数 | 描述 | 示例 |
|---|---|---|
| 从JSON文件加载MCP服务器配置 | |
| 仅使用指定的MCP服务器 | |
Advanced Flags
高级参数
| Flag | Description | Example |
|---|---|---|
| Add working directories | |
| Specify agent for session | |
| Start in permission mode | |
| MCP tool for permissions | |
| Load plugins from directory | |
| Load settings from file/JSON | |
| Which settings to load | |
| Beta API headers | |
| Enable debug mode | |
| Auto-connect to IDE | |
| Enable Chrome integration | |
| Disable Chrome for session | |
| Verbose LSP debugging | |
| Output version | |
| 参数 | 描述 | 示例 |
|---|---|---|
| 添加工作目录 | |
| 为会话指定Agent | |
| 以指定权限模式启动 | |
| 用于权限验证的MCP工具 | |
| 从指定目录加载插件 | |
| 从文件/JSON加载设置 | |
| 指定加载的设置来源 | |
| Beta版API头信息 | |
| 启用调试模式 | |
| 自动连接到IDE | |
| 启用Chrome集成 | |
| 会话中禁用Chrome | |
| 启用详细的LSP调试日志 | |
| 输出版本信息 | |
Output Formats
输出格式
JSON (for parsing)
JSON(用于解析)
bash
claude -p "query" --output-format jsonbash
claude -p "query" --output-format json{"result": "...", "session_id": "...", "usage": {...}}
{"result": "...", "session_id": "...", "usage": {...}}
undefinedundefinedStreaming (for real-time monitoring)
流式输出(用于实时监控)
bash
claude -p "query" --output-format stream-jsonbash
claude -p "query" --output-format stream-jsonNewline-delimited JSON events
换行分隔的JSON事件
undefinedundefinedStructured Output (schema validation)
结构化输出(Schema验证)
bash
claude -p "Extract data" \
--output-format json \
--json-schema '{"type":"object","properties":{...}}'bash
claude -p "Extract data" \
--output-format json \
--json-schema '{"type":"object","properties":{...}}'Headless Agent Pattern (CRITICAL)
Headless Agent模式(至关重要)
Proper headless agent spawn:
bash
claude -p "$TASK_PROMPT" \
--session-id "$UUID" \
--dangerously-skip-permissions \
--max-turns 100 \
--output-format stream-json \
--agents '{...}' \
--append-system-prompt "Context: ..."Missing any of these causes hangs:
- - Track the session
--session-id - - Headless requires this
--dangerously-skip-permissions - - Prevents infinite loops
--max-turns
正确的Headless Agent生成方式:
bash
claude -p "$TASK_PROMPT" \
--session-id "$UUID" \
--dangerously-skip-permissions \
--max-turns 100 \
--output-format stream-json \
--agents '{...}' \
--append-system-prompt "Context: ..."缺少以下任意参数会导致程序挂起:
- - 跟踪会话
--session-id - - Headless模式必须设置
--dangerously-skip-permissions - - 防止无限循环
--max-turns
Common Patterns
常见模式
CI/CD Automation
CI/CD自动化
bash
claude -p "Run tests and fix failures" \
--dangerously-skip-permissions \
--max-turns 50 \
--output-format json | jq '.result'bash
claude -p "Run tests and fix failures" \
--dangerously-skip-permissions \
--max-turns 50 \
--output-format json | jq '.result'Piped Input
管道输入
bash
cat error.log | claude -p "Find root cause"
gh pr diff | claude -p "Review for security"bash
cat error.log | claude -p "Find root cause"
gh pr diff | claude -p "Review for security"Multi-turn Session
多轮会话
bash
id=$(claude -p "Start task" --output-format json | jq -r '.session_id')
claude -p "Continue" --resume "$id"bash
id=$(claude -p "Start task" --output-format json | jq -r '.session_id')
claude -p "Continue" --resume "$id"Stream Monitoring
流监控
bash
claude -p "Long task" \
--output-format stream-json \
--include-partial-messages | while read -r line; do
echo "$line" | jq '.type'
donebash
claude -p "Long task" \
--output-format stream-json \
--include-partial-messages | while read -r line; do
echo "$line" | jq '.type'
doneKeyboard Shortcuts (Interactive)
键盘快捷键(交互式模式)
| Shortcut | Action |
|---|---|
| Cancel current |
| Exit |
| Reverse search history |
| Rewind changes |
| Toggle permission mode |
| 快捷键 | 操作 |
|---|---|
| 取消当前操作 |
| 退出 |
| 反向搜索历史记录 |
| 撤销更改 |
| 切换权限模式 |
Quick Commands
快捷命令
| Prefix | Action |
|---|---|
| Slash command |
| Bash mode |
| Add to memory |
| File mention |
| 前缀 | 操作 |
|---|---|
| 斜杠命令 |
| Bash模式 |
| 添加到记忆 |
| 文件引用 |