Skip to content

Commit 61faf08

Browse files
committed
type "array" precedes "duplicate-arguments-array"
1 parent 8262187 commit 61faf08

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ a configuration file.
228228
* default: `true`
229229
* key: `duplicate-arguments-array`
230230
231-
Should arguments be coerced into an array when duplicated:
231+
Should arguments be coerced into an array when duplicated? This option has no effect on `opts.array`.
232232
233233
```sh
234234
node example.js -x 1 -x 2

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,11 @@ function parse (args, opts) {
686686
var isValueArray = Array.isArray(value)
687687
var duplicate = configuration['duplicate-arguments-array']
688688

689+
// array has higher priority than duplicate
690+
if (!duplicate && isTypeArray) {
691+
duplicate = true
692+
}
693+
689694
// nargs has higher priority than duplicate
690695
if (!duplicate && checkAllAliases(key, flags.nargs)) {
691696
duplicate = true

test/yargs-parser.js

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,25 +2200,33 @@ describe('yargs-parser', function () {
22002200

22012201
parsed['x'].should.equal('b')
22022202
})
2203-
it('array[string]: keeps only last argument', function () {
2204-
var parsed = parser('-x a -x b', {
2205-
array: [{ key: 'x', string: true }],
2203+
it('array[string]: keeps all arguments - ignores configuration', function () {
2204+
var parsed = parser('-x a -x b -y c', {
2205+
array: [
2206+
{ key: 'x', string: true },
2207+
{ key: 'y', string: true }
2208+
],
22062209
configuration: {
22072210
'duplicate-arguments-array': false
22082211
}
22092212
})
22102213

2211-
parsed['x'].should.equal('b')
2212-
})
2213-
it('array[number]: keeps only last argument', function () {
2214-
var parsed = parser('-x 1 -x 2', {
2215-
array: [{ key: 'x', number: true }],
2214+
parsed['x'].should.deep.equal(['a', 'b'])
2215+
parsed['y'].should.deep.equal(['c'])
2216+
})
2217+
it('array[number]: keeps all arguments - ignores configuration', function () {
2218+
var parsed = parser('-x 1 -x 2 -y 3', {
2219+
array: [
2220+
{ key: 'x', number: true },
2221+
{ key: 'y', number: true }
2222+
],
22162223
configuration: {
22172224
'duplicate-arguments-array': false
22182225
}
22192226
})
22202227

2221-
parsed['x'].should.equal(2)
2228+
parsed['x'].should.deep.equal([1, 2])
2229+
parsed['y'].should.deep.equal([3])
22222230
})
22232231
it('does not interfere with nargs', function () {
22242232
var parsed = parser('-x a b c -x o p q', {
@@ -2327,15 +2335,16 @@ describe('yargs-parser', function () {
23272335
})
23282336
parsed['x'].should.deep.equal([1, 2, 3])
23292337
})
2330-
it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () {
2331-
var parsed = parser('-x 1 2 3 -x 2 3 4', {
2332-
array: ['x'],
2338+
it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]]', function () {
2339+
var parsed = parser('-x 1 2 3 -x 2 3 4 -y 7', {
2340+
array: ['x', 'y'],
23332341
configuration: {
23342342
'duplicate-arguments-array': false,
23352343
'flatten-duplicate-arrays': false
23362344
}
23372345
})
2338-
parsed['x'].should.deep.equal([2, 3, 4])
2346+
parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]])
2347+
parsed['y'].should.deep.equal([7])
23392348
})
23402349
})
23412350
describe('type=number', function () {
@@ -2363,15 +2372,16 @@ describe('yargs-parser', function () {
23632372
})
23642373
parsed['x'].should.deep.equal([1, 2, 3])
23652374
})
2366-
it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () {
2367-
var parsed = parser('-x 1 2 3 -x 2 3 4', {
2368-
array: ['x'],
2375+
it('[-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]', function () {
2376+
var parsed = parser('-x 1 2 3 -x 2 3 4 -y 7', {
2377+
array: ['x', 'y'],
23692378
configuration: {
23702379
'duplicate-arguments-array': false,
23712380
'flatten-duplicate-arrays': true
23722381
}
23732382
})
2374-
parsed['x'].should.deep.equal([2, 3, 4])
2383+
parsed['x'].should.deep.equal([1, 2, 3, 2, 3, 4])
2384+
parsed['y'].should.deep.equal([7])
23752385
})
23762386
})
23772387
describe('type=number', function () {

0 commit comments

Comments
 (0)