Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interprets Single Item Type Arrays as the Single Item #68

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions lib/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,11 @@ function parse (args, data, remain, types, shorthands) {

if (abbrevs[arg]) arg = abbrevs[arg]

var isArray = types[arg] === Array ||
Array.isArray(types[arg]) && types[arg].indexOf(Array) !== -1
var argType = types[arg];
if (Array.isArray(argType) && argType.length === 1) argType = argType[0]

var isArray = argType === Array ||
Array.isArray(argType) && argType.indexOf(Array) !== -1

// allow unknown things to be arrays if specified multiple times.
if (!types.hasOwnProperty(arg) && data.hasOwnProperty(arg)) {
Expand All @@ -289,12 +292,12 @@ function parse (args, data, remain, types, shorthands) {
, la = args[i + 1]

var isBool = typeof no === 'boolean' ||
types[arg] === Boolean ||
Array.isArray(types[arg]) && types[arg].indexOf(Boolean) !== -1 ||
(typeof types[arg] === 'undefined' && !hadEq) ||
argType === Boolean ||
Array.isArray(argType) && argType.indexOf(Boolean) !== -1 ||
(typeof argType === 'undefined' && !hadEq) ||
(la === "false" &&
(types[arg] === null ||
Array.isArray(types[arg]) && ~types[arg].indexOf(null)))
(argType === null ||
Array.isArray(argType) && ~argType.indexOf(null)))

if (isBool) {
// just set and move along
Expand All @@ -308,22 +311,22 @@ function parse (args, data, remain, types, shorthands) {
}

// also support "foo":[Boolean, "bar"] and "--foo bar"
if (Array.isArray(types[arg]) && la) {
if (~types[arg].indexOf(la)) {
if (Array.isArray(argType) && 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 ++
Expand All @@ -336,7 +339,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,}$/)) {
Expand Down
7 changes: 7 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 $HOME", function (t) {
var path = require("path")
if (!process.env.HOME) process.env.HOME = "/tmp"
Expand Down