mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-12 12:30:44 +00:00
fix(cli): preserve lazy command parent flags
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import type { Command } from "commander";
|
||||
|
||||
export function collectOption(value: string, previous: string[] = []): string[] {
|
||||
return [...previous, value];
|
||||
}
|
||||
@@ -23,10 +25,76 @@ export function parsePositiveIntOrUndefined(value: unknown): number | undefined
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function resolveActionArgs(actionCommand?: import("commander").Command): string[] {
|
||||
export function resolveActionArgs(actionCommand?: Command): string[] {
|
||||
if (!actionCommand) {
|
||||
return [];
|
||||
}
|
||||
const args = (actionCommand as import("commander").Command & { args?: string[] }).args;
|
||||
const args = (actionCommand as Command & { args?: string[] }).args;
|
||||
return Array.isArray(args) ? args : [];
|
||||
}
|
||||
|
||||
function isDefaultOptionValue(command: Command, name: string): boolean {
|
||||
if (typeof command.getOptionValueSource !== "function") {
|
||||
return false;
|
||||
}
|
||||
return command.getOptionValueSource(name) === "default";
|
||||
}
|
||||
|
||||
function appendOptionValue(out: string[], flag: string, value: unknown): void {
|
||||
if (value === undefined) {
|
||||
return;
|
||||
}
|
||||
if (value === false) {
|
||||
if (flag.startsWith("--no-")) {
|
||||
out.push(flag);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (value === true) {
|
||||
out.push(flag);
|
||||
return;
|
||||
}
|
||||
const arg = stringifyOptionValue(value);
|
||||
if (arg !== undefined) {
|
||||
out.push(flag, arg);
|
||||
}
|
||||
}
|
||||
|
||||
function stringifyOptionValue(value: unknown): string | undefined {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return String(value);
|
||||
}
|
||||
if (typeof value === "bigint") {
|
||||
return value.toString();
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function resolveCommandOptionArgs(command?: Command): string[] {
|
||||
if (!command) {
|
||||
return [];
|
||||
}
|
||||
const out: string[] = [];
|
||||
for (const option of command.options) {
|
||||
const name = option.attributeName();
|
||||
if (isDefaultOptionValue(command, name)) {
|
||||
continue;
|
||||
}
|
||||
const flag = option.long ?? option.short;
|
||||
if (!flag) {
|
||||
continue;
|
||||
}
|
||||
const value = command.getOptionValue(name);
|
||||
if (Array.isArray(value)) {
|
||||
for (const item of value) {
|
||||
appendOptionValue(out, flag, item);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
appendOptionValue(out, flag, value);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user