Skip to content

Commit 77ae1d4

Browse files
vmxbcoe
authored andcommitted
fix: make requiresArg work in conjunction with arrays (#136)
1 parent 7df1bd4 commit 77ae1d4

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ function parse (args, opts) {
167167
)) {
168168
key = arg.match(/^--?(.+)/)[1]
169169

170-
// nargs format = '--foo a b c'
171-
if (checkAllAliases(key, flags.nargs)) {
172-
i = eatNargs(i, key, args)
173170
// array format = '--foo a b c'
174-
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
171+
if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
175172
i = eatArray(i, key, args)
173+
// nargs format = '--foo a b c'
174+
} else if (checkAllAliases(key, flags.nargs)) {
175+
i = eatNargs(i, key, args)
176176
} else {
177177
next = args[i + 1]
178178

test/yargs-parser.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,17 @@ describe('yargs-parser', function () {
17021702

17031703
result.foo.should.eql('a')
17041704
})
1705+
1706+
it('should be ignored if input is an array', function () {
1707+
var result = parser(['--foo', 'a', 'b'], {
1708+
array: 'foo',
1709+
narg: {
1710+
foo: 1
1711+
}
1712+
})
1713+
1714+
result.foo.should.eql(['a', 'b'])
1715+
})
17051716
})
17061717

17071718
describe('env vars', function () {

0 commit comments

Comments
 (0)