diff --git a/index.js b/index.js index ae746eb..55bb436 100644 --- a/index.js +++ b/index.js @@ -8,8 +8,8 @@ const { ArrayPrototypePush, StringPrototypeCharAt, StringPrototypeIncludes, + StringPrototypeIndexOf, StringPrototypeSlice, - StringPrototypeSplit, StringPrototypeStartsWith, } = require('./primordials'); @@ -110,15 +110,16 @@ const parseArgs = ( arg = StringPrototypeSlice(arg, 2); // remove leading -- if (StringPrototypeIncludes(arg, '=')) { - // withValue equals(=) case - const argParts = StringPrototypeSplit(arg, '='); - - // If withValue option is specified, take 2nd part after '=' as value, - // else set value as undefined - const val = options.withValue && - ArrayPrototypeIncludes(options.withValue, argParts[0]) ? - argParts[1] : undefined; - storeOptionValue(options, argParts[0], val, result); + // Store option=value same way independent of `withValue` as: + // - looks like a value, store as a value + // - match the intention of the user + // - preserve information for author to process further + const index = StringPrototypeIndexOf(arg, '='); + storeOptionValue( + options, + StringPrototypeSlice(arg, 0, index), + StringPrototypeSlice(arg, index + 1), + result); } else if (pos + 1 < argv.length && !StringPrototypeStartsWith(argv[pos + 1], '-') ) { diff --git a/test/index.js b/test/index.js index 1208a98..b6cef24 100644 --- a/test/index.js +++ b/test/index.js @@ -47,6 +47,17 @@ test('args equals are passed "withValue"', function(t) { t.end(); }); +test('zero config args equals are parsed as if "withValue"', function(t) { + const passedArgs = ['--so=wat']; + const passedOptions = { }; + const expected = { flags: { so: true }, values: { so: 'wat' }, positionals: [] }; + const args = parseArgs(passedArgs, passedOptions); + + t.deepEqual(args, expected, 'arg value is passed'); + + t.end(); +}); + test('same arg is passed twice "withValue" and last value is recorded', function(t) { const passedArgs = ['--foo=a', '--foo', 'b']; const passedOptions = { withValue: ['foo'] }; @@ -58,6 +69,17 @@ test('same arg is passed twice "withValue" and last value is recorded', function t.end(); }); +test('args equals pass string including more equals', function(t) { + const passedArgs = ['--so=wat=bing']; + const passedOptions = { withValue: ['so'] }; + const expected = { flags: { so: true }, values: { so: 'wat=bing' }, positionals: [] }; + const args = parseArgs(passedArgs, passedOptions); + + t.deepEqual(args, expected, 'arg value is passed'); + + t.end(); +}); + test('first arg passed for "withValue" and "multiples" is in array', function(t) { const passedArgs = ['--foo=a']; const passedOptions = { withValue: ['foo'], multiples: ['foo'] };