12.1 CLI 启动流程
12.1.1 入口文件 index.ts
// index.ts
import yargs from "yargs"
import { hideBin } from "yargs/helpers"
const cli = yargs(hideBin(process.argv))
.parserConfiguration({ "populate--": true })
.scriptName("opencode")
.wrap(100)
.help("help", "show help").alias("help", "h")
.version("version", "show version number", Installation.VERSION)
.alias("version", "v")
.option("print-logs", { describe: "print logs to stderr", type: "boolean" })
.option("log-level", {
describe: "log level",
type: "string",
choices: ["DEBUG", "INFO", "WARN", "ERROR"],
})
.middleware(async (opts) => {
// 全局中间件:初始化日志系统
await Log.init({
print: process.argv.includes("--print-logs"),
dev: Installation.isLocal(),
level: opts.logLevel ?? (Installation.isLocal() ? "DEBUG" : "INFO"),
})
// 设置环境变量标识
process.env.AGENT = "1"
process.env.OPENCODE = "1"
})
.usage("\n" + UI.logo())
// 注册所有子命令
.command(TuiThreadCommand) // 默认命令 $0
.command(RunCommand) // opencode run
.command(ServeCommand) // opencode serve
.command(WebCommand) // opencode web
.command(AuthCommand) // opencode auth
.command(McpCommand) // opencode mcp
.command(ModelsCommand) // opencode models
.command(SessionCommand) // opencode session
.command(ExportCommand) // opencode export
.command(ImportCommand) // opencode import
.command(GenerateCommand) // opencode generate
.command(GithubCommand) // opencode github
.command(PrCommand) // opencode pr
.command(StatsCommand) // opencode stats
.command(UpgradeCommand) // opencode upgrade
.command(UninstallCommand) // opencode uninstall
.command(AcpCommand) // opencode acp
.command(AgentCommand) // opencode agent
.command(AttachCommand) // opencode attach
.strict()
await cli.parse()12.1.2 cli/bootstrap.ts——Instance 上下文引导
12.1.3 cli/cmd/cmd.ts——子命令注册
12.1.4 各子命令概览
命令
文件
功能
12.1.5 错误处理
Last updated
