Skip to content

CLI 命令行概述

OpenClaw CLI 是用户与系统交互的主要入口,基于 Commander.js 构建,使用依赖注入模式组织命令。

源码位置

src/cli/
├── program/
│   └── build-program.ts    # Commander 程序构建
├── deps.ts                 # 依赖注入
├── run-main.ts             # CLI 主运行函数
├── profile.ts              # Profile 管理
├── prompt.ts               # 交互提示
├── wait.ts                 # 等待工具
├── progress.ts             # 进度条
└── ...

src/commands/                # ~183 files, CLI 子命令
├── gateway-cli.ts          # gateway 命令组
├── channels-cli.ts         # channels 命令组
├── config-cli.ts           # config 命令组
├── plugins-cli.ts          # plugins 命令组
├── send-cli.ts             # send 命令
├── cron-cli.ts             # cron 命令组
├── skills-cli.ts           # skills 命令组
├── nodes-cli.ts            # nodes 命令组
├── memory-cli.ts           # memory 命令组
├── hooks-cli.ts            # hooks 命令组
├── update-cli.ts           # update 命令
└── ...

CLI 启动流程

Respawn 机制

Source: src/entry.ts

入口文件会检查是否需要 respawn——为了抑制 Node.js 的 ExperimentalWarning,需要在启动时添加 --disable-warning=ExperimentalWarning 标志:

typescript
// src/entry.ts (simplified)
function ensureExperimentalWarningSuppressed(): boolean {
  if (hasExperimentalWarningSuppressed()) return false;

  // Respawn with the flag
  const child = spawn(
    process.execPath,
    [EXPERIMENTAL_WARNING_FLAG, ...process.execArgv, ...process.argv.slice(1)],
    { stdio: "inherit", env: process.env }
  );

  attachChildProcessBridge(child);
  return true; // Parent stops here
}

核心概念

概念说明源文件
ProgramCommander.js 程序实例cli/program/build-program.ts
CliDeps依赖注入容器cli/deps.ts
ProfileCLI 配置 profilecli/profile.ts
Command Module命令实现模块commands/*.ts

章节导航

  1. 命令结构 — Commander.js 命令注册与模块组织
  2. 依赖注入 — CliDeps 模式、桥接层

OpenClaw 源码学习教程