Files
openclaw/src/cli/command-registration-policy.ts
2026-05-02 23:07:25 +01:00

46 lines
1.5 KiB
TypeScript

import { isTruthyEnvValue } from "../infra/env.js";
import { resolveCliArgvInvocation } from "./argv-invocation.js";
const RESERVED_NON_PLUGIN_COMMAND_ROOTS = new Set(["auth", "tool", "tools"]);
export function isReservedNonPluginCommandRoot(primary: string | null | undefined): boolean {
return typeof primary === "string" && RESERVED_NON_PLUGIN_COMMAND_ROOTS.has(primary);
}
export function shouldRegisterPrimaryCommandOnly(argv: string[]): boolean {
const invocation = resolveCliArgvInvocation(argv);
return invocation.primary !== null || !invocation.hasHelpOrVersion;
}
export function shouldSkipPluginCommandRegistration(params: {
argv: string[];
primary: string | null;
hasBuiltinPrimary: boolean;
}): boolean {
if (params.hasBuiltinPrimary) {
return true;
}
const invocation = resolveCliArgvInvocation(params.argv);
if (params.primary === "help") {
return invocation.hasHelpOrVersion && invocation.commandPath.length <= 1;
}
if (!params.primary) {
return invocation.hasHelpOrVersion;
}
if (isReservedNonPluginCommandRoot(params.primary)) {
return true;
}
return false;
}
export function shouldEagerRegisterSubcommands(env: NodeJS.ProcessEnv = process.env): boolean {
return isTruthyEnvValue(env.OPENCLAW_DISABLE_LAZY_SUBCOMMANDS);
}
export function shouldRegisterPrimarySubcommandOnly(
argv: string[],
env: NodeJS.ProcessEnv = process.env,
): boolean {
return !shouldEagerRegisterSubcommands(env) && shouldRegisterPrimaryCommandOnly(argv);
}