|
| 1 | +export type InlineValueOption<Key extends string> = { |
| 2 | + prefix: string; |
| 3 | + key: Key; |
| 4 | +}; |
| 5 | + |
| 6 | +export const CLI_SEMVER_PATTERN = /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/; |
| 7 | + |
| 8 | +type ParserConfig< |
| 9 | + Parsed extends object, |
| 10 | + ValueKey extends keyof Parsed & string, |
| 11 | + BooleanKey extends keyof Parsed & string, |
| 12 | +> = { |
| 13 | + inlineValueOptions: Array<InlineValueOption<ValueKey>>; |
| 14 | + valueOptions: Map<string, ValueKey>; |
| 15 | + booleanOptions: Map<string, BooleanKey>; |
| 16 | + parsePositional: (arg: string, index: number) => number; |
| 17 | + fail: (message: string) => never; |
| 18 | +}; |
| 19 | + |
| 20 | +export function parseMappedArgument< |
| 21 | + Parsed extends object, |
| 22 | + ValueKey extends keyof Parsed & string, |
| 23 | + BooleanKey extends keyof Parsed & string, |
| 24 | +>( |
| 25 | + args: string[], |
| 26 | + index: number, |
| 27 | + parsed: Parsed, |
| 28 | + config: ParserConfig<Parsed, ValueKey, BooleanKey>, |
| 29 | +) { |
| 30 | + const arg = args[index]; |
| 31 | + |
| 32 | + if (applyInlineValueOption(arg, parsed, config.inlineValueOptions)) { |
| 33 | + return index; |
| 34 | + } |
| 35 | + if (applyBooleanOption(arg, parsed, config.booleanOptions)) { |
| 36 | + return index; |
| 37 | + } |
| 38 | + |
| 39 | + return applyValueOrPositionalOption(args, index, parsed, config, arg); |
| 40 | +} |
| 41 | + |
| 42 | +function applyInlineValueOption<Parsed extends object, ValueKey extends keyof Parsed & string>( |
| 43 | + arg: string, |
| 44 | + parsed: Parsed, |
| 45 | + inlineOptions: Array<InlineValueOption<ValueKey>>, |
| 46 | +) { |
| 47 | + const option = inlineOptions.find((candidate) => arg.startsWith(candidate.prefix)); |
| 48 | + if (!option) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + parsed[option.key] = arg.slice(option.prefix.length) as Parsed[ValueKey]; |
| 53 | + return true; |
| 54 | +} |
| 55 | + |
| 56 | +function applyBooleanOption<Parsed extends object, BooleanKey extends keyof Parsed & string>( |
| 57 | + arg: string, |
| 58 | + parsed: Parsed, |
| 59 | + booleanOptions: Map<string, BooleanKey>, |
| 60 | +) { |
| 61 | + const option = booleanOptions.get(arg); |
| 62 | + if (!option) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + parsed[option] = true as Parsed[BooleanKey]; |
| 67 | + return true; |
| 68 | +} |
| 69 | + |
| 70 | +function applyValueOrPositionalOption< |
| 71 | + Parsed extends object, |
| 72 | + ValueKey extends keyof Parsed & string, |
| 73 | + BooleanKey extends keyof Parsed & string, |
| 74 | +>( |
| 75 | + args: string[], |
| 76 | + index: number, |
| 77 | + parsed: Parsed, |
| 78 | + config: ParserConfig<Parsed, ValueKey, BooleanKey>, |
| 79 | + arg: string, |
| 80 | +) { |
| 81 | + const option = config.valueOptions.get(arg); |
| 82 | + if (!option) { |
| 83 | + return config.parsePositional(arg, index); |
| 84 | + } |
| 85 | + |
| 86 | + parsed[option] = readNextArg(args, index, arg, config.fail) as Parsed[ValueKey]; |
| 87 | + return index + 1; |
| 88 | +} |
| 89 | + |
| 90 | +export function parseVersionOptionArgument< |
| 91 | + Parsed extends { version?: string }, |
| 92 | + ValueKey extends keyof Parsed & string, |
| 93 | + BooleanKey extends keyof Parsed & string, |
| 94 | +>( |
| 95 | + args: string[], |
| 96 | + index: number, |
| 97 | + parsed: Parsed, |
| 98 | + config: Omit<ParserConfig<Parsed, ValueKey, BooleanKey>, "parsePositional"> & { |
| 99 | + printUsage: () => void; |
| 100 | + }, |
| 101 | +) { |
| 102 | + return parseMappedArgument(args, index, parsed, { |
| 103 | + ...config, |
| 104 | + parsePositional: (arg, positionalIndex) => |
| 105 | + parseVersionOrHelp(arg, positionalIndex, parsed, config), |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +function parseVersionOrHelp<Parsed extends { version?: string }>( |
| 110 | + arg: string, |
| 111 | + index: number, |
| 112 | + parsed: Parsed, |
| 113 | + config: { printUsage: () => void; fail: (message: string) => never }, |
| 114 | +) { |
| 115 | + if (arg === "--help" || arg === "-h") { |
| 116 | + config.printUsage(); |
| 117 | + process.exit(0); |
| 118 | + } |
| 119 | + |
| 120 | + parsed.version = parseVersionPositionalArg(arg, parsed.version, config.fail); |
| 121 | + return index; |
| 122 | +} |
| 123 | + |
| 124 | +export function parseVersionPositionalArg( |
| 125 | + arg: string, |
| 126 | + currentVersion: string | undefined, |
| 127 | + fail: (message: string) => never, |
| 128 | +) { |
| 129 | + if (arg.startsWith("--")) { |
| 130 | + fail(`Unknown option: ${arg}`); |
| 131 | + } |
| 132 | + if (currentVersion) { |
| 133 | + fail(`Unexpected positional argument: ${arg}`); |
| 134 | + } |
| 135 | + |
| 136 | + return arg.replace(/^v/, ""); |
| 137 | +} |
| 138 | + |
| 139 | +export function readNextArg( |
| 140 | + args: string[], |
| 141 | + index: number, |
| 142 | + flag: string, |
| 143 | + fail: (message: string) => never, |
| 144 | +) { |
| 145 | + const value = args[index + 1]; |
| 146 | + if (!value || value.startsWith("--")) { |
| 147 | + fail(`Missing value for ${flag}`); |
| 148 | + } |
| 149 | + return value; |
| 150 | +} |
| 151 | + |
| 152 | +export function validateCliVersion( |
| 153 | + version: string, |
| 154 | + pattern: RegExp, |
| 155 | + fail: (message: string) => never, |
| 156 | +) { |
| 157 | + if (!pattern.test(version)) { |
| 158 | + fail(`Invalid semver: ${version}`); |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +export function validateCliDate(date: string, fail: (message: string) => never) { |
| 163 | + if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) { |
| 164 | + fail(`Invalid date: ${date}. Expected YYYY-MM-DD.`); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +export function optionalFlagArg(flag: string, enabled: boolean) { |
| 169 | + return enabled ? [flag] : []; |
| 170 | +} |
| 171 | + |
| 172 | +export function optionalValueArg(flag: string, value: string | undefined) { |
| 173 | + return value ? [flag, value] : []; |
| 174 | +} |
0 commit comments