diff --git a/index.js b/index.js index 564cb2ed..343b6c57 100644 --- a/index.js +++ b/index.js @@ -279,8 +279,8 @@ function parse (args, opts) { setConfig(argv) setConfigObjects() applyEnvVars(argv, false) - applyCoercions(argv) applyDefaultsAndAliases(argv, flags.aliases, defaults) + applyCoercions(argv) // for any counts either not in args or without an explicit default, set to 0 Object.keys(flags.counts).forEach(function (key) { diff --git a/test/yargs-parser.js b/test/yargs-parser.js index dac993c9..c2f5f51f 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1992,6 +1992,18 @@ describe('yargs-parser', function () { parsed.foo.bar.should.equal('nananana, batman!') }) + it('applies coercion to defaults', function () { + var parsed = parser([], { + default: { foo: 'bar' }, + coerce: { + foo: function (val) { + return val.toUpperCase() + } + } + }) + parsed.foo.should.equal('BAR') + }) + it('applies coercion function to an implicit array', function () { var parsed = parser(['--foo', '99', '-f', '33'], { coerce: {