|
| 1 | +import { z } from 'zod' |
| 2 | +import { format } from './colors' |
| 3 | +import type { Command } from './types' |
| 4 | + |
| 5 | +export function showHelp({ |
| 6 | + commands, |
| 7 | + cliName, |
| 8 | + command |
| 9 | +}: { |
| 10 | + commands: Command[] |
| 11 | + cliName: string |
| 12 | + command?: string |
| 13 | +}): void { |
| 14 | + if (command) { |
| 15 | + const cmd = commands.find(c => c.name === command) |
| 16 | + if (!cmd) { |
| 17 | + console.error(format.error(`Unknown command: ${command}`)) |
| 18 | + showHelp({ commands, cliName }) |
| 19 | + return |
| 20 | + } |
| 21 | + showCommandHelp({ command: cmd, cliName }) |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + console.log(`\n${format.title('USAGE')}`) |
| 26 | + console.log(` ${cliName} [command] [options]`) |
| 27 | + |
| 28 | + console.log(`\n${format.title('COMMANDS')}`) |
| 29 | + for (const cmd of commands) { |
| 30 | + console.log(` ${format.command(cmd.name)}`) |
| 31 | + console.log(` ${cmd.description}`) |
| 32 | + } |
| 33 | + |
| 34 | + console.log(`\n${format.title('OPTIONS')}`) |
| 35 | + console.log(` ${format.parameter('--help, -h')}`) |
| 36 | + console.log(' Show help information') |
| 37 | + console.log(` ${format.parameter('--version, -v')}`) |
| 38 | + console.log(' Show version information') |
| 39 | +} |
| 40 | + |
| 41 | +function showCommandHelp({ |
| 42 | + command, |
| 43 | + cliName |
| 44 | +}: { |
| 45 | + command: Command |
| 46 | + cliName: string |
| 47 | +}): void { |
| 48 | + console.log(`\n${format.title('USAGE')}`) |
| 49 | + console.log(` ${cliName} ${command.name} [options]`) |
| 50 | + |
| 51 | + console.log(`\n${format.title('DESCRIPTION')}`) |
| 52 | + console.log(` ${command.description}`) |
| 53 | + |
| 54 | + if (command.args instanceof z.ZodObject) { |
| 55 | + const shape = command.args.shape as z.AnyZodObject |
| 56 | + const entries = Object.entries(shape) |
| 57 | + |
| 58 | + if (entries.length > 0) { |
| 59 | + console.log(`\n${format.title('OPTIONS')}`) |
| 60 | + |
| 61 | + for (const [key, schema] of entries) { |
| 62 | + const type = getSchemaType(schema) |
| 63 | + const isRequired = schema.isOptional() |
| 64 | + const description = getSchemaDescription(schema) |
| 65 | + const defaultValue = getSchemaDefaultValue(schema) |
| 66 | + |
| 67 | + const flagStr = type === 'boolean' ? `--${key}` : `--${key} <${type}>` |
| 68 | + |
| 69 | + const requiredStr = isRequired ? ' (required)' : '' |
| 70 | + const defaultStr = defaultValue !== undefined ? ` (default: ${defaultValue})` : '' |
| 71 | + |
| 72 | + console.log(` ${format.parameter(flagStr)}${requiredStr}${defaultStr}`) |
| 73 | + if (description) { |
| 74 | + console.log(` ${description}`) |
| 75 | + } |
| 76 | + console.log('') |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +function getSchemaType(schema: z.ZodTypeAny): string { |
| 83 | + if (schema instanceof z.ZodBoolean) return 'boolean' |
| 84 | + if (schema instanceof z.ZodString) return 'string' |
| 85 | + if (schema instanceof z.ZodNumber) return 'number' |
| 86 | + if (schema instanceof z.ZodArray) return 'array' |
| 87 | + if (schema instanceof z.ZodOptional) return getSchemaType(schema.unwrap()) |
| 88 | + if (schema instanceof z.ZodDefault) return getSchemaType(schema.removeDefault()) |
| 89 | + return 'value' |
| 90 | +} |
| 91 | + |
| 92 | +function getSchemaDescription(schema: z.ZodTypeAny): string | undefined { |
| 93 | + if (schema instanceof z.ZodOptional) return getSchemaDescription(schema.unwrap()) |
| 94 | + if (schema instanceof z.ZodDefault) return getSchemaDescription(schema.removeDefault()) |
| 95 | + return schema.description |
| 96 | +} |
| 97 | + |
| 98 | +function getSchemaDefaultValue(schema: z.ZodTypeAny): unknown { |
| 99 | + if (schema instanceof z.ZodDefault) { |
| 100 | + const defaultValue = schema._def.defaultValue() |
| 101 | + return defaultValue |
| 102 | + } |
| 103 | + if (schema instanceof z.ZodOptional) return getSchemaDefaultValue(schema.unwrap()) |
| 104 | + return undefined |
| 105 | +} |
0 commit comments