Skip to content

Commit cc4dc56

Browse files
nexdrewbcoe
authored andcommitted
feat: coerce full array instead of each element (#51)
* feat: call coerce once for explicit array * support coercion for argv._ as an implicit array
1 parent 2fa84bd commit cc4dc56

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ function parse (args, opts) {
280280
setConfig(argv)
281281
setConfigObjects()
282282
applyEnvVars(argv, false)
283+
applyArrayCoercions(argv)
283284
applyDefaultsAndAliases(argv, flags.aliases, defaults)
284285

285286
// for any counts either not in args or without an explicit default, set to 0
@@ -482,6 +483,22 @@ function parse (args, opts) {
482483
})
483484
}
484485

486+
function applyArrayCoercions (argv) {
487+
var coerce
488+
Object.keys(argv).filter(function (key) {
489+
return key === '_' || checkAllAliases(key, flags.arrays)
490+
}).forEach(function (key) {
491+
coerce = checkAllAliases(key, flags.coercions)
492+
if (typeof coerce === 'function') {
493+
try {
494+
argv[key] = coerce(argv[key])
495+
} catch (err) {
496+
error = err
497+
}
498+
}
499+
})
500+
}
501+
485502
function applyDefaultsAndAliases (obj, aliases, defaults) {
486503
Object.keys(defaults).forEach(function (key) {
487504
if (!hasKey(obj, key.split('.'))) {
@@ -521,7 +538,7 @@ function parse (args, opts) {
521538
})
522539

523540
var key = keys[keys.length - 1]
524-
var coerce = checkAllAliases(key, flags.coercions)
541+
var coerce = !checkAllAliases(key, flags.arrays) && checkAllAliases(key, flags.coercions)
525542
if (typeof coerce === 'function') {
526543
try {
527544
value = coerce(value)

test/yargs-parser.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,13 +1980,30 @@ describe('yargs-parser', function () {
19801980
parsed.f.should.equal(-99)
19811981
})
19821982

1983-
it('applies coercion function to an array', function () {
1983+
it('applies coercion function to an implicit array', function () {
19841984
var parsed = parser(['--foo', '99', '-f', '33'], {
19851985
coerce: {
19861986
f: function (arg) {
19871987
return arg * -1
19881988
}
19891989
},
1990+
alias: {
1991+
f: ['foo']
1992+
}
1993+
})
1994+
parsed.f.should.deep.equal([-99, -33])
1995+
parsed.foo.should.deep.equal([-99, -33])
1996+
})
1997+
1998+
it('applies coercion function to an explicit array', function () {
1999+
var parsed = parser(['--foo', '99', '-f', '33'], {
2000+
coerce: {
2001+
f: function (arg) {
2002+
return arg.map(function (a) {
2003+
return a * -1
2004+
})
2005+
}
2006+
},
19902007
array: ['foo'],
19912008
alias: {
19922009
f: ['foo']
@@ -1996,6 +2013,19 @@ describe('yargs-parser', function () {
19962013
parsed.foo.should.deep.equal([-99, -33])
19972014
})
19982015

2016+
it('applies coercion function to _', function () {
2017+
var parsed = parser(['99', '33'], {
2018+
coerce: {
2019+
_: function (arg) {
2020+
return arg.map(function (a) {
2021+
return a * -1
2022+
})
2023+
}
2024+
}
2025+
})
2026+
parsed._.should.deep.equal([-99, -33])
2027+
})
2028+
19992029
// see: https://github.com/yargs/yargs/issues/550
20002030
it('coercion function can be used to parse large #s', function () {
20012031
var fancyNumberParser = function (arg) {
@@ -2014,7 +2044,7 @@ describe('yargs-parser', function () {
20142044
parsed.bar.should.equal(998)
20152045
})
20162046

2017-
it('populates argv.error, if an error is returned', function () {
2047+
it('populates argv.error, if an error is thrown', function () {
20182048
var parsed = parser.detailed(['--foo', '99'], {
20192049
coerce: {
20202050
foo: function (arg) {
@@ -2024,6 +2054,18 @@ describe('yargs-parser', function () {
20242054
})
20252055
parsed.error.message.should.equal('banana')
20262056
})
2057+
2058+
it('populates argv.error, if an error is thrown for an explicit array', function () {
2059+
var parsed = parser.detailed(['--foo', '99'], {
2060+
array: ['foo'],
2061+
coerce: {
2062+
foo: function (arg) {
2063+
throw Error('foo is array: ' + Array.isArray(arg))
2064+
}
2065+
}
2066+
})
2067+
parsed.error.message.should.equal('foo is array: true')
2068+
})
20272069
})
20282070

20292071
// see: https://github.com/yargs/yargs-parser/issues/37

0 commit comments

Comments
 (0)