Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type "array" with "duplicate-arguments-array" #163

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
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
7 changes: 7 additions & 0 deletions index.js
Expand Up @@ -497,6 +497,8 @@ function parse (args, opts) {
}

function maybeCoerceNumber (key, value) {
if (Array.isArray(value)) return value

if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (
Number.isSafeInteger(Math.floor(value))
Expand Down Expand Up @@ -684,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
80 changes: 61 additions & 19 deletions test/yargs-parser.js
Expand Up @@ -2191,7 +2191,7 @@ describe('yargs-parser', function () {

parsed['x'].should.deep.equal(['a', 'b'])
})
it('keeps only last argument', function () {
it('string: keeps only last argument', function () {
var parsed = parser('-x a -x b', {
configuration: {
'duplicate-arguments-array': false
Expand All @@ -2200,6 +2200,38 @@ describe('yargs-parser', function () {

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

parsed['x'].should.deep.equal(['a', 'b'])
parsed['y'].should.deep.equal(['c'])
parsed['z'].should.deep.equal(['d', 'e', 'f', 'g'])
})
it('number[]]: keeps all arguments - ignores configuration', function () {
var parsed = parser('-x 1 -x 2 -y 3 -z 4 5 -z 6 7', {
array: [
{ key: 'x', number: true },
{ key: 'y', number: true },
{ key: 'z', number: true }
],
configuration: {
'duplicate-arguments-array': false
}
})

parsed['x'].should.deep.equal([1, 2])
parsed['y'].should.deep.equal([3])
parsed['z'].should.deep.equal([4, 5, 6, 7])
})
it('does not interfere with nargs', function () {
var parsed = parser('-x a b c -x o p q', {
narg: { x: 3 },
Expand Down Expand Up @@ -2270,14 +2302,14 @@ describe('yargs-parser', function () {
duplicate=false, flatten=false
type=array
[-x 1 2 3] => [1, 2, 3]
[-x 1 2 3 -x 2 3 4] => [2, 3, 4]
[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]]
type=string/number/etc
[-x 1 -x 2 -x 3] => 3

duplicate=false, flatten=true
type=array
[-x 1 2 3] => [1, 2, 3]
[-x 1 2 3 -x 2 3 4] => [2, 3, 4]
[-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]
type=string/number/etc
[-x 1 -x 2 -x 3] => 3

Expand Down Expand Up @@ -2307,15 +2339,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 @@ -2343,15 +2376,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 Expand Up @@ -2405,25 +2439,33 @@ describe('yargs-parser', function () {
})
describe('duplicate=true, flatten=false,', function () {
describe('type=array', function () {
it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () {
var parsed = parser('-x 1 -x 2 -x 3', {
array: ['x'],
it('number[]: [-x 1 -x 2 -x 3] => [[1], [2], [3]]', function () {
var parsed = parser('-x 1 -x 2 -x 3 -y 4 5 -y 6 7', {
array: [
{ key: 'x', number: true },
{ key: 'y', number: true }
],
configuration: {
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': false
}
})
parsed['x'].should.deep.equal([1, 2, 3])
parsed['x'].should.deep.equal([[1], [2], [3]])
parsed['y'].should.deep.equal([[4, 5], [6, 7]])
})
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', {
array: ['x'],
it("string[]: [-x 1 -x 2 -x 3] => [['1'], ['2'], ['3']]", function () {
var parsed = parser('-x 1 -x 2 -x 3 -y 4 5 -y 6 7', {
array: [
{ key: 'x', string: true },
{ key: 'y', string: true }
],
configuration: {
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': false
}
})
parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]])
parsed['x'].should.deep.equal([['1'], ['2'], ['3']])
parsed['y'].should.deep.equal([['4', '5'], ['6', '7']])
})
})
describe('type=number', function () {
Expand Down