+
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,17 @@ function displayCommandHelp(
optionLine += `, \x1b[36m-${alias}\x1b[0m`;
}

// Extract default value if present
const defaultValue = extractDefaultValue(zodType);
let finalDescription = description;
if (defaultValue !== undefined) {
finalDescription = description + (description ? ' ' : '') + `(default: ${defaultValue})`;
}

// Add padding and description (accounting for color codes)
const visibleLength = ` --${kebabKey}${alias ? `, -${alias}` : ''}`.length;
const padding = ' '.repeat(Math.max(2, maxLength - visibleLength + 4));
console.log(`${optionLine}${padding}${description}`);
console.log(`${optionLine}${padding}${finalDescription}`);
}
}
}
Expand Down Expand Up @@ -340,6 +347,44 @@ function isZodArrayType(zodType: any): boolean {
return innerType instanceof z.ZodArray || (innerType._def && innerType._def.typeName === 'ZodArray');
}

/**
* Extracts the default value from a Zod type, handling nested optional and default wrappers.
*
* @param zodType - The Zod type to extract default value from
* @returns The formatted default value if present, undefined otherwise
*/
function extractDefaultValue(zodType: any): string | undefined {
// Traverse through the type definition to find default value
let currentType = zodType;

while (currentType && currentType._def) {
// Check if this is a ZodDefault type
if (currentType.constructor.name === 'ZodDefault' || currentType._def.typeName === 'ZodDefault') {
// Handle both function and direct value defaults
const defaultValue = currentType._def.defaultValue;
const value = typeof defaultValue === 'function' ? defaultValue() : defaultValue;

// Format the value based on its type
if (typeof value === 'string') {
return `"${value}"`;
}

return String(value);
}

// Move to the inner type if it exists
if (currentType._def.innerType) {
currentType = currentType._def.innerType;
} else if (currentType._def.type) {
currentType = currentType._def.type;
} else {
break;
}
}

return undefined;
}

/**
* Validates and transforms command options using Zod schema validation.
* Processes aliases and kebab-case conversion before validation.
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载