fix(cli): preserve lazy command parent flags

This commit is contained in:
Peter Steinberger
2026-04-30 00:48:46 +01:00
parent 36bb723dfb
commit 01254500df
7 changed files with 186 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
import type { Command } from "commander";
import { reparseProgramFromActionArgs } from "./action-reparse.js";
import { removeCommandByName } from "./command-tree.js";
import { resolveCommandOptionArgs } from "./helpers.js";
type RegisterLazyCommandParams = {
program: Command;
@@ -14,25 +15,6 @@ type RegisterLazyCommandParams = {
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,
@@ -51,7 +33,7 @@ export function registerLazyCommand({
const actionCommand = actionArgs.at(-1) as (Command & { args?: string[] }) | undefined;
if (actionCommand) {
actionCommand.args = [
...resolvePlaceholderOptionArgs(actionCommand),
...resolveCommandOptionArgs(actionCommand),
...(actionCommand.args ?? []),
];
}