refactor(cli): share help schema field projection (#16470)

This commit is contained in:
Vincent Koc authored and GitHub committed 2026-12-03 20:43:40 +00:00
1 parent 50d3c11ba9
commit 515d9b6580
1 file changed
+23 -26
+23 -26
View File
@@ -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<Record<string, z.ZodType>>,
).map(([key, field]) => ({ key, field, type: typeName(field) }));
const optionRows = (
options: z.ZodObject,
options: z.ZodObject | undefined,
alias: Readonly<Record<string, string>> | undefined,
): HelpCommandRow[] =>
Object.entries(options.shape as Readonly<Record<string, z.ZodType>>).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) };
});
/** `<required>`, `[optional]` and `<variadic...>` placeholders, in argv order. */
const synopsis = (name: string, args: z.ZodObject | undefined): string =>
const synopsis = (name: string, args: ReturnType<typeof fields>): string =>
[
name,
...Object.entries(
(args?.shape ?? {}) as Readonly<Record<string, z.ZodType>>,
).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<Record<string, z.ZodType>>,
).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));