/** * Copyright 2026 aphrody-code * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * @module bxc/cluster/cli-args * * Minimal flag parser shared by `bxc worker` and `bxc serve --agent`. */ import type { CrawlTier } from "../crawler/MassiveCrawler.ts"; export interface ParsedFlags { /** Last value of each valued flag (`--name value` or `--name=value`). */ values: Map; /** Every value of multi-value flags (`--seed a b --seed c`). */ lists: Map; /** Boolean flags present. */ booleans: Set; positionals: string[]; } export interface FlagSpec { /** Flags without a value. */ booleans?: readonly string[]; /** Flags that take every following non-flag argument. */ lists?: readonly string[]; } export function parseFlags(argv: readonly string[], spec: FlagSpec = {}): ParsedFlags { const booleanSet = new Set(spec.booleans ?? []); const listSet = new Set(spec.lists ?? []); const parsed: ParsedFlags = { values: new Map(), lists: new Map(), booleans: new Set(), positionals: [] }; for (let i = 0; i < argv.length; i++) { const arg = argv[i] as string; if (!arg.startsWith("--") || arg === "--") { if (arg !== "--") parsed.positionals.push(arg); continue; } const eq = arg.indexOf("="); const name = eq > 0 ? arg.slice(2, eq) : arg.slice(2); if (booleanSet.has(name)) { parsed.booleans.add(name); continue; } if (listSet.has(name)) { const list = parsed.lists.get(name) ?? []; if (eq > 0) list.push(arg.slice(eq + 1)); else while (i + 1 < argv.length && !(argv[i + 1] as string).startsWith("--")) list.push(argv[++i] as string); parsed.lists.set(name, list); continue; } const value = eq > 0 ? arg.slice(eq + 1) : argv[++i]; if (value === undefined) throw new Error(`Missing value for --${name}`); parsed.values.set(name, value); } return parsed; } /** Positive integer flag value, or `undefined` when absent. */ export function intFlag(flags: ParsedFlags, name: string): number | undefined { const raw = flags.values.get(name); if (raw === undefined) return undefined; const value = Number.parseInt(raw, 10); if (!Number.isInteger(value) || value < 0) throw new Error(`Invalid --${name}: ${raw}`); return value; } const TIERS: readonly CrawlTier[] = ["http", "render", "fast", "stealth", "max"]; /** `http,render,stealth` → validated tier list. */ export function parseLadder(raw: string | undefined): CrawlTier[] | undefined { if (!raw) return undefined; const ladder = raw .split(",") .map((t) => t.trim()) .filter(Boolean); for (const tier of ladder) { if (!TIERS.includes(tier as CrawlTier)) { throw new Error(`Unknown tier "${tier}" in --ladder (expected ${TIERS.join(", ")})`); } } return ladder as CrawlTier[]; }