@@ -162,8 +162,19 @@ export function isRootHelpInvocation(argv: string[]): boolean {
|
162 | 162 | return isRootInvocationForFlags(argv, HELP_FLAGS); |
163 | 163 | } |
164 | 164 | |
165 | | -export function normalizeGeneratedHelpCommandArgv(argv: string[]): string[] { |
166 | | -const positionals: Array<{ value: string; index: number }> = []; |
| 165 | +type HelpNormalizationPositional = { value: string; index: number }; |
| 166 | + |
| 167 | +type HelpNormalizationScanResult = |
| 168 | +| { |
| 169 | +ok: true; |
| 170 | +positionals: HelpNormalizationPositional[]; |
| 171 | +rootOptions: string[]; |
| 172 | +helpFlagIndex: number | null; |
| 173 | +} |
| 174 | +| { ok: false }; |
| 175 | + |
| 176 | +function scanHelpNormalizationArgv(argv: string[]): HelpNormalizationScanResult { |
| 177 | +const positionals: HelpNormalizationPositional[] = []; |
167 | 178 | const rootOptions: string[] = []; |
168 | 179 | let helpFlagIndex: number | null = null; |
169 | 180 | |
@@ -183,11 +194,20 @@ export function normalizeGeneratedHelpCommandArgv(argv: string[]): string[] {
|
183 | 194 | continue; |
184 | 195 | } |
185 | 196 | if (arg.startsWith("-")) { |
186 | | -return argv; |
| 197 | +return { ok: false }; |
187 | 198 | } |
188 | 199 | positionals.push({ value: arg, index }); |
189 | 200 | } |
190 | 201 | |
| 202 | +return { ok: true, positionals, rootOptions, helpFlagIndex }; |
| 203 | +} |
| 204 | + |
| 205 | +export function normalizeGeneratedHelpCommandArgv(argv: string[]): string[] { |
| 206 | +const scan = scanHelpNormalizationArgv(argv); |
| 207 | +if (!scan.ok) { |
| 208 | +return argv; |
| 209 | +} |
| 210 | +const { positionals, rootOptions, helpFlagIndex } = scan; |
191 | 211 | const [primary, secondary, target] = positionals; |
192 | 212 | if ( |
193 | 213 | !primary || |
@@ -213,30 +233,11 @@ export function normalizeGeneratedHelpCommandArgv(argv: string[]): string[] {
|
213 | 233 | } |
214 | 234 | |
215 | 235 | export function normalizeRootHelpTargetArgv(argv: string[]): string[] { |
216 | | -const positionals: Array<{ value: string; index: number }> = []; |
217 | | -const rootOptions: string[] = []; |
218 | | -let helpFlagIndex: number | null = null; |
219 | | - |
220 | | -for (let index = 2; index < argv.length; index += 1) { |
221 | | -const arg = argv[index]; |
222 | | -if (!arg || arg === FLAG_TERMINATOR) { |
223 | | -break; |
224 | | -} |
225 | | -const consumed = consumeRootOptionToken(argv, index); |
226 | | -if (consumed > 0) { |
227 | | -rootOptions.push(...argv.slice(index, index + consumed)); |
228 | | -index += consumed - 1; |
229 | | -continue; |
230 | | -} |
231 | | -if (HELP_FLAGS.has(arg)) { |
232 | | -helpFlagIndex = index; |
233 | | -continue; |
234 | | -} |
235 | | -if (arg.startsWith("-")) { |
236 | | -return argv; |
237 | | -} |
238 | | -positionals.push({ value: arg, index }); |
| 236 | +const scan = scanHelpNormalizationArgv(argv); |
| 237 | +if (!scan.ok) { |
| 238 | +return argv; |
239 | 239 | } |
| 240 | +const { positionals, rootOptions, helpFlagIndex } = scan; |
240 | 241 | |
241 | 242 | const [help, target] = positionals; |
242 | 243 | if ( |
|