通俗版 Claude Code 文档

结构照原 docs,内容改成真能照着做的人话版。

查看原始文档 目录顺序与官方保持一致

Automation

编程方式调用

编程方式调用 这一页讲的,就是 编程方式调用 这件事在 Claude Code 里到底怎么用。

页面信息

对应原页

编程方式调用

页面性质

第三方中文解释页

使用建议

先看人话解释,再对照原页命令和代码

这页不是官方原文,而是顺着官方文档结构做的中文解释版。命令、参数、配置名这些硬东西尽量保留,解释部分则尽量讲成人能照着做的话。

如果你碰到特别敏感的配置、权限或企业环境差异,最好顺手点上面的“查看原始文档”再核一遍。

这一页先讲明白

无头模式就是不进聊天界面,直接在脚本或 CI 里调用 Claude Code。

适合自动流程、批处理和流水线集成。

你可以把它当成“把帮工嵌进机器流程里”。人不一定站在旁边,但机器会按你定好的话把活派出去。

这种模式特别适合重复审查、批量解释、自动生成报告。

先挑一个低风险场景试,比如解释报错、生成摘要、审查改动。

自动流程里要特别注意输入范围和权限,别让它拿到超出预期的东西。

预留广告位

正文中段响应式广告 等你后面真接 AdSense,这里再放正式广告。

Documentation Index

这里不是让你背"Documentation Index"这个词,而是让你看它真干活时怎么使。

看这段时要特别盯工具和权限边界,别为了省事一把全开。

终端里敲

Documentation Index

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude -p "Find and fix the bug in auth.py" --allowedTools "Read,Edit,Bash"

Basic usage

看到这里,就把"Basic usage"当成一件真要上手的活来看。

看这段时要特别盯工具和权限边界,别为了省事一把全开。

终端里敲

Basic usage

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

claude -p "What does the auth module do?"

Start faster with bare mode

这里不是让你背"Start faster with bare mode"这个词,而是让你看它真干活时怎么使。

如果这里反复提 lead 和 teammate,通常是在提醒主代理别抢队友还没做完的活,先等人把结果交回来再继续统筹。

终端里敲

Start faster with bare mode

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude --bare -p "Summarize this file" --allowedTools "Read"

Examples

看到这里,就把"Examples"当成一件真要上手的活来看。

Pipe data through Claude

看到这里,就把"Pipe data through Claude"当成一件真要上手的活来看。

看这段时要特别盯工具和权限边界,别为了省事一把全开。

终端里敲

Pipe data through Claude

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

cat build-error.txt | claude -p 'concisely explain the root cause of this build error' > output.txt

Add Claude to a build script

这一段主要是在把"Add Claude to a build script"讲实,不是只摆个标题给你看。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

改配置

Add Claude to a build script

这会儿轮到改配置了,字段名和关键字别自己乱换。

{
  "scripts": {
    "lint:claude": "git diff main | claude -p \"you are a typo linter. for each typo in this diff, report filename:line on one line and the issue on the next. return nothing else.\""
  }
}

Get structured output

这一段是在说怎么用 --output-format 去做 control how responses are returned:。看这种内容,光知道名字没用,还是得落到手上。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

终端里敲

Get structured output 1

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude -p "Summarize this project" --output-format json
终端里敲

Get structured output 2

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude -p "Extract the main function names from auth.py" \
  --output-format json \
  --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}'
终端里敲

Get structured output 3

真到动手的时候了,下面这条直接敲一遍,看它回什么。

# Extract the text result
claude -p "Summarize this project" --output-format json | jq -r '.result'

# Extract structured output
claude -p "Extract function names from auth.py" \
  --output-format json \
  --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}' \
  | jq '.structured_output'

Stream responses

这一段是在说怎么用 --output-format stream-json with --verbose and --include-partial-messages 去做 receive tokens as they’re generated. Each line is a JSON object representing an event:。看这种内容,光知道名字没用,还是得落到手上。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

终端里敲

Stream responses 1

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude -p "Explain recursion" --output-format stream-json --verbose --include-partial-messages
终端里敲

Stream responses 2

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude -p "Write a poem" --output-format stream-json --verbose --include-partial-messages | \
  jq -rj 'select(.type == "stream_event" and .event.delta.type? == "text_delta") | .event.delta.text'

Auto-approve tools

这一段是在说怎么用 --allowedTools to let Claude use certain tools without prompting. This example runs a test suite and fixes failures, allowing Claude 去做 execute Bash commands and read/edit files without asking for permission:。看这种内容,光知道名字没用,还是得落到手上。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

终端里敲

Auto-approve tools 1

先别急着往下翻,下面这条命令跑完,心里才有底。

claude -p "Run the test suite and fix any failures" \
  --allowedTools "Bash,Read,Edit"
终端里敲

Auto-approve tools 2

先别急着往下翻,下面这条命令跑完,心里才有底。

claude -p "Apply the lint fixes" --permission-mode acceptEdits

Create a commit

这里主要是在交代"Create a commit"这一块会碰到哪些事。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

终端里敲

Create a commit

真到动手的时候了,下面这条直接敲一遍,看它回什么。

claude -p "Look at my staged changes and create an appropriate commit" \
  --allowedTools "Bash(git diff *),Bash(git log *),Bash(git status *),Bash(git commit *)"

Customize the system prompt

这一段是在说怎么用 --append-system-prompt to add instructions while keeping Claude Code’s default behavior. This example pipes a PR diff to Claude and instructs it 去做 review for security vulnerabilities:。看这种内容,光知道名字没用,还是得落到手上。

终端里敲

Customize the system prompt

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

gh pr diff "$1" | claude -p \
  --append-system-prompt "You are a security engineer. Review for vulnerabilities." \
  --output-format json

Continue conversations

这一段是在说怎么用 --continue to continue the most recent conversation, or --resume with a session ID 去做 continue a specific conversation. This example runs a review, then sends follow-up prompts:。看这种内容,光知道名字没用,还是得落到手上。

这里还牵扯作用域,意思就是这条规则到底管当前项目、你个人,还是只管这一趟会话。

终端里敲

Continue conversations 1

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

# First request
claude -p "Review this codebase for performance issues"

# Continue the most recent conversation
claude -p "Now focus on the database queries" --continue
claude -p "Generate a summary of all issues found" --continue
终端里敲

Continue conversations 2

这一段不是只让你理解意思,下面这条命令就是现在要跑的。

session_id=$(claude -p "Start a review" --output-format json | jq -r '.session_id')
claude -p "Continue that review" --resume "$session_id"

Next steps

这里不是让你背"Next steps"这个词,而是让你看它真干活时怎么使。

照着做一遍

第一次用无头模式,别先接进整条 CI 流水线。

先在本地命令行里跑一个最小示例,确认它会吐你想要的结果。

终端里敲

第 1 步:先跑一个最小问题

先问一个一眼能看出对不对的小问题。

claude -p "explain this function"
终端里敲

第 2 步:再试带文件的任务

让它真正碰一下项目里的文件,看输出是不是靠谱。

claude -p "summarize the changes in src/auth/login.ts"
终端里敲

第 3 步:要给别的程序吃时,再收紧格式

人看和机器看不是一回事,后面接程序时再固定结构。

claude -p "return JSON with keys summary and risk"
终端里敲

第 4 步:最后才接进自动流程

先本地确认稳定,再放进脚本或 CI。

./run-claude-check.sh

一眼看懂这一页

这页的作用,就是把原本偏专业的话题,拆成能直接照着走的明白话。

编程方式调用
   |
   v
编程方式调用 这一页讲的,就是 编程方式调用 这件事在 Claude Code 里到底怎么用。
   |
   v
照着步骤去做

文末提醒

这站会按官方 docs 的导航和内容变化继续重生成,原站加页、删页、改页时,这里会跟着更新。

人话解释会尽量顺着原页往下讲,但命令、参数名、配置名这些硬东西还是保留原样,免得你抄过去跑不起来。