ogt-cli-opencode
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseOpenCode CLI Skill
OpenCode CLI 技能
Run OpenCode CLI to leverage fast code generation and analysis tools directly from the terminal.
运行OpenCode CLI,直接在终端中使用快速代码生成与分析工具。
When to Use
适用场景
- Rapid code generation and scaffolding
- Quick refactoring tasks
- Test file generation
- Code quality analysis
- API integration generation
- Boilerplate and template creation
- Fast prototyping tasks
- 快速代码生成与脚手架搭建
- 快速重构任务
- 测试文件生成
- 代码质量分析
- API集成代码生成
- 样板代码与模板创建
- 快速原型开发任务
Quick Start
快速开始
Interactive mode
交互模式
bash
opencodebash
opencodeOne-shot generation
一次性生成
bash
opencode generate "Create a function that validates user emails"bash
opencode generate "Create a function that validates user emails"With specific template
使用指定模板
bash
opencode generate --template react-component "Button with loading state"bash
opencode generate --template react-component "Button with loading state"Analyze code
代码分析
bash
opencode analyze myfile.jsbash
opencode analyze myfile.jsGenerate tests
生成测试用例
bash
opencode test "src/utils/auth.js"bash
opencode test "src/utils/auth.js"Installation
安装步骤
bash
undefinedbash
undefinednpm (recommended)
npm(推荐方式)
npm install -g opencode-cli
npm install -g opencode-cli
Verify installation
验证安装
opencode --version
undefinedopencode --version
undefinedKey Commands
核心命令
| Command | Description |
|---|---|
| Interactive mode |
| Generate code from description |
| Analyze code quality and structure |
| Generate test cases for file |
| Suggest refactoring improvements |
| Generate documentation |
| 命令 | 说明 |
|---|---|
| 交互模式 |
| 根据描述生成代码 |
| 分析代码质量与结构 |
| 为文件生成测试用例 |
| 提供重构优化建议 |
| 生成文档 |
Key Options
核心选项
| Option | Description |
|---|---|
| Use specific code template |
| Target language (js, ts, python, go, etc.) |
| Output file path |
| Output format (default, json, etc.) |
| Model variant (default, fast, quality) |
| 选项 | 说明 |
|---|---|
| 使用指定代码模板 |
| 目标语言(js、ts、python、go等) |
| 输出文件路径 |
| 输出格式(默认、json等) |
| 模型变体(default、fast、quality) |
Authentication
认证方式
OpenCode CLI uses OAuth:
bash
undefinedOpenCode CLI采用OAuth认证:
bash
undefinedFirst-time authentication
首次认证
opencode auth login
opencode auth login
Check auth status
检查认证状态
opencode auth status
No API key configuration needed—authentication is OAuth-based.opencode auth status
无需配置API密钥——认证基于OAuth实现。Working with Files
文件操作
OpenCode can read and generate code for files in your project:
bash
undefinedOpenCode可读取并为项目中的文件生成代码:
bash
undefinedAnalyze a file
分析文件
opencode analyze src/api.ts
opencode analyze src/api.ts
Generate tests for existing code
为现有代码生成测试用例
opencode test src/utils/helpers.js
opencode test src/utils/helpers.js
Refactor code
重构代码
opencode refactor src/components/Form.jsx
opencode refactor src/components/Form.jsx
Generate documentation
生成文档
opencode document src/services/auth.js
undefinedopencode document src/services/auth.js
undefinedCode Templates
代码模板
OpenCode includes templates for common patterns:
bash
undefinedOpenCode内置了常见模式的模板:
bash
undefinedList available templates
列出可用模板
opencode templates list
opencode templates list
Use a specific template
使用指定模板
opencode generate --template express-api "User management API"
opencode generate --template react-form "Login form"
opencode generate --template node-cli "CLI tool for backups"
undefinedopencode generate --template express-api "User management API"
opencode generate --template react-form "Login form"
opencode generate --template node-cli "CLI tool for backups"
undefinedBuilt-in Capabilities
内置功能
- Template Library: Pre-configured patterns for common use cases
- Multi-language Support: JavaScript, TypeScript, Python, Go, Rust, etc.
- Test Generation: Automatic unit and integration test creation
- Code Analysis: Quality metrics and improvement suggestions
- Refactoring: Smart refactoring recommendations
- 模板库: 针对常见场景预配置的代码模式
- 多语言支持: JavaScript、TypeScript、Python、Go、Rust等
- 测试生成: 自动创建单元测试与集成测试
- 代码分析: 质量指标与优化建议
- 代码重构: 智能重构建议
For Sub-Agent Delegation
子代理委托场景
When spawning OpenCode CLI for background work:
bash
undefined当启动OpenCode CLI执行后台任务时:
bash
undefinedGenerate code non-interactively
非交互式生成代码
opencode generate "Express API with validation" 2>&1
opencode generate "Express API with validation" 2>&1
Analyze code and capture output
分析代码并捕获输出
opencode analyze myfile.js --format json > analysis.json
opencode analyze myfile.js --format json > analysis.json
Generate tests
生成测试用例
opencode test src/utils.js --output test/utils.test.js 2>&1
opencode test src/utils.js --output test/utils.test.js 2>&1
Run with timeout
设置超时时间运行
timeout 180 opencode generate "Database schema for e-commerce" 2>&1
undefinedtimeout 180 opencode generate "Database schema for e-commerce" 2>&1
undefinedScript: Run OpenCode Task
脚本:运行OpenCode任务
Use the bundled script for reliable sub-agent execution:
bash
node {baseDir}/scripts/run-opencode-task.cjs "Your task prompt" [options]Options:
- — Action:
--action <action>,generate,analyze,test,refactor(default: generate)document - — File path for non-generation actions
--file <path> - — Template to use for generation
--template <name> - — Programming language
--language <lang> - — Timeout in seconds (default: 180)
--timeout <secs> - — Output file path
--output <path> - — Working directory
--workdir <path>
The script handles:
- Timeout protection
- Error capture and formatting
- Clean output for parsing
- Exit code propagation
使用内置脚本实现可靠的子代理执行:
bash
node {baseDir}/scripts/run-opencode-task.cjs "Your task prompt" [options]选项:
- — 操作类型:
--action <action>、generate、analyze、test、refactor(默认值:generate)document - — 非生成类操作的文件路径
--file <path> - — 生成代码时使用的模板
--template <name> - — 编程语言
--language <lang> - — 超时时间(秒,默认值:180)
--timeout <secs> - — 输出文件路径
--output <path> - — 工作目录
--workdir <path>
该脚本支持:
- 超时保护
- 错误捕获与格式化
- 便于解析的整洁输出
- 退出码传递
Model Selection
模型选择
OpenCode offers different model variants:
| Variant | Best For |
|---|---|
| Quick generation, simple tasks |
| Balanced speed and quality (recommended) |
| Complex code, high-quality output |
bash
opencode generate --model quality "Complex state management system"OpenCode提供不同的模型变体:
| 变体 | 最佳适用场景 |
|---|---|
| 快速生成、简单任务 |
| 速度与质量平衡(推荐) |
| 复杂代码、高质量输出 |
bash
opencode generate --model quality "Complex state management system"Tips
使用技巧
- Be specific with descriptions — Better prompts yield better code
- Use templates for consistent patterns in your codebase
- Review generated code — Always review and test generated code
- Combine with analysis — Analyze before refactoring
- Set timeouts for long-running generation tasks
- 描述要具体 —— 更清晰的提示会生成更优质的代码
- 使用模板 —— 确保代码库中的模式一致性
- 审查生成的代码 —— 务必审查并测试生成的代码
- 结合分析功能 —— 重构前先进行代码分析
- 设置超时时间 —— 针对耗时较长的生成任务
Rate Limits
速率限制
Rate limits depend on your OpenCode plan:
- Free tier: Limited generations per day
- Pro tier: Higher rate limits and priority
- Team tier: Custom limits with team management
速率限制取决于你的OpenCode套餐:
- 免费版:每日生成次数有限
- 专业版:更高的速率限制与优先处理权
- 团队版:自定义限制与团队管理功能
Comparison with Other CLIs
与其他CLI工具对比
| Feature | OpenCode | Claude CLI | Gemini CLI | Copilot CLI |
|---|---|---|---|---|
| Template library | ✅ Extensive | ❌ | ❌ | ❌ |
| Test generation | ✅ Native | ❌ | ❌ | ❌ |
| Code analysis | ✅ Native | ❌ | ❌ | ❌ |
| Fast generation | ✅ Optimized | Slower | Medium | Medium |
| Extended context | ❌ | ✅ 200K tokens | ✅ 1M tokens | Limited |
| 功能 | OpenCode | Claude CLI | Gemini CLI | Copilot CLI |
|---|---|---|---|---|
| 模板库 | ✅ 丰富 | ❌ | ❌ | ❌ |
| 测试生成 | ✅ 原生支持 | ❌ | ❌ | ❌ |
| 代码分析 | ✅ 原生支持 | ❌ | ❌ | ❌ |
| 生成速度 | ✅ 优化后更快 | 较慢 | 中等 | 中等 |
| 扩展上下文 | ❌ | ✅ 200K tokens | ✅ 1M tokens | 有限 |
Working with Output
输出处理
JSON Output for Parsing
用于解析的JSON输出
bash
opencode analyze myfile.js --format json | jq '.issues'bash
opencode analyze myfile.js --format json | jq '.issues'Save Generated Code
保存生成的代码
bash
undefinedbash
undefinedOutput to file
输出到指定文件
opencode generate "REST API boilerplate" --output api.js
opencode generate "REST API boilerplate" --output api.js
Or redirect
或重定向输出
opencode generate "Database schema" > schema.sql
undefinedopencode generate "Database schema" > schema.sql
undefinedPipeline with Other Tools
与其他工具配合使用
bash
undefinedbash
undefinedGenerate, format, then lint
生成代码、格式化、然后检查语法
opencode generate "Express app" | prettier --write /dev/stdin
opencode generate "Express app" | prettier --write /dev/stdin
Generate test and run immediately
生成测试用例并立即运行
opencode test src/utils.js && npm test
undefinedopencode test src/utils.js && npm test
undefinedTroubleshooting
故障排除
Authentication Issues
认证问题
bash
undefinedbash
undefinedCheck auth status
检查认证状态
opencode auth status
opencode auth status
Re-authenticate
重新认证
opencode auth logout
opencode auth login
undefinedopencode auth logout
opencode auth login
undefinedCommand Not Found
命令未找到
bash
undefinedbash
undefinedVerify installation
验证安装情况
which opencode
which opencode
Reinstall
重新安装
npm install -g opencode-cli
undefinednpm install -g opencode-cli
undefinedGeneration Timeout
生成超时
bash
undefinedbash
undefinedUse longer timeout or simpler prompt
使用更长超时时间或更简单的提示
timeout 300 opencode generate "Simpler version: Basic auth endpoint"
timeout 300 opencode generate "Simpler version: Basic auth endpoint"
Try fast model
尝试使用fast模型
opencode generate --model fast "Your prompt"
undefinedopencode generate --model fast "Your prompt"
undefinedPoor Quality Output
输出质量不佳
bash
undefinedbash
undefinedUse quality model
使用quality模型
opencode generate --model quality "Complex feature needed here"
opencode generate --model quality "Complex feature needed here"
Be more specific in prompt
提示更具体
opencode generate "User authentication API with JWT, email verification, and refresh tokens"
opencode generate "User authentication API with JWT, email verification, and refresh tokens"
Check templates first
先查看可用模板
opencode templates list
undefinedopencode templates list
undefined