CLI 参数用于从命令行自定义 Claude Code 行为,对于脚本、CI/CD 管道和自动化至关重要。
会话控制
参数
说明
示例
--continue, -c
恢复最近的对话
claude -c
--resume, -r
按 ID 或名称恢复会话
claude -r auth-refactor
--from-pr
恢复与 PR 关联的会话
claude --from-pr 123
--fork-session
恢复时分支到新会话
claude -r abc123 --fork-session
--session-id
使用特定会话 UUID
claude --session-id "550e8400-..."
--worktree, -w
在隔离的 git worktree 中启动
claude -w feature-auth
模型与行为
参数
说明
示例
--model
设置模型(sonnet、opus 或全名)
claude --model opus
--fallback-model
过载时自动回退(打印模式)
claude -p --fallback-model sonnet "query"
--permission-mode
以特定权限模式启动
claude --permission-mode plan
--agent
为会话指定代理
claude --agent my-custom-agent
--agents
通过 JSON 定义自定义子代理
见下文示例
打印模式(非交互)
打印模式(-p)是使用 Claude Code 进行脚本的关键。它发送查询并退出——无交互会话。
参数
说明
示例
--print, -p
非交互模式
claude -p "explain this codebase"
--output-format
输出格式:text、json、stream-json
claude -p --output-format json "query"
--json-schema
根据 JSON Schema 验证输出
claude -p --json-schema '{...}' "query"
--max-turns
限制代理轮次
claude -p --max-turns 3 "query"
--max-budget-usd
限制此次运行的花费
claude -p --max-budget-usd 5.00 "query"
--input-format
输入格式:text 或 stream-json
claude -p --input-format stream-json
脚本示例:
# 生成 API 文档并保存到文件claude -p "Generate API documentation for src/api/" --output-format text > api-docs.md# 获取 JSON 格式的结构化分析claude -p "Analyze the error handling in this project" --output-format json | jq '.result'# 限制昂贵操作的花费claude -p --max-budget-usd 2.00 --max-turns 5 "Refactor the auth module"# 管道内容进行分析cat error.log | claude -p "What caused this crash?"# CI/CD:检查代码质量并设置预算上限claude -p --max-budget-usd 1.00 --output-format json \"Review the last commit for security issues" | jq '.result'
权限与工具控制
参数
说明
示例
--allowedTools
无需权限提示即可运行的工具
--allowedTools "Bash(git log:*)" "Read"
--disallowedTools
完全阻止的工具
--disallowedTools "Bash(rm *)" "Edit"
--tools
限制可用的内置工具
--tools "Bash,Edit,Read"
--dangerously-skip-permissions
跳过所有权限提示(谨慎使用)
不推荐用于生产
--allowedTools 是更安全的替代方案:
# 只允许读取操作和 git 命令claude -p --allowedTools "Bash(git log:*)" "Bash(git diff:*)" "Read" "Grep" "Glob" \"Generate a code review for the last 3 commits"
系统提示自定义
参数
说明
用途
--system-prompt
替换整个系统提示
完全控制行为
--system-prompt-file
用文件内容替换
版本控制的提示
--append-system-prompt
添加到默认提示
保留默认功能同时添加规则
--append-system-prompt-file
追加文件内容
从文件加载规则
# 添加项目特定规则claude --append-system-prompt "Always use TypeScript. Never use any. Include JSDoc comments."# 从文件加载规则以保持团队一致性claude --append-system-prompt-file ./team-rules.txt
MCP 与扩展
参数
说明
示例
--mcp-config
从 JSON 文件加载 MCP 服务器
claude --mcp-config ./mcp.json
--strict-mcp-config
只使用 --mcp-config 中的服务器
claude --strict-mcp-config --mcp-config ./mcp.json
--chrome
启用 Chrome 浏览器集成
claude --chrome
--plugin-dir
从目录加载插件
claude --plugin-dir ./my-plugins
--add-dir
添加额外工作目录
claude --add-dir ../apps ../lib
其他有用参数
参数
说明
--verbose
详细日志,显示完整的轮次输出
--debug
启用调试模式,可按类别过滤:--debug "api,mcp"
--version, -v
显示版本号
--ide
启动时自动连接到 IDE
--init
启动时运行初始化钩子
--remote
在 claude.ai 上创建 Web 会话
--teleport
在本地终端中恢复 Web 会话
--disable-slash-commands
为此会话禁用所有技能和命令
--settings
从 JSON 文件加载额外设置
四、环境变量
环境变量在比设置或参数更深层级配置 Claude Code。在 shell 配置文件(~/.zshrc、~/.bashrc)或每个会话中设置。
认证
变量
用途
ANTHROPIC_API_KEY
Anthropic API 密钥。不设置则使用订阅认证
ANTHROPIC_AUTH_TOKEN
自定义 Authorization 头值(带 Bearer 前缀)
云服务商配置
变量
用途
CLAUDE_CODE_USE_BEDROCK
设为 1 通过 AWS Bedrock 路由
CLAUDE_CODE_USE_VERTEX
设为 1 通过 Google Vertex AI 路由
CLAUDE_CODE_USE_FOUNDRY
设为 1 通过 Microsoft Foundry 路由
模型配置
变量
用途
ANTHROPIC_MODEL
覆盖默认模型
ANTHROPIC_DEFAULT_SONNET_MODEL
默认 Sonnet 级模型 ID
ANTHROPIC_DEFAULT_OPUS_MODEL
默认 Opus 级模型 ID
ANTHROPIC_DEFAULT_HAIKU_MODEL
默认 Haiku 级模型 ID
CLAUDE_CODE_EFFORT_LEVEL
思考努力程度:low、medium、high
CLAUDE_CODE_MAX_OUTPUT_TOKENS
最大输出 token(默认 32,000;最大 64,000)
CLAUDE_CODE_SUBAGENT_MODEL
子代理使用的模型
Bash 执行
变量
用途
默认值
BASH_DEFAULT_TIMEOUT_MS
bash 命令的默认超时
可变
BASH_MAX_TIMEOUT_MS
模型可设置的最大超时
可变
BASH_MAX_OUTPUT_LENGTH
截断前的最大字符数
可变
CLAUDE_CODE_SHELL
覆盖自动 shell 检测
自动
CLAUDE_CODE_SHELL_PREFIX
包装所有 bash 命令的前缀
无
功能开关
变量
设为 1 的效果
CLAUDE_CODE_DISABLE_AUTO_MEMORY
禁用自动记忆
CLAUDE_CODE_DISABLE_BACKGROUND_TASKS
禁用后台任务功能
CLAUDE_CODE_DISABLE_FAST_MODE
禁用快速模式
CLAUDE_CODE_DISABLE_GIT_INSTRUCTIONS
从系统提示中移除 git 工作流指令
DISABLE_AUTOUPDATER
禁用自动更新
DISABLE_PROMPT_CACHING
禁用提示缓存
DISABLE_COST_WARNINGS
抑制费用警告消息
设置环境变量:
# 在 ~/.zshrc 或 ~/.bashrc 中export ANTHROPIC_API_KEY="sk-ant-..."export CLAUDE_CODE_EFFORT_LEVEL="high"export CLAUDE_CODE_MAX_OUTPUT_TOKENS=64000# 每个会话覆盖CLAUDE_CODE_EFFORT_LEVEL=low claude -p "Quick summary of this file"# AWS Bedrock 用户export CLAUDE_CODE_USE_BEDROCK=1export AWS_REGION=us-east-1
五、高级用户技巧
1. 链式命令提高上下文效率
不要发送多条消息,在请求前预先加载上下文:
# 不好:多轮对话,浪费 token"What does the auth module do?""Now add rate limiting to it""Now write tests for the rate limiting"# 好:单个提示包含完整上下文"Add rate limiting to the auth module in src/auth/ and write tests.Rate limit: 100 requests per minute per IP.Use the existing Redis connection for storage.Follow the test patterns in tests/auth/"
2. 使用 /cost 监控费用
养成在活跃会话中每 15-20 分钟检查 /cost 的习惯。如果费用上升比预期快:
运行 /compact 减少上下文大小
切换到 /model sonnet 处理不太复杂的子任务
将大任务分解为更小、更集中的会话
3. 策略性使用 /compact
/compact 不仅仅是上下文满时的应急按钮。主动使用:
# 完成子任务后,在开始下一个之前压缩/compact retain the API schema and test results, discard the debugging steps# 复杂操作前,腾出空间/compact keep only the project architecture and current task requirements
4. 脚本化重复工作流
结合 -p 模式与 shell 脚本来处理定期运行的任务:
#!/bin/bash# daily-review.sh -- 早晨代码审查例行程序echo "=== Reviewing yesterday's changes ==="claude -p --max-budget-usd 1.00 --output-format text \"Review all commits from the last 24 hours. Flag any security issues,missing tests, or code quality problems. Be concise." \--allowedTools "Bash(git log:*)" "Bash(git diff:*)" "Bash(git show:*)" "Read" "Grep"echo "=== Done ==="
5. 后台长时间运行的命令
当 Claude 运行慢命令(测试、构建、部署)时,按 Ctrl+B 将其发送到后台。你可以在命令完成时继续处理你的提示。
6. 复杂提示使用 Vim 模式
如果你编写长而详细的提示,用 /vim 启用 Vim 模式。你可以获得完整的 Vim 导航和编辑功能。
7. 为专门任务定义子代理
使用 --agents 参数创建 Claude 可以委托的专注子代理:
claude --agents '{ "test-writer": { "description": "Writes comprehensive tests. Use after any code change.", "prompt": "You write thorough tests. Cover happy path, edge cases, and error conditions.", "tools": ["Read", "Edit", "Bash"], "model": "sonnet" }, "security-reviewer": { "description": "Reviews code for security vulnerabilities.", "prompt": "You are a security expert. Find injection, auth, and data exposure risks.", "tools": ["Read", "Grep", "Glob"], "model": "opus" }}'