From 72496d09f4b540240412e633b46b84f995e629cc Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 23 Jan 2022 13:29:21 +1300 Subject: [PATCH 1/3] fix: only use arrays in results for multiples --- index.js | 29 +++++++++++++++++++---------- test/index.js | 20 ++++++++++---------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index e1e0f7b..cea6a28 100644 --- a/index.js +++ b/index.js @@ -48,18 +48,27 @@ function getMainArgs() { } function storeOptionValue(parseOptions, option, value, result) { + const multiple = parseOptions.multiples && + StringPrototypeIncludes(parseOptions.multiples, option); + + // Flags result.flags[option] = true; - // Append value to previous values array for case of multiples - // option, else add to empty array - result.values[option] = ArrayPrototypeConcat( - [], - parseOptions.multiples && - ArrayPrototypeIncludes(parseOptions.multiples, option) && - result.values[option] || - [], - value - ); + // Values + if (multiple) { + // Always store value in array, including for flags. + // result.values[option] starts out not present, + // first value is added as new array [newValue], + // subsequent values are pushed to existing array. + const usedAsFlag = value === undefined; + const newValue = usedAsFlag ? true : value; + if (result.values[option] !== undefined) + ArrayPrototypePush(result.values[option], newValue); + else + result.values[option] = [newValue]; + } else { + result.values[option] = value; + } } const parseArgs = ( diff --git a/test/index.js b/test/index.js index c75802a..a257cc4 100644 --- a/test/index.js +++ b/test/index.js @@ -18,7 +18,7 @@ test('Everything after a bare `--` is considered a positional argument', functio test('args are true', function(t) { const passedArgs = ['--foo', '--bar']; - const expected = { flags: { foo: true, bar: true }, values: { foo: [undefined], bar: [undefined] }, positionals: [] }; + const expected = { flags: { foo: true, bar: true }, values: { foo: undefined, bar: undefined }, positionals: [] }; const args = parseArgs(passedArgs); t.deepEqual(args, expected, 'args are true'); @@ -28,7 +28,7 @@ test('args are true', function(t) { test('arg is true and positional is identified', function(t) { const passedArgs = ['--foo=a', '--foo', 'b']; - const expected = { flags: { foo: true }, values: { foo: [undefined] }, positionals: ['b'] }; + const expected = { flags: { foo: true }, values: { foo: undefined }, positionals: ['b'] }; const args = parseArgs(passedArgs); t.deepEqual(args, expected, 'arg is true and positional is identified'); @@ -39,7 +39,7 @@ test('arg is true and positional is identified', function(t) { test('args equals are passed "withValue"', function(t) { const passedArgs = ['--so=wat']; const passedOptions = { withValue: ['so'] }; - const expected = { flags: { so: true }, values: { so: ['wat'] }, positionals: [] }; + const expected = { flags: { so: true }, values: { so: 'wat' }, positionals: [] }; const args = parseArgs(passedArgs, passedOptions); t.deepEqual(args, expected, 'arg value is passed'); @@ -50,7 +50,7 @@ test('args equals are passed "withValue"', function(t) { test('same arg is passed twice "withValue" and last value is recorded', function(t) { const passedArgs = ['--foo=a', '--foo', 'b']; const passedOptions = { withValue: ['foo'] }; - const expected = { flags: { foo: true }, values: { foo: ['b'] }, positionals: [] }; + const expected = { flags: { foo: true }, values: { foo: 'b' }, positionals: [] }; const args = parseArgs(passedArgs, passedOptions); t.deepEqual(args, expected, 'last arg value is passed'); @@ -77,7 +77,7 @@ test('correct default args when use node -p', function(t) { const result = parseArgs(); const expected = { flags: { foo: true }, - values: { foo: [undefined] }, + values: { foo: undefined }, positionals: [] }; t.deepEqual(result, expected); @@ -94,7 +94,7 @@ test('correct default args when use node --print', function(t) { const result = parseArgs(); const expected = { flags: { foo: true }, - values: { foo: [undefined] }, + values: { foo: undefined }, positionals: [] }; t.deepEqual(result, expected); @@ -111,7 +111,7 @@ test('correct default args when use node -e', function(t) { const result = parseArgs(); const expected = { flags: { foo: true }, - values: { foo: [undefined] }, + values: { foo: undefined }, positionals: [] }; t.deepEqual(result, expected); @@ -128,7 +128,7 @@ test('correct default args when use node --eval', function(t) { const result = parseArgs(); const expected = { flags: { foo: true }, - values: { foo: [undefined] }, + values: { foo: undefined }, positionals: [] }; t.deepEqual(result, expected); @@ -145,7 +145,7 @@ test('correct default args when normal arguments', function(t) { const result = parseArgs(); const expected = { flags: { foo: true }, - values: { foo: [undefined] }, + values: { foo: undefined }, positionals: [] }; t.deepEqual(result, expected); @@ -160,7 +160,7 @@ test('excess leading dashes on options are retained', function(t) { const passedOptions = { }; const expected = { flags: { '-triple': true }, - values: { '-triple': [undefined] }, + values: { '-triple': undefined }, positionals: [] }; const result = parseArgs(passedArgs, passedOptions); From bb5d916a46ebc45f3be642402e3fe580ce19d26c Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 23 Jan 2022 13:48:10 +1300 Subject: [PATCH 2/3] Fix which primordial being used, array rather than string --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index cea6a28..ae746eb 100644 --- a/index.js +++ b/index.js @@ -49,7 +49,7 @@ function getMainArgs() { function storeOptionValue(parseOptions, option, value, result) { const multiple = parseOptions.multiples && - StringPrototypeIncludes(parseOptions.multiples, option); + ArrayPrototypeIncludes(parseOptions.multiples, option); // Flags result.flags[option] = true; From 4483d961624237acb04177084fabc0d7bcf7daf6 Mon Sep 17 00:00:00 2001 From: John Gee Date: Sun, 23 Jan 2022 22:57:10 +1300 Subject: [PATCH 3/3] Add test for first value added for multiple --- test/index.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/index.js b/test/index.js index a257cc4..1208a98 100644 --- a/test/index.js +++ b/test/index.js @@ -58,6 +58,17 @@ test('same arg is passed twice "withValue" and last value is recorded', function 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'] }; + const expected = { flags: { foo: true }, values: { foo: ['a'] }, positionals: [] }; + const args = parseArgs(passedArgs, passedOptions); + + t.deepEqual(args, expected, 'first multiple in array'); + + t.end(); +}); + test('args are passed "withValue" and "multiples"', function(t) { const passedArgs = ['--foo=a', '--foo', 'b']; const passedOptions = { withValue: ['foo'], multiples: ['foo'] };