Codex: أداة سطر الأوامر Codex

آخر تحديث: 2026-08-31

Codex CLI هو الطريقة الأكثر شعبية بين المطورين. يغطي هذا الدرس الاستخدام المتقدم لـ CLI والإعداد.

📋 المتطلبات المسبقة: CLI مثبت (راجع الدرس 2)، معرفة أساسية بالأوامر

1. ما ستتعلمه


2. معلمات CLI المتقدمة

(1) قائمة المعلمات الكاملة

المعلمة الوصف المثال
--model تحديد النموذج --model o3
--sandbox وضع 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 prompt نظام مخصص --system-prompt "You are..."
--max-tokens أقصى عدد token للإخراج --max-tokens 4096
--temperature معلمة الحرارة --temperature 0.2

(2) مجموعات شائعة

BASH
# Read-only code review
codex --sandbox readonly "Review code security"

# Auto-fix lint errors
codex --full-auto --approval-policy approve "Fix all lint errors"

# Specific model + quiet mode
codex --model o3 --quiet "Refactor auth module"

# Custom system prompt
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: إعداد CLI لأليس

TOML
# Alice's global config
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
# Pass task directly, exits after execution
codex --quiet "Add type annotations to app.py"

# Output to file
codex --quiet "Generate API documentation" > api-docs.md

(2) إدخال الأنابيب

BASH
# Read from stdin
echo "Explain this code" | codex --quiet

# Read from file
cat error.log | codex --quiet "Analyze error log, find root cause"

# Combine with git
git diff | codex --quiet "Review these changes"

(3) إخراج JSON

BASH
# JSON format output, suitable for programmatic parsing
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: auto-fix 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: سير العمل المؤتمت لبوب

BASH
# Bob's daily-dev.sh script
#!/bin/bash

# Pull latest code
git pull origin main

# Auto-fix lint
codex --full-auto --quiet "Fix all lint errors"

# Generate missing tests
codex --full-auto --quiet "Generate unit tests for source files without tests"

# Run tests
npm test

# If all pass, commit
if [ $? -eq 0 ]; then
  git add -A
  git commit -m "chore: auto lint-fix and test generation"
fi

6. التنقيح وتسجيل الدخول

BASH
# Verbose output mode
codex --verbose "Fix bug"

# Save log
codex --log-file codex.log "Fix bug"

# View audit records
cat ~/.codex/audit.log

❓ أسئلة شائعة

س هل يمكن تشغيل CLI و App في نفس الوقت؟
ج نعم، لكن غير موصى به على نفس المشروع في نفس الوقت. مشاريع مختلفة يمكن استخدامها بالتوازي.
س هل يدعم الوضع غير التفاعلي جميع الميزات؟
ج معظم الميزات مدعومة، لكن ميزات التأكيد التفاعلي مثل Computer Use محدودة في الوضع غير التفاعلي.
س كيف أقيّد وقت تنفيذ CLI؟
ج استخدم أمر timeout الخاص بالنظام: timeout 300 codex --quiet "Fix bug".
س ما صيغة إخراج JSON الكاملة؟
ج تتضمن حقول task، status، files_modified، files_read، result. شغّل codex --json --help لرؤية المخطط الكامل.
س هل يمكن لـ Codex تعديل الملفات في وضع الأنابيب؟
ج نعم، لكن يتطلب --approval-policy approve أو --full-auto. وضع ask الافتراضي سيتوقف بانتظار التأكيد في الأنابيب.

📖 ملخص


📝 تمارين

  1. أساسي (⭐): استخدم الوضع غير التفاعلي لـ CLI لمراجعة كود، وأخرِج النتائج إلى ملف.
  2. متوسط (⭐⭐): أنشئ Makefile يدمج مهام Codex الشائعة.
  3. متقدم (⭐⭐⭐): اكتب Git Hook يجعل Codex يراجع جودة التغييرات تلقائيًا قبل كل التزام.
Web-Tutorial.com

فريق Web-Tutorial التقني

منصة دروس برمجية يديرها عدة مطورين. كل درس يتم كتابته ومراجعته بواسطة مطورين متخصصين في المجال. نعمل على ضمان دقة وموثوقية المحتوى — إذا لاحظت أي مشكلة، فيرجى إخبارنا.

100%