Skip to content

Commit c357584

Browse files
authored
fix: only use arrays in results for multiples (#42)
1 parent 3d2834d commit c357584

2 files changed

Lines changed: 40 additions & 20 deletions

File tree

index.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,27 @@ function getMainArgs() {
4848
}
4949

5050
function storeOptionValue(parseOptions, option, value, result) {
51+
const multiple = parseOptions.multiples &&
52+
ArrayPrototypeIncludes(parseOptions.multiples, option);
53+
54+
// Flags
5155
result.flags[option] = true;
5256

53-
// Append value to previous values array for case of multiples
54-
// option, else add to empty array
55-
result.values[option] = ArrayPrototypeConcat(
56-
[],
57-
parseOptions.multiples &&
58-
ArrayPrototypeIncludes(parseOptions.multiples, option) &&
59-
result.values[option] ||
60-
[],
61-
value
62-
);
57+
// Values
58+
if (multiple) {
59+
// Always store value in array, including for flags.
60+
// result.values[option] starts out not present,
61+
// first value is added as new array [newValue],
62+
// subsequent values are pushed to existing array.
63+
const usedAsFlag = value === undefined;
64+
const newValue = usedAsFlag ? true : value;
65+
if (result.values[option] !== undefined)
66+
ArrayPrototypePush(result.values[option], newValue);
67+
else
68+
result.values[option] = [newValue];
69+
} else {
70+
result.values[option] = value;
71+
}
6372
}
6473

6574
const parseArgs = (

test/index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ test('Everything after a bare `--` is considered a positional argument', functio
1818

1919
test('args are true', function(t) {
2020
const passedArgs = ['--foo', '--bar'];
21-
const expected = { flags: { foo: true, bar: true }, values: { foo: [undefined], bar: [undefined] }, positionals: [] };
21+
const expected = { flags: { foo: true, bar: true }, values: { foo: undefined, bar: undefined }, positionals: [] };
2222
const args = parseArgs(passedArgs);
2323

2424
t.deepEqual(args, expected, 'args are true');
@@ -28,7 +28,7 @@ test('args are true', function(t) {
2828

2929
test('arg is true and positional is identified', function(t) {
3030
const passedArgs = ['--foo=a', '--foo', 'b'];
31-
const expected = { flags: { foo: true }, values: { foo: [undefined] }, positionals: ['b'] };
31+
const expected = { flags: { foo: true }, values: { foo: undefined }, positionals: ['b'] };
3232
const args = parseArgs(passedArgs);
3333

3434
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) {
3939
test('args equals are passed "withValue"', function(t) {
4040
const passedArgs = ['--so=wat'];
4141
const passedOptions = { withValue: ['so'] };
42-
const expected = { flags: { so: true }, values: { so: ['wat'] }, positionals: [] };
42+
const expected = { flags: { so: true }, values: { so: 'wat' }, positionals: [] };
4343
const args = parseArgs(passedArgs, passedOptions);
4444

4545
t.deepEqual(args, expected, 'arg value is passed');
@@ -50,14 +50,25 @@ test('args equals are passed "withValue"', function(t) {
5050
test('same arg is passed twice "withValue" and last value is recorded', function(t) {
5151
const passedArgs = ['--foo=a', '--foo', 'b'];
5252
const passedOptions = { withValue: ['foo'] };
53-
const expected = { flags: { foo: true }, values: { foo: ['b'] }, positionals: [] };
53+
const expected = { flags: { foo: true }, values: { foo: 'b' }, positionals: [] };
5454
const args = parseArgs(passedArgs, passedOptions);
5555

5656
t.deepEqual(args, expected, 'last arg value is passed');
5757

5858
t.end();
5959
});
6060

61+
test('first arg passed for "withValue" and "multiples" is in array', function(t) {
62+
const passedArgs = ['--foo=a'];
63+
const passedOptions = { withValue: ['foo'], multiples: ['foo'] };
64+
const expected = { flags: { foo: true }, values: { foo: ['a'] }, positionals: [] };
65+
const args = parseArgs(passedArgs, passedOptions);
66+
67+
t.deepEqual(args, expected, 'first multiple in array');
68+
69+
t.end();
70+
});
71+
6172
test('args are passed "withValue" and "multiples"', function(t) {
6273
const passedArgs = ['--foo=a', '--foo', 'b'];
6374
const passedOptions = { withValue: ['foo'], multiples: ['foo'] };
@@ -77,7 +88,7 @@ test('correct default args when use node -p', function(t) {
7788
const result = parseArgs();
7889

7990
const expected = { flags: { foo: true },
80-
values: { foo: [undefined] },
91+
values: { foo: undefined },
8192
positionals: [] };
8293
t.deepEqual(result, expected);
8394

@@ -94,7 +105,7 @@ test('correct default args when use node --print', function(t) {
94105
const result = parseArgs();
95106

96107
const expected = { flags: { foo: true },
97-
values: { foo: [undefined] },
108+
values: { foo: undefined },
98109
positionals: [] };
99110
t.deepEqual(result, expected);
100111

@@ -111,7 +122,7 @@ test('correct default args when use node -e', function(t) {
111122
const result = parseArgs();
112123

113124
const expected = { flags: { foo: true },
114-
values: { foo: [undefined] },
125+
values: { foo: undefined },
115126
positionals: [] };
116127
t.deepEqual(result, expected);
117128

@@ -128,7 +139,7 @@ test('correct default args when use node --eval', function(t) {
128139
const result = parseArgs();
129140

130141
const expected = { flags: { foo: true },
131-
values: { foo: [undefined] },
142+
values: { foo: undefined },
132143
positionals: [] };
133144
t.deepEqual(result, expected);
134145

@@ -145,7 +156,7 @@ test('correct default args when normal arguments', function(t) {
145156
const result = parseArgs();
146157

147158
const expected = { flags: { foo: true },
148-
values: { foo: [undefined] },
159+
values: { foo: undefined },
149160
positionals: [] };
150161
t.deepEqual(result, expected);
151162

@@ -160,7 +171,7 @@ test('excess leading dashes on options are retained', function(t) {
160171
const passedOptions = { };
161172
const expected = {
162173
flags: { '-triple': true },
163-
values: { '-triple': [undefined] },
174+
values: { '-triple': undefined },
164175
positionals: []
165176
};
166177
const result = parseArgs(passedArgs, passedOptions);

0 commit comments

Comments
 (0)