// Tiny argv parser: supports `--key value` and `--key=value`. Unknown // positional args are dropped. Good enough for the flag-driven bin shims; the // `archilyzer` CLI's subcommands use parseArgv below, which keeps positionals. export function parseFlags(argv: string[]): Record { const out: Record = {}; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (!a.startsWith("--")) continue; const eq = a.indexOf("="); if (eq > 0) { out[a.slice(2, eq)] = a.slice(eq + 1); } else { const next = argv[i + 1]; if (next && !next.startsWith("--")) { out[a.slice(2)] = next; i++; } else { out[a.slice(2)] = "true"; } } } return out; } // A flag's value: the string it was given, or `true` for a bare `--flag`. export type FlagValue = string | true; /** * The `archilyzer` CLI's argv parser: positionals in order, plus flags. * * `--key=value` always carries its value. `--key value` takes the next word as * the value UNLESS `key` is one of `booleans` (so `build site --nodata jeralyzer` * keeps `jeralyzer` a positional) or the next word is itself a flag; a bare * `--key` is `true`. A lone `--` is skipped rather than treated as the end of * the flags, because `pnpm run build -- --nodata` hands the script its `--` * verbatim and the flag after it must still be read as a flag. `-h` is `--help`. */ export function parseArgv( argv: readonly string[], booleans: Iterable = [], ): { flags: Record; positionals: string[] } { const bool = new Set(booleans); const flags: Record = {}; const positionals: string[] = []; for (let i = 0; i < argv.length; i++) { const a = argv[i]; if (a === "--") continue; if (a === "-h") { flags.help = true; continue; } if (!a.startsWith("--")) { positionals.push(a); continue; } const eq = a.indexOf("="); if (eq > 2) { flags[a.slice(2, eq)] = a.slice(eq + 1); continue; } const key = a.slice(2); const next = argv[i + 1]; if (!bool.has(key) && next !== undefined && !next.startsWith("-")) { flags[key] = next; i++; } else { flags[key] = true; } } return { flags, positionals }; }