Skip to content

fix: do not satisfy array narg with defaults for bare flags - #562

Closed
webywz wants to merge 1 commit into
yargs:mainfrom
webywz:fix/array-default-narg-requires-arg
Closed

webywz wants to merge 1 commit into
yargs:mainfrom
webywz:fix/array-default-narg-requires-arg

Conversation

@webywz

@webywz webywz commented Oct 30, 2026

Copy link
Copy Markdown

Summary

Fixes #445.

When an option is array + narg/requiresArg + default, passing the flag with no following value used to load the default into argsToSet inside eatArray, which made the nargs length check pass. Non-array options already error with Not enough arguments following: <key> in the same situation.

Change

In eatArray, skip filling from defaults when narg requires at least one value (numeric count > 0 or NaN). The existing nargs check then reports the error. Defaults still apply when the flag is omitted entirely (applyDefaultsAndAliases), and a bare flag without narg still gets the array default as before.

Test plan

  • Added regressions under array with nargs for #445 (narg 1 and NaN with default; bare flag omitted; bare flag without narg)
  • npm test — 369 passing

When an array option is configured with both a default and narg /
requiresArg, providing the flag without a following value used to fill
argsToSet from the default and skip the nargs length check. Non-array
options already report "Not enough arguments following" in that case.

Leave argsToSet empty when nargs requires a value so the existing check
fires; defaults still apply when the flag is omitted entirely.

Fixes yargs#445
@shadowspawn

Copy link
Copy Markdown
Member

Unsolicited Pull Requests may be closed without discussion.

For more see Contributing and AI Policy.

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.

array w/ default breaks requiresArg

2 participants