From 651d4473946096d341a480bbe56793de3fc706aa Mon Sep 17 00:00:00 2001 From: Sam Jones Date: Wed, 3 Aug 2016 21:45:12 -0400 Subject: [PATCH] code: don't swallow next String option when empty Fixes: #65 Credit: @samjonester Reviewed-By: @othiym23 PR-URL: https://github.com/npm/nopt/pull/67 --- lib/nopt.js | 10 ++++++++-- test/basic.js | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/nopt.js b/lib/nopt.js index dbb2030..1fb1135 100644 --- a/lib/nopt.js +++ b/lib/nopt.js @@ -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 diff --git a/test/basic.js b/test/basic.js index ec2ba26..5c18ac0 100644 --- a/test/basic.js +++ b/test/basic.js @@ -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)