From bca1bcd023118f6fd8e33ff8f9abf0eee2c5ad7b Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Mon, 24 Nov 2014 09:00:25 -0800 Subject: [PATCH 1/2] whitespace fix --- lib/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index e0af14f..58f736c 100755 --- a/lib/index.js +++ b/lib/index.js @@ -41,7 +41,7 @@ exports.parse = function (definition, options) { } // -a -ab --aa -c 1 -d x -d y -e 1-4 -f arg1 arg2 arg3 - + var args = options.argv || process.argv.slice(2); var last = null; @@ -130,7 +130,7 @@ exports.parse = function (definition, options) { if (def.require && flags[def.name] === undefined) { return internals.formatError(definition); } - + if (def.alias) { var aliases = [].concat(def.alias); for (var d = 0, dl = aliases.length; d < dl; ++d) { From 1ee4b9bfdaa60d6c61a9db7c337430b1e928f151 Mon Sep 17 00:00:00 2001 From: Daniel Bretoi Date: Mon, 24 Nov 2014 11:59:22 -0800 Subject: [PATCH 2/2] add special help type --- README.md | 7 ++++++- lib/index.js | 30 ++++++++++++++++++++++-------- test/index.js | 19 +++++++++++++++++++ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 2157230..5d44ba2 100755 --- a/README.md +++ b/README.md @@ -72,7 +72,12 @@ h: { } ``` -* `type`: Available types are: `boolean`, `range`, `number`, `string`. Defaults to `string`. +* `type`: Available types are: `boolean`, `range`, `number`, `string`, and `help`. Defaults to `string`. + + `help` is a special type that allows the switch to be executed even though + other paramters are required. Use case is to display a help message and + quit. This will bypass all other errors, so be sure to capture it. It + behaves like a `boolean`. * `description`: Description message that will be returned with usage information. diff --git a/lib/index.js b/lib/index.js index 58f736c..30e9e88 100755 --- a/lib/index.js +++ b/lib/index.js @@ -44,6 +44,8 @@ exports.parse = function (definition, options) { var args = options.argv || process.argv.slice(2); var last = null; + var errors = []; + var help = false; for (i = 0, il = args.length; i < il; ++i) { var arg = args[i]; @@ -53,27 +55,35 @@ exports.parse = function (definition, options) { var char = arg[1]; if (!char) { - return internals.formatError('Invalid empty \'-\' option'); + errors.push(internals.formatError('Invalid empty \'-\' option')); + continue; } if (char === '-' && arg.length <= 2) { - return internals.formatError('Invalid empty \'--\' option'); + errors.push(internals.formatError('Invalid empty \'--\' option')); + continue; } var opts = (char === '-' ? [arg.slice(2)] : arg.slice(1).split('')); for (var p = 0, pl = opts.length; p < pl; ++p) { if (last) { - return internals.formatError('Invalid option:', last.name, 'missing value'); + errors.push(internals.formatError('Invalid option:', last.name, 'missing value')); + continue; } var opt = opts[p]; var def = keys[opt]; if (!def) { - return internals.formatError('Unknown option:', opt); + errors.push(internals.formatError('Unknown option:', opt)); + continue; } - if (def.type === 'boolean') { + if (def.type === 'help') { + flags[def.name] = true; + help = true; + } + else if (def.type === 'boolean') { flags[def.name] = true; } else { @@ -93,7 +103,8 @@ exports.parse = function (definition, options) { value = parseInt(arg, 10); if (!Hoek.isInteger(value)) { - return internals.formatError('Invalid value (non-number) for option:', last.name) + errors.push(internals.formatError('Invalid value (non-number) for option:', last.name)); + continue; } } } @@ -102,7 +113,8 @@ exports.parse = function (definition, options) { last.valid && last.valid.indexOf(value) === -1) { - return internals.formatError('Invalid value for option:', last.name); + errors.push(internals.formatError('Invalid value for option:', last.name)); + continue; } var name = last ? last.name : '_'; @@ -128,7 +140,7 @@ exports.parse = function (definition, options) { } if (def.require && flags[def.name] === undefined) { - return internals.formatError(definition); + errors.push(internals.formatError(definition)); } if (def.alias) { @@ -140,6 +152,8 @@ exports.parse = function (definition, options) { } } + if (errors.length && !help) { return errors[0]; } + return flags; }; diff --git a/test/index.js b/test/index.js index 81daa07..49cbdf8 100755 --- a/test/index.js +++ b/test/index.js @@ -142,6 +142,25 @@ describe('parse()', function () { done(); }); + it('does not return message when required parameter is missing if type help is being executed', function (done) { + + var line = '--try -q -h'; + var definition = { + h: { + type: 'help' + }, + b: { + type: 'number', + require: true + } + }; + + var argv = parse(line, definition); + expect(argv.h).to.equal(true); + + done(); + }); + it('returns error message when required parameter is missing', function (done) { var line = '-a';