Comparing version 1.1.1 to 1.2.0
@@ -39,3 +39,3 @@ { | ||
}, | ||
"version": "1.1.1" | ||
"version": "1.2.0" | ||
} |
@@ -83,3 +83,3 @@ var optionRegExp = /^--?(.+)$/; | ||
function handleDefaults(state, onError) { | ||
function handleDefaults(parser, state, onError) { | ||
state.options.getOptions().forEach(function(option) { | ||
@@ -95,2 +95,6 @@ var targetProperty = option.targetProperty; | ||
if (typeof defaultValue === 'function') { | ||
defaultValue = defaultValue.call(parser); | ||
} | ||
var handler; | ||
@@ -488,3 +492,3 @@ // If the option has a type and it is not a complex type, we validate | ||
finishLastOption(); | ||
handleDefaults(state, onError); | ||
handleDefaults(this, state, onError); | ||
@@ -491,0 +495,0 @@ // Run the validators |
@@ -228,2 +228,49 @@ 'use strict'; | ||
it('should allow default values as functions', function() { | ||
var parser = require('../') | ||
.createParser({ | ||
'--foo -f': { | ||
type: 'string', | ||
defaultValue: function() { | ||
return 'bar'; | ||
} | ||
}, | ||
'--bar -b': { | ||
type: 'string' | ||
} | ||
}); | ||
var parsed = parser.parse('--bar test'.split(/\s/)); | ||
expect(parsed).to.deep.equal({ | ||
foo: 'bar', | ||
bar: 'test' | ||
}); | ||
}); | ||
it('should call defaultValue function with Parser context', function() { | ||
var usage = 'Usage: test'; | ||
var parser = require('../') | ||
.createParser({ | ||
'--foo -f': { | ||
type: 'string', | ||
defaultValue: function() { | ||
expect(this.getUsageString().indexOf(usage)).to.not.equal(-1); | ||
return 'bar'; | ||
} | ||
}, | ||
'--bar -b': { | ||
type: 'string' | ||
} | ||
}) | ||
.usage(usage); | ||
var parsed = parser.parse('--bar test'.split(/\s/)); | ||
expect(parsed).to.deep.equal({ | ||
foo: 'bar', | ||
bar: 'test' | ||
}); | ||
}); | ||
it('should allow default values use pseudo defaults', function() { | ||
@@ -230,0 +277,0 @@ var parser = require('../') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35911
730