mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
Allow function to coerce command line arguments to a preferred type. Closes #143
This commit is contained in:
parent
4ead9aa4f9
commit
494af17d90
11
node_modules/common/args.js
generated
vendored
11
node_modules/common/args.js
generated
vendored
@ -39,12 +39,14 @@
|
||||
* @param {string} longName The equivalent long name of the option, entered like: --test.
|
||||
* @param {boolean} hasValue Does this option require a value? Like: -t templatename
|
||||
* @param {string} helpText
|
||||
* @param {function} [coercer] A function to coerce the given value to a specific type.
|
||||
* @example
|
||||
* myParser.addOption('t', 'template', true, 'The path to the template.');
|
||||
* myParser.addOption('h', 'help', false, 'Show the help message.');
|
||||
*/
|
||||
exports.ArgParser.prototype.addOption = function(shortName, longName, hasValue, helpText, canHaveMultiple) {
|
||||
this._options.push({shortName: shortName, longName: longName, hasValue: hasValue, helpText: helpText, canHaveMultiple: (canHaveMultiple || false)});
|
||||
exports.ArgParser.prototype.addOption = function(shortName, longName, hasValue, helpText, canHaveMultiple, coercer) {
|
||||
this._options.push({shortName: shortName, longName: longName, hasValue: hasValue, helpText: helpText, canHaveMultiple: (canHaveMultiple || false), coercer: coercer});
|
||||
|
||||
if (shortName) {
|
||||
this._shortNameIndex[shortName] = this._options.length - 1;
|
||||
}
|
||||
@ -139,9 +141,14 @@
|
||||
name = option.longName;
|
||||
}
|
||||
|
||||
if (typeof option.coercer === 'function') {
|
||||
value = option.coercer(value);
|
||||
}
|
||||
|
||||
// Allow for multiple options of the same type to be present
|
||||
if (option.canHaveMultiple && hasOwnProperty.call(result, name)) {
|
||||
var val = result[name];
|
||||
|
||||
if (val instanceof Array) {
|
||||
val.push(value);
|
||||
} else {
|
||||
|
||||
31
test/specs/common/args.js
Normal file
31
test/specs/common/args.js
Normal file
@ -0,0 +1,31 @@
|
||||
describe("common/args", function() {
|
||||
var common = {args: require('common/args')},
|
||||
argParser = new common.args.ArgParser(),
|
||||
ourOptions;
|
||||
|
||||
function trueFalse(v) {
|
||||
var r = false;
|
||||
if (v) {
|
||||
if (v === 'true') { r = true; }
|
||||
else if (v === 'false') { r = false; }
|
||||
else { v = !!r; }
|
||||
}
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
argParser.addOption('s', 'strict', true, 'Throw error on invalid input.', false, trueFalse);
|
||||
argParser.addOption('n', 'name', true, 'The name of the project.', false);
|
||||
|
||||
ourOptions = argParser.parse(['-s', 'true', '-n', 'true']);
|
||||
|
||||
it('should corece a true value if a coercer is provided', function() {
|
||||
expect(ourOptions.strict).toBeDefined();
|
||||
expect(ourOptions.strict).toEqual(true);
|
||||
});
|
||||
|
||||
it('should corece a string value if a no coercer is provided', function() {
|
||||
expect(ourOptions.name).toBeDefined();
|
||||
expect(ourOptions.name).toEqual('true');
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user