(argv: string[])
| 21 | } |
| 22 | |
| 23 | function parseArgs(argv: string[]): ParsedArgs { |
| 24 | const args = argv.slice(2); |
| 25 | if (args.length === 0) { |
| 26 | printUsage(); |
| 27 | process.exit(ExitCode.Success); |
| 28 | } |
| 29 | |
| 30 | // First non-flag arg is the command. |
| 31 | let command = ""; |
| 32 | const positional: string[] = []; |
| 33 | const flags: Record<string, string | boolean> = {}; |
| 34 | |
| 35 | for (let i = 0; i < args.length; i++) { |
| 36 | const a = args[i]; |
| 37 | if (a.startsWith("--")) { |
| 38 | const key = a.slice(2); |
| 39 | const next = args[i + 1]; |
| 40 | if (next !== undefined && !next.startsWith("--")) { |
| 41 | flags[key] = next; |
| 42 | i++; |
| 43 | } else { |
| 44 | flags[key] = true; |
| 45 | } |
| 46 | } else if (!command) { |
| 47 | command = a; |
| 48 | } else { |
| 49 | positional.push(a); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return { command, positional, flags }; |
| 54 | } |
| 55 | |
| 56 | function printUsage(): void { |
| 57 | const lines = [ |
no test coverage detected