mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 08:00:45 +00:00
fix(cli): preserve lazy placeholder options
This commit is contained in:
@@ -5,6 +5,12 @@ import { registerLazyCommand } from "./register-lazy-command.js";
|
||||
export type CommandGroupPlaceholder = {
|
||||
name: string;
|
||||
description: string;
|
||||
options?: readonly CommandGroupPlaceholderOption[];
|
||||
};
|
||||
|
||||
export type CommandGroupPlaceholderOption = {
|
||||
flags: string;
|
||||
description: string;
|
||||
};
|
||||
|
||||
export type CommandGroupEntry = {
|
||||
@@ -53,6 +59,7 @@ export function registerLazyCommandGroup(
|
||||
program,
|
||||
name: placeholder.name,
|
||||
description: placeholder.description,
|
||||
options: placeholder.options,
|
||||
removeNames: [...new Set(getCommandGroupNames(entry))],
|
||||
register: async () => {
|
||||
await entry.register(program);
|
||||
|
||||
@@ -6,21 +6,55 @@ type RegisterLazyCommandParams = {
|
||||
program: Command;
|
||||
name: string;
|
||||
description: string;
|
||||
options?: readonly {
|
||||
flags: string;
|
||||
description: string;
|
||||
}[];
|
||||
removeNames?: string[];
|
||||
register: () => Promise<void> | void;
|
||||
};
|
||||
|
||||
function resolvePlaceholderOptionArgs(command: Command): string[] {
|
||||
const out: string[] = [];
|
||||
for (const option of command.options) {
|
||||
const value = command.getOptionValue(option.attributeName());
|
||||
if (value === undefined || value === false) {
|
||||
continue;
|
||||
}
|
||||
const flag = option.long ?? option.short;
|
||||
if (!flag) {
|
||||
continue;
|
||||
}
|
||||
out.push(flag);
|
||||
if (value !== true) {
|
||||
out.push(String(value));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function registerLazyCommand({
|
||||
program,
|
||||
name,
|
||||
description,
|
||||
options,
|
||||
removeNames,
|
||||
register,
|
||||
}: RegisterLazyCommandParams): void {
|
||||
const placeholder = program.command(name).description(description);
|
||||
for (const option of options ?? []) {
|
||||
placeholder.option(option.flags, option.description);
|
||||
}
|
||||
placeholder.allowUnknownOption(true);
|
||||
placeholder.allowExcessArguments(true);
|
||||
placeholder.action(async (...actionArgs) => {
|
||||
const actionCommand = actionArgs.at(-1) as (Command & { args?: string[] }) | undefined;
|
||||
if (actionCommand) {
|
||||
actionCommand.args = [
|
||||
...resolvePlaceholderOptionArgs(actionCommand),
|
||||
...(actionCommand.args ?? []),
|
||||
];
|
||||
}
|
||||
for (const commandName of new Set(removeNames ?? [name])) {
|
||||
removeCommandByName(program, commandName);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user