From 515d9b6580edff6e6c4a399c0b6eca33db4fa4b4 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 4 Dec 2026 04:43:40 +0800 Subject: [PATCH] refactor(cli): share help schema field projection (#16470) --- src/cli/help.ts | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/cli/help.ts b/src/cli/help.ts index 07ffa88983a..6c0b1baf1c0 100644 --- a/src/cli/help.ts +++ b/src/cli/help.ts @@ -91,7 +91,7 @@ const shownDefault = (field: z.ZodType, type: string): unknown => { return value; }; -const fieldDescription = (field: z.ZodType, type = typeName(field)): string => { +const fieldDescription = (field: z.ZodType, type: string): string => { const defaultValue = shownDefault(field, type); const description = field.description ?? ""; return defaultValue === undefined @@ -99,28 +99,27 @@ const fieldDescription = (field: z.ZodType, type = typeName(field)): string => { : `${description} (default: ${String(defaultValue)})`; }; +const fields = (schema: z.ZodObject | undefined) => + Object.entries( + (schema?.shape ?? {}) as Readonly>, + ).map(([key, field]) => ({ key, field, type: typeName(field) })); + const optionRows = ( - options: z.ZodObject, + options: z.ZodObject | undefined, alias: Readonly> | undefined, ): HelpCommandRow[] => - Object.entries(options.shape as Readonly>).map( - ([key, field]) => { - const type = typeName(field); - const short = alias?.[key]; - const value = type === "boolean" ? "" : ` <${type}>`; - const label = `--${optionFlagName(key)}${short === undefined ? "" : `, -${short}`}${value}`; - return { name: label, description: fieldDescription(field, type) }; - }, - ); + fields(options).map(({ key, field, type }) => { + const short = alias?.[key]; + const value = type === "boolean" ? "" : ` <${type}>`; + const label = `--${optionFlagName(key)}${short === undefined ? "" : `, -${short}`}${value}`; + return { name: label, description: fieldDescription(field, type) }; + }); /** ``, `[optional]` and `` placeholders, in argv order. */ -const synopsis = (name: string, args: z.ZodObject | undefined): string => +const synopsis = (name: string, args: ReturnType): string => [ name, - ...Object.entries( - (args?.shape ?? {}) as Readonly>, - ).map(([key, field]) => { - const type = typeName(field); + ...args.map(({ key, field, type }) => { const label = (type.includes("|") ? type : key) + (type === "array" ? "..." : ""); return field._zod.optout === "optional" ? `[${label}]` : `<${label}>`; @@ -187,8 +186,7 @@ const globalOptionsLines = (help: GlobalOptionsHelp | undefined): string[] => { "Integrations:", ...alignedRows(help.root.integrations, false), ); - const custom = - help.custom === undefined ? [] : optionRows(help.custom, undefined); + const custom = optionRows(help.custom, undefined); if (custom.length > 0) lines.push("", "Custom Global Options:", ...alignedRows(custom)); lines.push("", "Global Options:", ...alignedRows(globalOptionRows(help))); @@ -204,20 +202,19 @@ const title = (name: string, help: CommandHelp): string => { /** Render one command's help, without a trailing newline. */ export const formatCommandHelp = (name: string, help: CommandHelp): string => { + const args = fields(help.args); const lines = [ title(name, help), "", - `Usage: ${synopsis(name, help.args)}${help.options === undefined ? "" : " [options]"}`, + `Usage: ${synopsis(name, args)}${help.options === undefined ? "" : " [options]"}`, ]; - const args = Object.entries( - (help.args?.shape ?? {}) as Readonly>, - ).map(([key, field]) => ({ + const argumentRows = args.map(({ key, field, type }) => ({ name: key, - description: fieldDescription(field), + description: fieldDescription(field, type), })); - if (args.length > 0) lines.push("", "Arguments:", ...alignedRows(args)); - const options = - help.options === undefined ? [] : optionRows(help.options, help.alias); + if (argumentRows.length > 0) + lines.push("", "Arguments:", ...alignedRows(argumentRows)); + const options = optionRows(help.options, help.alias); if (options.length > 0) lines.push("", "Options:", ...alignedRows(options)); if (help.hint !== undefined) lines.push("", help.hint); lines.push(...globalOptionsLines(help.globalOptions));