Files
nexus/wiki/concepts/ClaudeCodePrintMode.md
2026-04-27 12:03:03 +08:00

55 lines
1.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Claude Code Print Mode"
type: concept
tags: [claude-code, hermes, ai-agent, terminal, non-interactive]
last_updated: 2026-04-27
---
## Aliases
- Print Mode
- claude -p
## Definition
Claude Code 的非交互单次执行模式。通过 `claude -p print` 命令调用,配合 stdin 传递任务描述文本,适合绝大多数自动化场景和 Hermes Agent 的程序化调用。
## How It Works
```bash
cat << 'TASK_END' | claude -p print \
--dangerously-skip-permissions \
--add-dir ~/.claude/skills/[技能名] \
--add-dir [项目源码路径] \
--add-dir /tmp \
--max-turns 30 \
2>&1
[具体任务描述]
TASK_END
```
## Key Parameters
- `--dangerously-skip-permissions`:跳过交互确认(信任目录、权限提示)
- `--add-dir <路径>`:添加可访问目录,可多次使用
- `--max-turns N`:最大迭代次数,建议 20-30
- `--bare`:跳过插件/MCP/CLAUDE.md 加载,最快启动
- `--model <模型>`:指定模型,如 `sonnet``opus`
- `--output-format json`:结构化 JSON 输出
## Task Text Structurestdin 写法规范)
```
1. 告诉 Claude Code 要做什么
2. 告诉它用哪个 skill
3. 告诉它目标文件输出到哪里
4. 如果需要格式转换(如 SVG → PNG明确写出转换命令
5. 最后要求它输出 "done: 文件路径"
```
## Relationship to TMUX Mode
- Print Mode适合绝大多数自动化场景单次执行无需后续交互
- TMUX 交互模式:适合超长任务或需要中途干预的场景
## Sources
- [[Claude Code 调用方法总结]]
## Connections
- [[ClaudeCodePrintMode]] ← 推荐使用 ← [[Claude Code]]
- [[ClaudeCodePrintMode]] ← 对比 ← [[ClaudeCodeTerminalIntegration]]