(argv: string[])
| 22 | } |
| 23 | |
| 24 | export function parseArgs(argv: string[]): ParsedArgs { |
| 25 | const options = new Map<string, string | boolean>(); |
| 26 | const multi = new Map<string, string[]>(); |
| 27 | const positionals: string[] = []; |
| 28 | |
| 29 | const pushMulti = (name: string, value: string) => { |
| 30 | const existing = multi.get(name); |
| 31 | if (existing) { |
| 32 | existing.push(value); |
| 33 | } else { |
| 34 | multi.set(name, [value]); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | for (let i = 0; i < argv.length; i++) { |
| 39 | const arg = argv[i]; |
| 40 | |
| 41 | if (arg === '--') { |
| 42 | positionals.push(...argv.slice(i + 1)); |
| 43 | break; |
| 44 | } |
| 45 | |
| 46 | if (arg.startsWith('--')) { |
| 47 | const [name, inlineValue] = arg.slice(2).split('=', 2); |
| 48 | if (inlineValue !== undefined) { |
| 49 | options.set(name, inlineValue); |
| 50 | pushMulti(name, inlineValue); |
| 51 | continue; |
| 52 | } |
| 53 | const next = argv[i + 1]; |
| 54 | if (next !== undefined && !next.startsWith('-')) { |
| 55 | options.set(name, next); |
| 56 | pushMulti(name, next); |
| 57 | i++; |
| 58 | continue; |
| 59 | } |
| 60 | options.set(name, true); |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | if (arg === '-h') { |
| 65 | options.set('help', true); |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | positionals.push(arg); |
| 70 | } |
| 71 | |
| 72 | return { options, multi, positionals }; |
| 73 | } |
| 74 | |
| 75 | export function getString(parsed: ParsedArgs, name: string): string | undefined; |
| 76 | export function getString(options: Map<string, string | boolean>, name: string): string | undefined; |
no test coverage detected