diff --git a/package.json b/package.json index dd85cfd51..07fd9b87c 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "string-width": "^4.2.0", "which-module": "^2.0.0", "y18n": "^4.0.0", - "yargs-parser": "^17.1.0" + "yargs-parser": "^18.0.0" }, "devDependencies": { "c8": "^7.0.0", diff --git a/test/yargs.js b/test/yargs.js index 389a50711..0c0d9133e 100644 --- a/test/yargs.js +++ b/test/yargs.js @@ -2428,4 +2428,76 @@ describe('yargs dsl tests', () => { }, 5) }) }) + + // See: https://github.com/yargs/yargs/issues/1098 + it('should allow array and requires arg to be used in conjunction', () => { + const argv = yargs(['-i', 'item1', 'item2', 'item3']) + .option('i', { + alias: 'items', + type: 'array', + requiresArg: true + }) + .argv + argv.items.should.eql(['item1', 'item2', 'item3']) + argv.i.should.eql(['item1', 'item2', 'item3']) + }) + + // See: https://github.com/yargs/yargs/issues/1570 + describe('"nargs" with "array"', () => { + it('should not consume more than nargs items', () => { + const argv = yargs(['-i', 'item1', 'item2', '-i', 'item3', 'item4']) + .option('i', { + alias: 'items', + type: 'array', + nargs: 1 + }) + .argv + argv.items.should.eql(['item1', 'item3']) + argv.i.should.eql(['item1', 'item3']) + argv._.should.eql(['item2', 'item4']) + }) + + it('should apply nargs with higher precedence than requiresArg: true', () => { + const argv = yargs(['-i', 'item1', 'item2', '-i', 'item3', 'item4']) + .option('i', { + alias: 'items', + type: 'array', + nargs: 1, + requiresArg: true + }) + .argv + argv.items.should.eql(['item1', 'item3']) + argv.i.should.eql(['item1', 'item3']) + argv._.should.eql(['item2', 'item4']) + }) + + // TODO: make this work with aliases, using a check similar to + // checkAllAliases() in yargs-parser. + it('should apply nargs with higher precedence than requiresArg()', () => { + const argv = yargs(['-i', 'item1', 'item2', '-i', 'item3', 'item4']) + .option('items', { + alias: 'i', + type: 'array', + nargs: 1 + }) + .requiresArg(['items']) + .argv + argv.items.should.eql(['item1', 'item3']) + argv.i.should.eql(['item1', 'item3']) + argv._.should.eql(['item2', 'item4']) + }) + + it('should raise error if not enough values follow nargs key', (done) => { + yargs + .option('i', { + alias: 'items', + type: 'array', + nargs: 1 + }) + .parse(['-i'], (err) => { + err.message.should.match(/Not enough arguments following: i/) + return done() + }) + }) + }) }) diff --git a/yargs.js b/yargs.js index c32066020..92474737e 100644 --- a/yargs.js +++ b/yargs.js @@ -235,9 +235,18 @@ function Yargs (processArgs, cwd, parentRequire) { return self } - self.requiresArg = function (keys) { - argsert('', [keys], arguments.length) - populateParserHintObject(self.nargs, false, 'narg', keys, 1) + self.requiresArg = function (keys, value) { + argsert(' [number]', [keys], arguments.length) + // If someone configures nargs at the same time as requiresArg, + // nargs should take precedent, + // see: https://github.com/yargs/yargs/pull/1572 + // TODO: make this work with aliases, using a check similar to + // checkAllAliases() in yargs-parser. + if (typeof keys === 'string' && options.narg[keys]) { + return self + } else { + populateParserHintObject(self.requiresArg, false, 'narg', keys, NaN) + } return self }