refactor(bench): use node:util parseArgs for CLI parsing#293
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace the hand-rolled flag-loop in bench/index.js with
parseArgsfromnode:util— available since Node 22, which isengines.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:
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'sstrict: truemode. Previously a typo like--lng en-USwould 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/--languagealiases, 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— worksnode bench/index.js en-US(positional) — runs single languagenode bench/index.js --bogus-flag— fails fast with parseArgs errornode bench/index.js --function nonexistent— preserves friendly error + exit 1npx standard bench/index.js— lint clean🤖 Generated with Claude Code