
2025年5月15日
Claude Code CLI 用法与控制
了解如何在命令行中使用 Claude Code,包括 CLI 命令、参数和斜杠命令。
入门
Claude Code 提供两种主要的交互方式:
- 交互模式:运行 claude 启动 REPL 会话
- 一次性命令模式:用 claude -p “query” 快速执行命令
示例:
# 启动交互模式
claude
# 启动并带有初始问题
claude "explain this project"
# 执行单条命令后退出
claude -p "what does this function do?"
# 处理管道输入内容
cat logs.txt | claude -p "analyze these errors"
CLI 命令
命令 | 描述 | 示例 |
---|---|---|
claude | 启动交互式 REPL | claude |
claude “query” | 用初始提示启动 REPL | claude “explain this project” |
claude -p “query” | 执行一次性查询后退出 | claude -p “explain this function” |
cat file | claude -p “query” | 处理管道输入内容 | cat logs.txt | claude -p “explain” |
claude -c | 继续最近的会话 | claude -c |
claude -c -p “query” | 在打印模式下继续会话 | claude -c -p “Check for type errors” |
claude -r “” “query” | 按会话 ID 恢复会话 | claude -r “abc123” “Finish this PR” |
claude config | 配置设置 | claude config set –global theme dark |
claude update | 更新到最新版本 | claude update |
claude mcp | 配置 Model Context Protocol | 见教程的 MCP 部分 |
CLI 参数(Flags)
通过以下命令行参数自定义 Claude Code 的行为:
参数 | 描述 | 示例 |
---|---|---|
–print, -p | 以非交互模式打印响应(详见下方“打印模式”说明) | claude -p “query” |
–output-format | 指定打印模式下的输出格式(可选项:text, json, stream-json) | claude -p “query” –output-format json |
–verbose | 启用详细日志,显示完整对话过程(调试用) | claude –verbose |
–max-turns | 限制非交互模式下的智能体回合数 | claude -p –max-turns 3 “query” |
–permission-prompt-tool | 指定 MCP 工具处理非交互模式下的权限请求 | claude -p –permission-prompt-tool mcp_auth_tool “query” |
–resume | 按会话 ID 或交互选择恢复特定会话 | claude –resume abc123 “query” |
–continue | 加载当前目录下最近的对话 | claude –continue |
–dangerously-skip-permissions | 跳过权限提示(需谨慎使用) | claude –dangerously-skip-permissions |
–output-format json 参数特别适用于脚本和自动化,可用于程序化解析 Claude 的响应。
打印模式(Print Mode)详情
-p(或 –print)参数启用 Claude Code 的非交互模式,便于输入输出管道化处理,支持多种输出格式以满足不同需求。
基本用法
# 基本打印模式 - 只输出最终响应文本
claude -p "Explain how to use the print flag"
# 处理标准输入
echo "What is 2+2?" | claude -p
# 用提示词在打印模式下恢复会话
claude -p --resume <session-id> "Resume session with this prompt"
输出格式
–output-format 选项(与 -p 一起用)支持三种格式:
- 文本输出(默认)
claude -p "Explain the output formats"
# 仅输出响应文本
- JSON 输出
claude -p --output-format json "Explain how to use JSON output"
- 输出结构化的 JSON 对象:
{
"cost_usd": 0.003,
"duration_ms": 1234,
"duration_api_ms": 800,
"result": "The response text here...",
"session_id": "abc123"
}
- 流式 JSON 输出
claude -p --output-format stream-json "Create a Python script"
- 在流模式下,每条消息作为单独的 JSON 对象输出,包括工具调用、助手文本、工具返回、系统统计等。
打印模式下的详细输出
使用 –verbose 结合 -p 时,需配合 –output-format json 或 –output-format stream-json:
claude -p --verbose --output-format json "Debug this code"
详细 JSON 模式下,输出包括完整的对话记录:
[
{
"role": "user",
"content": "Debug this code"
},
{
"role": "assistant",
"content": "I'll help you debug that code..."
},
{
"role": "system",
"cost_usd": 0.003,
"duration_ms": 1234,
"duration_api_ms": 800,
"result": "The response text here...",
"session_id": "abc123"
}
]
打印模式下的其他选项
- Max Turns(最大回合数)
claude -p --max-turns 3 "Fix this code" < file.py
- 限制非交互模式下智能体的回合数。
- 权限提示工具
claude -p --permission-prompt-tool mcp_auth_tool "Create a file"
- 指定 MCP 工具处理权限请求。
- 恢复会话
claude -p --resume abc123 "Resume session with this prompt"
- 用新提示词在打印模式下按 ID 恢复特定会话。
- 继续会话
claude -c -p "Continue with this next task"
- 继续当前项目中的最后一次对话。
斜杠命令(Slash Commands)
在交互会话期间控制 Claude 的行为:
命令 | 用途 |
---|---|
/bug | 报告 Bug(将对话发送至 Anthropic) |
/clear | 清除对话历史 |
/compact [说明] | 压缩对话内容,可选指定关注点 |
/config | 查看/修改配置信息 |
/cost | 显示令牌消耗统计 |
/doctor | 检查 Claude Code 安装状态 |
/help | 获取使用帮助 |
/init | 用 CLAUDE.md 指南初始化项目 |
/login | 切换 Anthropic 账户 |
/logout | 注销 Anthropic 账户 |
/memory | 编辑 CLAUDE.md 记忆文件 |
/pr_comments | 查看 Pull Request 评论 |
/review | 请求代码审查 |
/status | 查看账户和系统状态 |
/terminal-setup | 安装 Shift+Enter 作为换行键绑定(仅限 iTerm2 和 VSCode) |
/vim | 进入 Vim 模式,切换插入/命令模式 |
特殊快捷方式
用 # 快速添加记忆
用 # 开头输入内容,可快速添加记忆:
# 始终使用描述性变量名
随后你将被提示选择要存储该记忆的文件。
终端中的换行
输入多行命令可用:
- 快速转义:输入 \ 后回车
- 键盘快捷键:Option+Enter(或配置后用 Shift+Enter)
设置 Option+Enter 作为换行的方法:
- Mac Terminal.app:
- 打开设置 → Profiles → Keyboard
- 勾选“Use Option as Meta Key”
- iTerm2 和 VSCode 终端:
- 打开设置 → Profiles → Keys
- General 下将左/右 Option 键设为“Esc+”
- 建议在 Claude Code 内运行 /terminal-setup 自动配置 Shift+Enter 作为更直观的换行
详细设置见终端设置页面。
Vim 模式
Claude Code 支持部分 Vim 快捷键,通过 /vim 或 /config 启用。
支持的 Vim 子集包括:
- 模式切换:Esc(切到 NORMAL),i/I, a/A, o/O(切到 INSERT)
- 导航:h/j/k/l,w/e/b,0/$/^,gg/G
- 编辑:x,dw/de/db/dd/D,cw/ce/cb/cc/C,.(重复上次操作)