{"record":{"id":"870db6a8e5de6fa7","repo":"JuliusBrussee/caveman","slug":"duplicate-arg","errorCode":null,"errorMessage":"duplicate ${arg}","messagePattern":"duplicate (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/stats-cli.ts","lineNumber":38,"sourceCode":"API cost estimates and subscription API equivalents are separate. Neither\nproves an invoice reduction. Unknown models and billing modes stay unpriced.\n`;\n\nexport type StatsCLIOptions = {\n  json: boolean;\n  plain: boolean;\n  open: boolean;\n  out?: string;\n  filters: string[];\n};\n\nexport function parseStatsOptions(argv: string[]): StatsCLIOptions {\n  const result: StatsCLIOptions = { json: false, plain: false, open: false, filters: [] };\n  const seen = new Set<string>();\n  const valueFlags = new Set([\"--days\", \"--provider\", \"--model\", \"--agent\", \"--auth\", \"--out\"]);\n  for (let i = 0; i < argv.length; i++) {\n    const arg = argv[i]!;\n    if (seen.has(arg)) throw new Error(`duplicate ${arg}`);\n    seen.add(arg);\n    if (valueFlags.has(arg)) {\n      const value = argv[++i];\n      if (!value || value.startsWith(\"--\") || /[\\u0000-\\u001f\\u007f]/u.test(value)) throw new Error(`${arg} requires a value`);\n      if (arg === \"--days\" && (!/^\\d+$/u.test(value) || Number(value) < 1 || Number(value) > 3660)) {\n        throw new Error(\"--days must be an integer from 1 to 3660\");\n      }\n      if (arg === \"--out\") result.out = value;\n      else result.filters.push(arg, value);\n    } else if (arg === \"--json\") result.json = true;\n    else if (arg === \"--plain\" || arg === \"--no-open\") result.plain = true;\n    else if (arg === \"--open\") result.open = true;\n    else if (arg === \"--all-time\") result.filters.push(\"--days\", \"0\");\n    else throw new Error(`unknown option ${arg}`);\n  }\n  if (seen.has(\"--days\") && seen.has(\"--all-time\")) throw new Error(\"choose --days or --all-time\");\n  if (result.plain && result.open) throw new Error(\"choose --plain or --open\");\n  if (result.json && result.open) throw new Error(\"--json cannot open a browser\");","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/stats-cli.ts#L20-L56","documentation":"parseStatsOptions rejects repeating any argument on the command line, tracking every flag in a Set. Passing the same flag twice (even a value flag with different values) is treated as a likely mistake and throws rather than silently taking the last occurrence.","triggerScenarios":"Running the stats CLI with the same flag twice, e.g. `--days 7 --days 30`, `--model a --model b`, or repeating boolean flags like `--json --json` (including via shell alias or script that appends flags).","commonSituations":"Wrapper scripts that append their own flags onto a user-provided string that already contains them; copy-paste errors; composability where two config layers both add --provider.","solutions":["Remove the duplicated flag, keeping a single occurrence","If the duplicate comes from a wrapper script, deduplicate flags before invoking the CLI (or pass the final value only)","Check the exact CLI usage printed by the error: it echoes the duplicated arg name"],"exampleFix":"// before\nmyapp stats --days 7 --days 30\n// after\nmyapp stats --days 30","handlingStrategy":"validation","validationCode":"const flags = argv.filter(a => a.startsWith(\"--\"));\nconst dupes = flags.filter((f, i) => flags.indexOf(f) !== i);\nif (dupes.length) throw new Error(`duplicate ${dupes[0]}`);","typeGuard":null,"tryCatchPattern":"try {\n  const opts = parseStatsOptions(argv);\n} catch (e) {\n  if (String(e.message).startsWith(\"duplicate \")) {\n    console.error(e.message); process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Deduplicate flag arrays in wrapper scripts before invoking the CLI","Avoid building argv by string concatenation of config layers","Use the last-occurrence convention in your own wrappers instead of forwarding duplicates"],"tags":["cli","argument-parsing"],"backgroundTag":"duplicate-argument","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}