Skip to content

Commit

Permalink
feat: apply coercions to default options (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssonal authored and bcoe committed Nov 7, 2016
1 parent 02c3545 commit c79052b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -2004,6 +2004,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: {
Expand Down

0 comments on commit c79052b

Please sign in to comment.