diff --git a/src/cli/program/action-reparse.ts b/src/cli/program/action-reparse.ts new file mode 100644 index 000000000..fcb46c5de --- /dev/null +++ b/src/cli/program/action-reparse.ts @@ -0,0 +1,22 @@ +import type { Command } from "commander"; +import { buildParseArgv } from "../argv.js"; +import { resolveActionArgs } from "./helpers.js"; + +export async function reparseProgramFromActionArgs( + program: Command, + actionArgs: unknown[], +): Promise { + const actionCommand = actionArgs.at(-1) as Command | undefined; + const root = actionCommand?.parent ?? program; + const rawArgs = (root as Command & { rawArgs?: string[] }).rawArgs; + const actionArgsList = resolveActionArgs(actionCommand); + const fallbackArgv = actionCommand?.name() + ? [actionCommand.name(), ...actionArgsList] + : actionArgsList; + const parseArgv = buildParseArgv({ + programName: program.name(), + rawArgs, + fallbackArgv, + }); + await program.parseAsync(parseArgv); +} diff --git a/src/cli/program/command-registry.ts b/src/cli/program/command-registry.ts index cbe61c67c..17b4a8594 100644 --- a/src/cli/program/command-registry.ts +++ b/src/cli/program/command-registry.ts @@ -1,7 +1,7 @@ import type { Command } from "commander"; import type { ProgramContext } from "./context.js"; -import { buildParseArgv, getPrimaryCommand, hasHelpOrVersion } from "../argv.js"; -import { resolveActionArgs } from "./helpers.js"; +import { getPrimaryCommand, hasHelpOrVersion } from "../argv.js"; +import { reparseProgramFromActionArgs } from "./action-reparse.js"; import { registerSubCliCommands } from "./register.subclis.js"; type CommandRegisterParams = { @@ -157,19 +157,7 @@ function registerLazyCoreCommand( } } await entry.register({ program, ctx, argv: process.argv }); - const actionCommand = actionArgs.at(-1) as Command | undefined; - const root = actionCommand?.parent ?? program; - const rawArgs = (root as Command & { rawArgs?: string[] }).rawArgs; - const actionArgsList = resolveActionArgs(actionCommand); - const fallbackArgv = actionCommand?.name() - ? [actionCommand.name(), ...actionArgsList] - : actionArgsList; - const parseArgv = buildParseArgv({ - programName: program.name(), - rawArgs, - fallbackArgv, - }); - await program.parseAsync(parseArgv); + await reparseProgramFromActionArgs(program, actionArgs); }); } diff --git a/src/cli/program/register.subclis.ts b/src/cli/program/register.subclis.ts index a822113e6..48fc16965 100644 --- a/src/cli/program/register.subclis.ts +++ b/src/cli/program/register.subclis.ts @@ -1,8 +1,8 @@ import type { Command } from "commander"; import type { OpenClawConfig } from "../../config/config.js"; import { isTruthyEnvValue } from "../../infra/env.js"; -import { buildParseArgv, getPrimaryCommand, hasHelpOrVersion } from "../argv.js"; -import { resolveActionArgs } from "./helpers.js"; +import { getPrimaryCommand, hasHelpOrVersion } from "../argv.js"; +import { reparseProgramFromActionArgs } from "./action-reparse.js"; type SubCliRegistrar = (program: Command) => Promise | void; @@ -273,19 +273,7 @@ function registerLazyCommand(program: Command, entry: SubCliEntry) { placeholder.action(async (...actionArgs) => { removeCommand(program, placeholder); await entry.register(program); - const actionCommand = actionArgs.at(-1) as Command | undefined; - const root = actionCommand?.parent ?? program; - const rawArgs = (root as Command & { rawArgs?: string[] }).rawArgs; - const actionArgsList = resolveActionArgs(actionCommand); - const fallbackArgv = actionCommand?.name() - ? [actionCommand.name(), ...actionArgsList] - : actionArgsList; - const parseArgv = buildParseArgv({ - programName: program.name(), - rawArgs, - fallbackArgv, - }); - await program.parseAsync(parseArgv); + await reparseProgramFromActionArgs(program, actionArgs); }); }