ogt-cli-opencode

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

OpenCode 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
opencode
bash
opencode

One-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.js
bash
opencode analyze myfile.js

Generate tests

生成测试用例

bash
opencode test "src/utils/auth.js"
bash
opencode test "src/utils/auth.js"

Installation

安装步骤

bash
undefined
bash
undefined

npm (recommended)

npm(推荐方式)

npm install -g opencode-cli
npm install -g opencode-cli

Verify installation

验证安装

opencode --version
undefined
opencode --version
undefined

Key Commands

核心命令

CommandDescription
opencode
Interactive mode
opencode generate "<prompt>"
Generate code from description
opencode analyze <file>
Analyze code quality and structure
opencode test <file>
Generate test cases for file
opencode refactor <file>
Suggest refactoring improvements
opencode document <file>
Generate documentation
命令说明
opencode
交互模式
opencode generate "<prompt>"
根据描述生成代码
opencode analyze <file>
分析代码质量与结构
opencode test <file>
为文件生成测试用例
opencode refactor <file>
提供重构优化建议
opencode document <file>
生成文档

Key Options

核心选项

OptionDescription
--template <name>
Use specific code template
--language <lang>
Target language (js, ts, python, go, etc.)
--output <path>
Output file path
--format <fmt>
Output format (default, json, etc.)
--model <model>
Model variant (default, fast, quality)
选项说明
--template <name>
使用指定代码模板
--language <lang>
目标语言(js、ts、python、go等)
--output <path>
输出文件路径
--format <fmt>
输出格式(默认、json等)
--model <model>
模型变体(default、fast、quality)

Authentication

认证方式

OpenCode CLI uses OAuth:
bash
undefined
OpenCode CLI采用OAuth认证:
bash
undefined

First-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
undefined
OpenCode可读取并为项目中的文件生成代码:
bash
undefined

Analyze 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
undefined
opencode document src/services/auth.js
undefined

Code Templates

代码模板

OpenCode includes templates for common patterns:
bash
undefined
OpenCode内置了常见模式的模板:
bash
undefined

List 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"
undefined
opencode generate --template express-api "User management API" opencode generate --template react-form "Login form" opencode generate --template node-cli "CLI tool for backups"
undefined

Built-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
undefined

Generate 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
undefined
timeout 180 opencode generate "Database schema for e-commerce" 2>&1
undefined

Script: 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
    ,
    document
    (default: generate)
  • --file <path>
    — File path for non-generation actions
  • --template <name>
    — Template to use for generation
  • --language <lang>
    — Programming language
  • --timeout <secs>
    — Timeout in seconds (default: 180)
  • --output <path>
    — Output file path
  • --workdir <path>
    — Working directory
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
    document
    (默认值:generate)
  • --file <path>
    — 非生成类操作的文件路径
  • --template <name>
    — 生成代码时使用的模板
  • --language <lang>
    — 编程语言
  • --timeout <secs>
    — 超时时间(秒,默认值:180)
  • --output <path>
    — 输出文件路径
  • --workdir <path>
    — 工作目录
该脚本支持:
  • 超时保护
  • 错误捕获与格式化
  • 便于解析的整洁输出
  • 退出码传递

Model Selection

模型选择

OpenCode offers different model variants:
VariantBest For
fast
Quick generation, simple tasks
default
Balanced speed and quality (recommended)
quality
Complex code, high-quality output
bash
opencode generate --model quality "Complex state management system"
OpenCode提供不同的模型变体:
变体最佳适用场景
fast
快速生成、简单任务
default
速度与质量平衡(推荐)
quality
复杂代码、高质量输出
bash
opencode generate --model quality "Complex state management system"

Tips

使用技巧

  1. Be specific with descriptions — Better prompts yield better code
  2. Use templates for consistent patterns in your codebase
  3. Review generated code — Always review and test generated code
  4. Combine with analysis — Analyze before refactoring
  5. Set timeouts for long-running generation tasks
  1. 描述要具体 —— 更清晰的提示会生成更优质的代码
  2. 使用模板 —— 确保代码库中的模式一致性
  3. 审查生成的代码 —— 务必审查并测试生成的代码
  4. 结合分析功能 —— 重构前先进行代码分析
  5. 设置超时时间 —— 针对耗时较长的生成任务

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工具对比

FeatureOpenCodeClaude CLIGemini CLICopilot CLI
Template library✅ Extensive
Test generation✅ Native
Code analysis✅ Native
Fast generation✅ OptimizedSlowerMediumMedium
Extended context✅ 200K tokens✅ 1M tokensLimited
功能OpenCodeClaude CLIGemini CLICopilot 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
undefined
bash
undefined

Output 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
undefined
opencode generate "Database schema" > schema.sql
undefined

Pipeline with Other Tools

与其他工具配合使用

bash
undefined
bash
undefined

Generate, 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
undefined
opencode test src/utils.js && npm test
undefined

Troubleshooting

故障排除

Authentication Issues

认证问题

bash
undefined
bash
undefined

Check auth status

检查认证状态

opencode auth status
opencode auth status

Re-authenticate

重新认证

opencode auth logout opencode auth login
undefined
opencode auth logout opencode auth login
undefined

Command Not Found

命令未找到

bash
undefined
bash
undefined

Verify installation

验证安装情况

which opencode
which opencode

Reinstall

重新安装

npm install -g opencode-cli
undefined
npm install -g opencode-cli
undefined

Generation Timeout

生成超时

bash
undefined
bash
undefined

Use 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"
undefined
opencode generate --model fast "Your prompt"
undefined

Poor Quality Output

输出质量不佳

bash
undefined
bash
undefined

Use 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
undefined
opencode templates list
undefined