From 494af17d90ae376529f44de17320959fb1e860b7 Mon Sep 17 00:00:00 2001 From: Michael Mathews Date: Thu, 5 Jul 2012 22:10:07 +0100 Subject: [PATCH] Allow function to coerce command line arguments to a preferred type. Closes #143 --- node_modules/common/args.js | 11 +++++++++-- test/specs/common/args.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 test/specs/common/args.js diff --git a/node_modules/common/args.js b/node_modules/common/args.js index efb56ae4..affdc79e 100644 --- a/node_modules/common/args.js +++ b/node_modules/common/args.js @@ -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 { diff --git a/test/specs/common/args.js b/test/specs/common/args.js new file mode 100644 index 00000000..2dd564c4 --- /dev/null +++ b/test/specs/common/args.js @@ -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'); + }); +}); \ No newline at end of file