From 77ae1d4e1c2590eeca025952671fff935ab7e884 Mon Sep 17 00:00:00 2001 From: Volker Mische Date: Sat, 6 Oct 2018 21:54:00 +0200 Subject: [PATCH] fix: make requiresArg work in conjunction with arrays (#136) --- index.js | 8 ++++---- test/yargs-parser.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index caf8a646..e0054fd0 100644 --- a/index.js +++ b/index.js @@ -167,12 +167,12 @@ function parse (args, opts) { )) { key = arg.match(/^--?(.+)/)[1] - // nargs format = '--foo a b c' - if (checkAllAliases(key, flags.nargs)) { - i = eatNargs(i, key, args) // array format = '--foo a b c' - } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) { + if (checkAllAliases(key, flags.arrays) && args.length > i + 1) { i = eatArray(i, key, args) + // nargs format = '--foo a b c' + } else if (checkAllAliases(key, flags.nargs)) { + i = eatNargs(i, key, args) } else { next = args[i + 1] diff --git a/test/yargs-parser.js b/test/yargs-parser.js index b6e2396e..e483536e 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1702,6 +1702,17 @@ describe('yargs-parser', function () { result.foo.should.eql('a') }) + + it('should be ignored if input is an array', function () { + var result = parser(['--foo', 'a', 'b'], { + array: 'foo', + narg: { + foo: 1 + } + }) + + result.foo.should.eql(['a', 'b']) + }) }) describe('env vars', function () {