diff --git a/lib/validation.ts b/lib/validation.ts index fbd9c18cc..79017d26b 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -20,7 +20,8 @@ export function validation (yargs: YargsInstance, usage: UsageInstance, y18n: Y1 self.nonOptionCount = function nonOptionCount (argv) { const demandedCommands = yargs.getDemandedCommands() // don't count currently executing commands - const _s = argv._.length - yargs.getContext().commands.length + const positionalCount = argv._.length + (argv['--'] ? argv['--'].length : 0) + const _s = positionalCount - yargs.getContext().commands.length if (demandedCommands._ && (_s < demandedCommands._.min || _s > demandedCommands._.max)) { if (_s < demandedCommands._.min) { diff --git a/test/validation.cjs b/test/validation.cjs index eb21323ba..01be04585 100644 --- a/test/validation.cjs +++ b/test/validation.cjs @@ -952,6 +952,16 @@ describe('validation tests', () => { }) .parse() }) + + // See: https://github.com/yargs/yargs/issues/1732 + it('treats positionals in "--" towards count requirement', () => { + const argv = yargs('--cool man -- batman robin') + .demandCommand(2) + .fail((msg) => { + throw Error(msg) + }) + .parse() + }) }) describe('strictCommands', () => {