(argv: string[])
| 41 | } |
| 42 | |
| 43 | export function parseRawArgs(argv: string[]): RawParsedArgs { |
| 44 | const flags: CliFlags = { json: false, help: false, version: false }; |
| 45 | let command: string | null = null; |
| 46 | const positionals: string[] = []; |
| 47 | const warnings: string[] = []; |
| 48 | const providedFlags: ParsedFlagRecord[] = []; |
| 49 | let parseFlags = true; |
| 50 | |
| 51 | for (let i = 0; i < argv.length; i += 1) { |
| 52 | const arg = argv[i]!; |
| 53 | if (parseFlags && arg === '--') { |
| 54 | parseFlags = false; |
| 55 | continue; |
| 56 | } |
| 57 | if (!parseFlags) { |
| 58 | if (!command) command = normalizeCommandAlias(arg); |
| 59 | else positionals.push(arg); |
| 60 | continue; |
| 61 | } |
| 62 | if (shouldPreservePostCommandArgs(command)) { |
| 63 | positionals.push(arg); |
| 64 | continue; |
| 65 | } |
| 66 | const isLongFlag = arg.startsWith('--'); |
| 67 | const isShortFlag = arg.startsWith('-') && arg.length > 1; |
| 68 | if (!isLongFlag && !isShortFlag) { |
| 69 | if (!command) command = normalizeCommandAlias(arg); |
| 70 | else positionals.push(arg); |
| 71 | continue; |
| 72 | } |
| 73 | |
| 74 | const [token, inlineValue] = isLongFlag ? splitLongFlag(arg) : [arg, undefined]; |
| 75 | if (isLegacyIgnoredSnapshotShortFlag(command, token)) { |
| 76 | continue; |
| 77 | } |
| 78 | const definition = resolveFlagDefinition(token, command); |
| 79 | if (shouldPassThroughLocalToolFlag(command, definition)) { |
| 80 | positionals.push(arg); |
| 81 | continue; |
| 82 | } |
| 83 | if (!definition) { |
| 84 | if (shouldTreatUnknownDashTokenAsPositional(command, positionals, arg)) { |
| 85 | if (!command) command = arg; |
| 86 | else positionals.push(arg); |
| 87 | continue; |
| 88 | } |
| 89 | throw new AppError('INVALID_ARGS', `Unknown flag: ${token}`); |
| 90 | } |
| 91 | |
| 92 | const parsed = parseFlagValue(definition, token, inlineValue, argv[i + 1]); |
| 93 | if (parsed.consumeNext) i += 1; |
| 94 | const existingValue = (flags as Record<string, unknown>)[definition.key]; |
| 95 | if (definition.multiple) { |
| 96 | const values = Array.isArray(existingValue) |
| 97 | ? [...existingValue, parsed.value] |
| 98 | : existingValue === undefined |
| 99 | ? [parsed.value] |
| 100 | : [existingValue, parsed.value]; |
no test coverage detected