diff --git a/lib/nopt.js b/lib/nopt.js index b9d068f..dbb2030 100644 --- a/lib/nopt.js +++ b/lib/nopt.js @@ -283,9 +283,15 @@ function parse (args, data, remain, types, shorthands) { if (abbrevs[arg]) arg = abbrevs[arg] - var isTypeArray = Array.isArray(types[arg]) - , isArray = types[arg] === Array || - isTypeArray && types[arg].indexOf(Array) !== -1 + var argType = types[arg] + var isTypeArray = Array.isArray(argType) + if (isTypeArray && argType.length === 1) { + isTypeArray = false + argType = argType[0] + } + + var isArray = argType === Array || + isTypeArray && argType.indexOf(Array) !== -1 // allow unknown things to be arrays if specified multiple times. if (!types.hasOwnProperty(arg) && data.hasOwnProperty(arg)) { @@ -298,12 +304,12 @@ function parse (args, data, remain, types, shorthands) { , la = args[i + 1] var isBool = typeof no === 'boolean' || - types[arg] === Boolean || - isTypeArray && types[arg].indexOf(Boolean) !== -1 || - (typeof types[arg] === 'undefined' && !hadEq) || + argType === Boolean || + isTypeArray && argType.indexOf(Boolean) !== -1 || + (typeof argType === 'undefined' && !hadEq) || (la === "false" && - (types[arg] === null || - isTypeArray && ~types[arg].indexOf(null))) + (argType === null || + isTypeArray && ~argType.indexOf(null))) if (isBool) { // just set and move along @@ -318,21 +324,21 @@ function parse (args, data, remain, types, shorthands) { // also support "foo":[Boolean, "bar"] and "--foo bar" if (isTypeArray && la) { - if (~types[arg].indexOf(la)) { + if (~argType.indexOf(la)) { // an explicit type val = la i ++ - } else if ( la === "null" && ~types[arg].indexOf(null) ) { + } else if ( la === "null" && ~argType.indexOf(null) ) { // null allowed val = null i ++ } else if ( !la.match(/^-{2,}[^-]/) && !isNaN(la) && - ~types[arg].indexOf(Number) ) { + ~argType.indexOf(Number) ) { // number val = +la i ++ - } else if ( !la.match(/^-[^-]/) && ~types[arg].indexOf(String) ) { + } else if ( !la.match(/^-[^-]/) && ~argType.indexOf(String) ) { // string val = la i ++ @@ -345,7 +351,7 @@ function parse (args, data, remain, types, shorthands) { continue } - if (types[arg] === String && la === undefined) + if (argType === String && la === undefined) la = "" if (la && la.match(/^-{2,}$/)) { diff --git a/test/basic.js b/test/basic.js index eb4857b..ec2ba26 100644 --- a/test/basic.js +++ b/test/basic.js @@ -15,6 +15,13 @@ test("Empty String results in empty string, not true", function (t) { t.end() }) +// https://github.com/npm/nopt/issues/66 +test("Empty String should not be true when type is single item Array", function (t) { + var parsed = nopt({ 'foo': [String] }, {}, ["--foo"], 0) + t.same(parsed.foo, "") + t.end() +}) + test("~ path is resolved to " + (isWin ? '%USERPROFILE%' : '$HOME'), function (t) { var path = require("path") , the