Skip to content

Commit

Permalink
code: don't swallow next String option when empty
Browse files Browse the repository at this point in the history
Fixes: #65
Credit: @samjonester
Reviewed-By: @othiym23
PR-URL: #67
  • Loading branch information
samjonester authored and othiym23 committed Dec 13, 2016
1 parent 0d95e90 commit 651d447
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,14 @@ function parse (args, data, remain, types, shorthands) {
continue
}

if (argType === String && la === undefined)
la = ""
if (argType === String) {
if (la === undefined) {
la = ""
} else if (la.match(/^-{1,2}[^-]+/)) {
la = ""
i --
}
}

if (la && la.match(/^-{2,}$/)) {
la = undefined
Expand Down
8 changes: 8 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ test("Empty String results in empty string, not true", function (t) {
t.end()
})

// https://github.com/npm/nopt/issues/65
test("Empty String should not swallow next flag", function (t) {
var parsed = nopt({ empty: String, foo: String }, {}, ["--empty", "--foo"], 0)
t.same(parsed.empty, "")
t.same(parsed.foo, "")
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)
Expand Down

0 comments on commit 651d447

Please sign in to comment.