| 54 | } |
| 55 | |
| 56 | export function parseArgs(argv) { |
| 57 | const options = { dryRun: false, json: false, prune: false }; |
| 58 | |
| 59 | for (let index = 0; index < argv.length; index += 1) { |
| 60 | const arg = argv[index]; |
| 61 | switch (arg) { |
| 62 | case "--": |
| 63 | break; |
| 64 | case "--dry-run": |
| 65 | options.dryRun = true; |
| 66 | break; |
| 67 | case "--prune": |
| 68 | options.prune = true; |
| 69 | break; |
| 70 | case "--json": |
| 71 | options.json = true; |
| 72 | break; |
| 73 | case "--help": |
| 74 | options.help = true; |
| 75 | break; |
| 76 | default: { |
| 77 | if (!VALUE_OPTIONS.has(arg)) { |
| 78 | throw new Error(`Unknown option: ${arg}`); |
| 79 | } |
| 80 | const value = argv[index + 1]; |
| 81 | if (!value || value.startsWith("--")) { |
| 82 | throw new Error(`${arg} requires a value.`); |
| 83 | } |
| 84 | options[toOptionKey(arg)] = value; |
| 85 | index += 1; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return options; |
| 91 | } |
| 92 | |
| 93 | function toOptionKey(arg) { |
| 94 | if (arg === "--profile-root") return "profileRoot"; |