Repository navigation
fix: ignore the configured default for an option with zero narg - #570
Closed
naveentehrpariya wants to merge 1 commit into
Closed
naveentehrpariya wants to merge 1 commit into
naveentehrpariya wants to merge 1 commit into
Conversation
An option configured with `narg: 0` takes no arguments, so passing it is
a switch and its value should be the one implied by its type. `eatNargs`
instead called `defaultValue`, which returns the configured `default` for
any option not declared `boolean` or `count`, so a `default` of `false`
made the option parse as `false` even when it was given:
parser(['-b'], { narg: { b: 0 }, default: { b: false } })
// => { b: false }, expected { b: true }
Use the type's value directly. The configured default still applies when
the option is absent.
Fixes yargs#418
naveentehrpariya
force-pushed
the
fix-narg-zero-default
branch
from
November 8, 2026 10:44
6167c53 to
6ae0c3f
Compare
Member
|
Unsolicited Pull Requests may be closed without discussion. For more see Contributing and AI Policy. |
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.
Fixes #418.
The bug
An option configured with
narg: 0takes no arguments, so passing it is a switch — the existing testshould not use next value for boolean/number/string configured with zero nargasserts exactly that:bool→true,nr→undefined,str→''.But
eatNargsproduces that value throughdefaultValue(), which returns the configureddefaultfor any option that is not declaredbooleanorcount:So adding a
defaultoffalsemakes the option parse asfalseeven when it is given on the command line:Declaring the option
boolean: trueworks, which is why this only shows up for options that usenargto accept either a value or no value at all.The fix
eatNargsnow takes the value implied by the option's type directly, instead of going throughdefaultValue(). Adefaultis the value an option takes when it is absent, so it should not be consulted for an option that was given.Tests
narg: 0and adefaultoffalseparses astrue(along with its aliases) when it is passed. Fails onmainwithexpected … to have property 'b' of true, but got false.defaultstill applies when the option is not passed.npm testis 364 passing / 0 failing,npm run test:typescript20 passing,npm run checkclean.