Skip to content

Commit

Permalink
type "array" precedes "duplicate-arguments-array"
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Jun 10, 2019
1 parent 8262187 commit 61faf08
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ a configuration file.
* default: `true`
* key: `duplicate-arguments-array`
Should arguments be coerced into an array when duplicated:
Should arguments be coerced into an array when duplicated? This option has no effect on `opts.array`.
```sh
node example.js -x 1 -x 2
Expand Down
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,11 @@ function parse (args, opts) {
var isValueArray = Array.isArray(value)
var duplicate = configuration['duplicate-arguments-array']

// array has higher priority than duplicate
if (!duplicate && isTypeArray) {
duplicate = true
}

// nargs has higher priority than duplicate
if (!duplicate && checkAllAliases(key, flags.nargs)) {
duplicate = true
Expand Down
44 changes: 27 additions & 17 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2200,25 +2200,33 @@ describe('yargs-parser', function () {

parsed['x'].should.equal('b')
})
it('array[string]: keeps only last argument', function () {
var parsed = parser('-x a -x b', {
array: [{ key: 'x', string: true }],
it('array[string]: keeps all arguments - ignores configuration', function () {
var parsed = parser('-x a -x b -y c', {
array: [
{ key: 'x', string: true },
{ key: 'y', string: true }
],
configuration: {
'duplicate-arguments-array': false
}
})

parsed['x'].should.equal('b')
})
it('array[number]: keeps only last argument', function () {
var parsed = parser('-x 1 -x 2', {
array: [{ key: 'x', number: true }],
parsed['x'].should.deep.equal(['a', 'b'])
parsed['y'].should.deep.equal(['c'])
})
it('array[number]: keeps all arguments - ignores configuration', function () {
var parsed = parser('-x 1 -x 2 -y 3', {
array: [
{ key: 'x', number: true },
{ key: 'y', number: true }
],
configuration: {
'duplicate-arguments-array': false
}
})

parsed['x'].should.equal(2)
parsed['x'].should.deep.equal([1, 2])
parsed['y'].should.deep.equal([3])
})
it('does not interfere with nargs', function () {
var parsed = parser('-x a b c -x o p q', {
Expand Down Expand Up @@ -2327,15 +2335,16 @@ describe('yargs-parser', function () {
})
parsed['x'].should.deep.equal([1, 2, 3])
})
it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () {
var parsed = parser('-x 1 2 3 -x 2 3 4', {
array: ['x'],
it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]]', function () {
var parsed = parser('-x 1 2 3 -x 2 3 4 -y 7', {
array: ['x', 'y'],
configuration: {
'duplicate-arguments-array': false,
'flatten-duplicate-arrays': false
}
})
parsed['x'].should.deep.equal([2, 3, 4])
parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]])
parsed['y'].should.deep.equal([7])
})
})
describe('type=number', function () {
Expand Down Expand Up @@ -2363,15 +2372,16 @@ describe('yargs-parser', function () {
})
parsed['x'].should.deep.equal([1, 2, 3])
})
it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () {
var parsed = parser('-x 1 2 3 -x 2 3 4', {
array: ['x'],
it('[-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]', function () {
var parsed = parser('-x 1 2 3 -x 2 3 4 -y 7', {
array: ['x', 'y'],
configuration: {
'duplicate-arguments-array': false,
'flatten-duplicate-arrays': true
}
})
parsed['x'].should.deep.equal([2, 3, 4])
parsed['x'].should.deep.equal([1, 2, 3, 2, 3, 4])
parsed['y'].should.deep.equal([7])
})
})
describe('type=number', function () {
Expand Down

0 comments on commit 61faf08

Please sign in to comment.