Skip to content

Commit

Permalink
defer coercion completely until after parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrimue committed Sep 5, 2016
1 parent aeed711 commit 1bd95e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
16 changes: 3 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ function parse (args, opts) {
setConfig(argv)
setConfigObjects()
applyEnvVars(argv, false)
applyArrayCoercions(argv)
applyCoercions(argv)
applyDefaultsAndAliases(argv, flags.aliases, defaults)

// for any counts either not in args or without an explicit default, set to 0
Expand Down Expand Up @@ -483,11 +483,9 @@ function parse (args, opts) {
})
}

function applyArrayCoercions (argv) {
function applyCoercions (argv) {
var coerce
Object.keys(argv).filter(function (key) {
return key === '_' || checkAllAliases(key, flags.arrays)
}).forEach(function (key) {
Object.keys(argv).forEach(function (key) {
coerce = checkAllAliases(key, flags.coercions)
if (typeof coerce === 'function') {
try {
Expand Down Expand Up @@ -538,14 +536,6 @@ function parse (args, opts) {
})

var key = keys[keys.length - 1]
var coerce = !checkAllAliases(key, flags.arrays) && checkAllAliases(key, flags.coercions)
if (typeof coerce === 'function') {
try {
value = coerce(value)
} catch (err) {
error = err
}
}

if (value === increment) {
o[key] = increment(o[key])
Expand Down
7 changes: 5 additions & 2 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1984,7 +1984,8 @@ describe('yargs-parser', function () {
var parsed = parser(['--foo.bar', 'nananana'], {
coerce: {
foo: function (val) {
return val + ', batman!'
val.bar += ', batman!'
return val
}
}
})
Expand All @@ -1995,7 +1996,9 @@ describe('yargs-parser', function () {
var parsed = parser(['--foo', '99', '-f', '33'], {
coerce: {
f: function (arg) {
return arg * -1
return arg.map(function (a) {
return a * -1
})
}
},
alias: {
Expand Down

0 comments on commit 1bd95e9

Please sign in to comment.