Skip to content

refactor(bench): use node:util parseArgs for CLI parsing#293

Merged
TylerVigario merged 1 commit into
mainfrom
refactor/bench-parseargs
May 8, 2026
Merged

refactor(bench): use node:util parseArgs for CLI parsing#293
TylerVigario merged 1 commit into
mainfrom
refactor/bench-parseargs

Conversation

@TylerVigario
Copy link
Copy Markdown
Collaborator

Summary

Replace the hand-rolled flag-loop in bench/index.js with parseArgs from node:util — available since Node 22, which is engines.node's floor.

The previous parser was ~62 lines of imperative "skip the next arg if the previous was a flag" bookkeeping. The new version is a flat options declaration:

parseArgs({
  options: {
    lang: { type: 'string', multiple: true },
    language: { type: 'string', multiple: true },
    function: { type: 'string', multiple: true },
    fn: { type: 'string', multiple: true },
    value: { type: 'string' },
    save: { type: 'boolean' },
    // ...
  },
  allowPositionals: true,
  strict: true
})

Net +48 / -62. Each flag's name, type, and multi-occurrence behavior is visible at a glance.

Behavior change

Unknown flags now throw via parseArgs's strict: true mode. Previously a typo like --lng en-US would silently fall through and the bench would run with default arguments. Failing fast is the right tradeoff for a benchmark CLI — you find out at parse time, not after a 30-second run that benchmarked the wrong thing.

All other behaviors preserved: --lang/--language aliases, comma-separated values within a single flag, repeating flags (--lang en --lang fr), positionals as language codes, unknown function name validation with the existing red error message.

Test plan

  • node bench/index.js --help — works
  • node bench/index.js en-US (positional) — runs single language
  • node bench/index.js --bogus-flag — fails fast with parseArgs error
  • node bench/index.js --function nonexistent — preserves friendly error + exit 1
  • npx standard bench/index.js — lint clean

🤖 Generated with Claude Code

Replace the hand-rolled flag-loop with parseArgs from node:util
(available since Node 22, the engines.node floor).

The new parser is declarative — each flag's name, type, and
multi-occurrence behavior are visible at a glance — and shorter:
~62 lines of imperative parsing become ~48 lines of options
declaration plus aggregation.

One behavior change: unknown flags now throw via parseArgs's
strict mode. The previous loop silently ignored them, so a typo
like `--lng en-US` would parse cleanly and use defaults. Strict
mode surfaces typos at parse time, which is the right tradeoff
for a benchmark CLI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@TylerVigario TylerVigario merged commit 55e2c63 into main May 8, 2026
7 checks passed
@TylerVigario TylerVigario deleted the refactor/bench-parseargs branch May 8, 2026 03:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant