โจ๏ธ OpenAI Codex CLI
Practical guide ยท software engineering assistant
๐ Introduction
This document provides a hands-on introduction to using OpenAI Codex CLI in a software development environment. The goal is to help developers understand how Codex can analyze code, generate code, perform code reviews, refactor applications, identify bugs, and assist with project planning.
This guide assumes that:
- Codex CLI is already installed.
- OpenAI API authentication is working correctly.
- The user can successfully start Codex from the terminal.
๐ Chapter 1: Verifying the Installation
Before using Codex, verify that the installation is working correctly.
Check the Installed Version
codex --versionExplanationcodex launches the Codex CLI executable. --version requests the currently installed version. This command confirms that Codex is installed and accessible through the system PATH.
Example Output:
codex-cli 0.135.0
A successful output indicates that the installation is functioning properly.
๐ Chapter 2: Creating a Test Workspace
Create a dedicated workspace for experimentation.
mkdir -p ~/workspace/test-codex
cd ~/workspace/test-codex
Explanationmkdir -p creates parent directories if they do not exist. cd changes the current working directory.
๐ Chapter 3: Understanding Existing Code
Create a simple Python program.
cat > calculator.py << 'EOF'
def add(a, b):
return a + b
print(add(10, 20))
EOF
Then launch Codex: codex and ask: "Explain calculator.py line by line." Expected result: explanation of function, parameters, return, call.
๐ Chapter 4: Refactoring Code
Prompt: "Refactor calculator.py into a proper command line calculator supporting add, subtract, multiply and divide." Codex improves structure, adds operations, validation.
๐ Chapter 5: Feature Enhancement
Create a task manager (todo.py). Then prompt: "Improve this application: save tasks to JSON, load tasks on startup, support delete task, support list task." Codex adds persistence and commands.
cat > todo.py << 'EOF'
tasks = []
while True:
task = input("Task: ")
if task == "exit": break
tasks.append(task)
print(tasks)
EOF
๐ Chapter 6: Bug Detection
Create buggy file bug.py with index out of range. Prompt: "Find and fix all bugs in bug.py." Codex identifies IndexError and suggests safe access.
numbers = [1,2,3,4,5]
print(numbers[10]) # IndexError
๐ Chapter 7: Automated Code Review
Move into existing project cd path/to/project โ codex โ prompt: "Review this repository and identify: 1. Security issues 2. Performance issues 3. Code quality issues 4. Maintainability concerns."
๐ Chapter 8: Generating Unit Tests
Prompt: "Create pytest unit tests for all Python modules in this project." Codex analyzes modules and generates test files.
๐ Chapter 9: Architecture Analysis
Powerful use case โ Prompt: "Analyze this repository and explain: 1. Architecture 2. Main components 3. Data flow 4. Security risks 5. Performance bottlenecks."
๐ Chapter 10: OCR Project Improvement Example
For an OCR repository: "Analyze all OCR preprocessing steps and propose an implementation plan to improve OCR confidence from 30% to above 70%. Provide: 1. Architecture 2. File changes 3. New dependencies 4. Docker changes 5. Step-by-step implementation plan." This workflow delivers senior architect level insights.
โ๏ธ Recommended Daily Workflow
- Pull latest source code.
- Start Codex inside the repository.
- Ask Codex to analyze recent changes.
- Request bug detection.
- Generate tests & perform security review.
- Refactor problematic modules.
- Generate implementation plans.
- Review results before applying changes.
- Commit verified changes to Git.
๐ Conclusion
OpenAI Codex CLI is most effective when used as an AI software engineering assistant rather than a simple code generator. Its greatest value comes from repository understanding, architecture analysis, code review, automated testing, refactoring, debugging, technical planning, and large-scale project assistance.
๐ Pengantar
Dokumen ini memberikan pengantar praktis menggunakan OpenAI Codex CLI dalam lingkungan pengembangan perangkat lunak. Tujuannya membantu developer memahami bagaimana Codex dapat menganalisis kode, menghasilkan kode, melakukan peninjauan kode, refaktor aplikasi, mengidentifikasi bug, dan membantu perencanaan proyek.
Panduan ini mengasumsikan:
- Codex CLI sudah terinstal.
- Otentikasi OpenAI API berfungsi dengan benar.
- Pengguna dapat menjalankan Codex dari terminal.
๐ Bab 1: Memverifikasi Instalasi
Sebelum menggunakan Codex, pastikan instalasi berfungsi.
Periksa Versi Terinstal
codex --versionPenjelasancodex menjalankan eksekutabel CLI Codex. --version menampilkan versi terinstal. Perintah ini mengonfirmasi bahwa Codex terinstal dan dapat diakses melalui PATH sistem.
Contoh Keluaran:
codex-cli 0.135.0
Keluaran sukses menandakan instalasi berfungsi dengan baik.
๐ Bab 2: Membuat Ruang Kerja Uji Coba
Buat ruang kerja khusus untuk eksperimen.
mkdir -p ~/workspace/test-codex
cd ~/workspace/test-codex
Penjelasanmkdir -p membuat direktori induk jika belum ada. cd mengubah direktori kerja saat ini.
๐ Bab 3: Memahami Kode yang Ada
Buat program Python sederhana.
cat > calculator.py << 'EOF'
def add(a, b):
return a + b
print(add(10, 20))
EOF
Jalankan Codex: codex dan tanyakan: "Jelaskan calculator.py baris per baris." Hasil: penjelasan fungsi, parameter, return, pemanggilan.
๐ Bab 4: Refaktor Kode
Perintah: "Refaktor calculator.py menjadi kalkulator baris perintah yang mendukung penjumlahan, pengurangan, perkalian, dan pembagian." Codex meningkatkan struktur, menambah operasi, dan validasi.
๐ Bab 5: Peningkatan Fitur
Buat pengelola tugas (todo.py). Prompt: "Tingkatkan aplikasi ini: simpan tugas ke JSON, muat saat startup, dukungan hapus tugas, dukung daftar tugas." Codex menambah penyimpanan persisten dan perintah.
cat > todo.py << 'EOF'
tasks = []
while True:
task = input("Tugas: ")
if task == "keluar": break
tasks.append(task)
print(tasks)
EOF
๐ Bab 6: Deteksi Bug
Buat file bug.py dengan bug sengaja (indeks di luar jangkauan). Prompt: "Temukan dan perbaiki semua bug di bug.py." Codex mengidentifikasi IndexError dan menyarankan akses aman.
numbers = [1,2,3,4,5]
print(numbers[10]) # IndexError
๐ Bab 7: Peninjauan Kode Otomatis
Masuk ke proyek yang sudah ada cd path/to/project โ codex โ prompt: "Tinjau repositori ini dan identifikasi: 1. Masalah keamanan 2. Masalah kinerja 3. Masalah kualitas kode 4. Masalah pemeliharaan."
๐ Bab 8: Menghasilkan Unit Test
Prompt: "Buat unit test pytest untuk semua modul Python di proyek ini." Codex menganalisis modul dan menghasilkan file test.
๐ Bab 9: Analisis Arsitektur
Kasus penggunaan canggih โ Prompt: "Analisis repositori ini dan jelaskan: 1. Arsitektur 2. Komponen utama 3. Aliran data 4. Risiko keamanan 5. Hambatan kinerja."
๐ Bab 10: Contoh Peningkatan Proyek OCR
Untuk repositori OCR: "Analisis semua langkah prapemrosesan OCR dan usulkan rencana implementasi untuk meningkatkan keyakinan OCR dari 30% menjadi di atas 70%. Berikan: 1. Arsitektur 2. Perubahan berkas 3. Dependensi baru 4. Perubahan Docker 5. Rencana implementasi langkah demi langkah." Alur kerja ini memberikan wawasan level arsitek senior.
โ๏ธ Alur Kerja Harian yang Direkomendasikan
- Tarik kode sumber terbaru.
- Jalankan Codex di dalam repositori.
- Minta Codex menganalisis perubahan terbaru.
- Minta deteksi bug.
- Hasilkan test & lakukan peninjauan keamanan.
- Refaktor modul bermasalah.
- Hasilkan rencana implementasi.
- Tinjau hasil sebelum menerapkan perubahan.
- Commit perubahan terverifikasi ke Git.
๐ Kesimpulan
OpenAI Codex CLI paling efektif digunakan sebagai asisten rekayasa perangkat lunak AI, bukan sekadar generator kode. Nilai terbesarnya berasal dari pemahaman repositori, analisis arsitektur, peninjauan kode, pengujian otomatis, refaktorisasi, debugging, perencanaan teknis, dan bantuan proyek skala besar.
Comments