Codex: Codex コマンドラインツール
最終更新:2026-08-31
Codex CLI は開発者の間で最も人気のある方法です。このレッスンでは高度な CLI 使用方法と設定を説明します。
📋 前提条件: CLI がインストール済み(レッスン2を参照)、基本的なコマンド知識
1. 学ぶ内容
- CLI 高度パラメータ
- CLI 設定ファイル
- 出力フォーマット制御
- 他のツールとの統合
2. CLI 高度パラメータ
(1) 完全なパラメータリスト
| パラメータ | 説明 | 例 |
|---|---|---|
--model |
モデルを指定 | --model o3 |
--sandbox |
サンドボックスモード | --sandbox readonly |
--approval-policy |
承認ポリシー | --approval-policy approve |
--auto-edit |
Auto Edit モード | — |
--full-auto |
Full Auto モード | — |
--quiet |
静寂モード、出力を減らす | — |
--json |
JSON フォーマット出力 | — |
--context |
コンテキストファイルを指定 | --context AGENTS.md |
--system-prompt |
カスタムシステムプロンプト | --system-prompt "You are..." |
--max-tokens |
最大出力トークン数 | --max-tokens 4096 |
--temperature |
temperature パラメータ | --temperature 0.2 |
(2) 一般的な組み合わせ
BASH
# 読み取り専用コードレビュー
codex --sandbox readonly "Review code security"
# lint エラーの自動修正
codex --full-auto --approval-policy approve "Fix all lint errors"
# 特定のモデル + 静寂モード
codex --model o3 --quiet "Refactor auth module"
# カスタムシステムプロンプト
codex --system-prompt "You are a Rust expert, follow Rust API Guidelines" "Optimize algorithm performance"
3. CLI 設定ファイル
(1) グローバル設定
TOML
# ~/.codex/config.toml
model = "gpt-5-codex"
approval_policy = "ask"
sandbox_mode = "workspace-write"
[output]
format = "text" # text / json / markdown
color = true
progress = true
[context]
auto_compact = true
max_files = 50
exclude = ["node_modules/", ".git/", "dist/"]
(2) プロジェクト設定
TOML
# .codex/config.toml
model = "deepseek-coder"
approval_policy = "approve"
[context]
include = ["src/**/*.ts", "tests/**/*.ts"]
▶ 例1:Alice の CLI 設定
TOML
# Alice のグローバル設定
model = "gpt-5-codex"
approval_policy = "ask"
[output]
format = "text"
color = true
[context]
auto_compact = true
exclude = ["node_modules/", ".next/", "dist/", "coverage/"]
[sandbox]
mode = "workspace-write"
blocked_paths = [".env", ".env.local", "secrets/"]
4. 非インタラクティブモード
Codex CLI は非インタラクティブモードをサポートし、スクリプト統合に最適です:
(1) 単一実行
BASH
# タスクを直接渡し、実行後に終了
codex --quiet "Add type annotations to app.py"
# ファイルに出力
codex --quiet "Generate API documentation" > api-docs.md
(2) パイプ入力
BASH
# stdin から読み取り
echo "Explain this code" | codex --quiet
# ファイルから読み取り
cat error.log | codex --quiet "Analyze error log, find root cause"
# git と組み合わせ
git diff | codex --quiet "Review these changes"
(3) JSON 出力
BASH
# JSON フォーマット出力、プログラム的解析に適している
codex --json "List all TODO comments" > todos.json
JSON 出力例:
JSON
{
"task": "List all TODO comments",
"status": "completed",
"files_modified": [],
"files_read": ["src/main.ts", "src/utils.ts"],
"result": "Found 5 TODO comments:\n- src/main.ts:12\n- src/utils.ts:45\n..."
}
5. 他のツールとの統合
(1) Git Hooks
BASH
# pre-commit hook:lint を自動修正
# .git/hooks/pre-commit
#!/bin/bash
codex --full-auto --quiet "Fix all lint errors"
git add -A
(2) CI/CD 統合
YAML
# GitHub Actions
- name: Code Review
run: |
codex --sandbox readonly --quiet "Review PR changes, focusing on security and performance"
(3) Makefile 統合
MAKEFILE
# Makefile
lint-fix:
codex --full-auto "Fix all lint errors"
test-gen:
codex --full-auto "Generate unit tests for all files under src/"
review:
codex --sandbox readonly "Review current changes"
▶ 例2:Bob の自動化ワークフロー
BASH
# Bob の daily-dev.sh スクリプト
#!/bin/bash
# 最新コードをプル
git pull origin main
# lint を自動修正
codex --full-auto --quiet "Fix all lint errors"
# 不足しているテストを生成
codex --full-auto --quiet "Generate unit tests for source files without tests"
# テストを実行
npm test
# すべて通ったらコミット
if [ $? -eq 0 ]; then
git add -A
git commit -m "chore: auto lint-fix and test generation"
fi
6. デバッグとログ
BASH
# 詳細出力モード
codex --verbose "Fix bug"
# ログを保存
codex --log-file codex.log "Fix bug"
# 監査記録を表示
cat ~/.codex/audit.log
❓ よくある質問
Q CLI と App を同時に実行できますか?
A はい、同じプロジェクトでの同時使用は推奨されません。異なるプロジェクトなら並列使用可能です。
Q 非インタラクティブモードはすべての機能をサポートしていますか?
A ほとんどの機能はサポートされていますが、Computer Use のようなインタラクティブ確認機能は非インタラクティブモードでは制限されます。
Q CLI の実行時間を制限するにはどうすればよいですか?
A システムの timeout コマンドを使用:
timeout 300 codex --quiet "Fix bug"。Q 完全な JSON 出力フォーマットは何ですか?
A task、status、files_modified、files_read、result フィールドが含まれます。
codex --json --help で完全なスキーマを確認できます。Q パイプモードで Codex はファイルを変更できますか?
A はい、
--approval-policy approve または --full-auto モードが必要です。デフォルトの ask モードではパイプ内で確認待ちでハングします。📖 まとめ
- CLI 高度パラメータ:
--model/--sandbox/--quiet/--json - 非インタラクティブモード:単一実行、パイプ入力、JSON 出力
- 統合シナリオ:Git Hooks、CI/CD、Makefile
- 設定優先順位:CLI 引数 > 環境変数 > プロジェクト > グローバル
📝 練習問題
- 基本 (⭐):CLI 非インタラクティブモードでコードレビューを行い、ファイルに出力する。
- 中級 (⭐⭐):一般的な Codex タスクを統合した Makefile を作成する。
- 上級 (⭐⭐⭐):各コミット前に Codex が変更品質を自動レビューする Git Hook を作成する。