メインコンテンツへスキップ
モジュール 3:実アーキテクチャ 1 / 6
上級 S13 Claude Code CLI Print Mode Structured Output Automation

CLI コントロールサーフェス:Print Mode、JSON、Sessions

Print mode、構造化出力、turn 上限、権限モード、再開可能 session を使い、Claude Code を境界付き CLI process として運用する。

2026年3月20日 12 分で読む
確認済み カリキュラム確認日: 2026年10月21日

学ぶこと

Claude Code は対話型 TUI だけではありません。リリース済み CLI は、一回の 境界付きタスク、machine-readable 出力、JSON Schema、agent turn 上限、 permission mode、保存 session の再開を提供します。

この記事は Claude Code 2.1.305 の command contract で検証済みです。重要な automation では claude --version と最新 CLI reference を再確認してください。 claude --help には文書化されたすべての flag が載るわけではありません。たとえば --max-turns は CLI reference にのみ記載されています。

-p は --print の短縮形です。Claude は非対話で実行し、結果を出力して 終了します。

claude -p "Summarize the changed files and report which tests ran."

別 CLI の -p を転用しないでください。Codex の -p は configuration profile、Grok Build の -p は single-turn prompt です。

出力契約を選ぶ

人が読むなら text、一つの構造化結果なら JSON、incremental events が必要なら stream JSON を使います。

claude -p --output-format text "Explain the failing test."
claude -p --output-format json "Return the repository status."
claude -p --output-format stream-json --verbose --include-partial-messages \
  "Report progress while reviewing this repository."

JSON の結果では応答テキストが result に入り、session_id などの metadata も 含まれます。文書化されたストリーミング形式では stream-json に --verbose を 組み合わせ、--include-partial-messages で token 単位の events が加わります。

出力は untrusted data です。parse と shape validation を行い、shell command 生成から分離してください。

JSON Schema を要求する

--json-schema は print-mode 出力を schema で検証します。--output-format json と組み合わせると、検証済みのオブジェクトが structured_output フィールドに 入ります。shell の分割や展開を防ぐため、schema 全体を quote します。

claude -p --output-format json \
  --json-schema '{"type":"object","properties":{"summary":{"type":"string"},"tests":{"type":"array","items":{"type":"string"}}},"required":["summary","tests"],"additionalProperties":false}' \
  "Review the current diff and return the requested fields." \
  | jq '.structured_output'

Claude がリクエストを断った場合など、有効な構造化出力なしで実行が終わると、 応答に structured_output フィールドはありません。使う前にフィールドの存在を 確認してください。Schema は形を検証しますが、真実性は保証しません。呼び出し側が files、tests、external state を検証します。

Authority と作業量を制限する

Turn 上限と permission mode は別々の control です。

claude -p \
  --max-turns 6 \
  --permission-mode plan \
  "Map the implementation path and risks without editing."

--max-turns は agent loop を制限し、上限に達するとエラーで終了します。 --permission-mode は tool authority を決めます。どちらも OS sandbox、 repository isolation、destructive command review の代わりにはなりません。

flag も permissions.defaultMode もモードを指定していない場合、v2.1.302 以降の claude -p は Manual モード(設定値 default)で起動します。承認が必要な呼び出しは、 --allowedTools "Bash(npm test *)" のような allow rule、hook、 --permission-prompt-tool が承認しない限り拒否されます。タスクに必要な最小限だけを 許可するか、auto や dontAsk などのモードを意図的に選んでください。

Agent SDK の canUseTool callback や --permission-prompt-tool などの permission host があっても誰も応答しない場合は、--permission-prompts none(v2.1.259 以降)を 付けます。これらのプロンプトは待機されず、拒否されます。

Session を明示的に続ける

再現性が必要なら session ID を指定します。ID は JSON 出力から取得できます。

session_id=$(claude -p --output-format json "Start the review." | jq -r '.session_id')
claude -p --resume "$session_id" \
  "Continue from the saved context and re-check the current worktree first."

--continue は current directory の最新 conversation を選び、便利ですが曖昧です。

claude -p --continue "Continue only if the repository state still matches."

保存 context は古い可能性があります。再開ごとに instructions、permissions、 branch、diff、test state を確認してください。

Production Checklist

  • Claude Code version を固定して記録する。
  • -p は Claude print mode としてのみ解釈する。
  • Output format を選び、machine-readable output を検証する。
  • Downstream が fields に依存するなら JSON Schema を使い、structured_output の存在を確認する。
  • Turns を制限し、デフォルトに頼らず意図した permission mode を選ぶ。
  • -p は信頼できる repository でのみ実行する。workspace trust ダイアログを省略し、project hooks と .mcp.json servers も実行するためです。自分で書いていないコードには --bare で起動します。その場合、Anthropic API では ANTHROPIC_API_KEY または --settings で渡した apiKeyHelper だけで認証します。
  • Session 再開後に worktree を再検証する。
  • Model output を直接 shell へ埋め込まない。

次へ

Session 14 では、この境界付き CLI workflow を MCP server に接続し、server trust と tool authority を明示的に保ちます。