Skip to content

Commit

Permalink
fix: only use arrays in results for multiples (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jan 23, 2022
1 parent 3d2834d commit c357584
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
29 changes: 19 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,27 @@ function getMainArgs() {
}

function storeOptionValue(parseOptions, option, value, result) {
const multiple = parseOptions.multiples &&
ArrayPrototypeIncludes(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 = (
Expand Down
31 changes: 21 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -50,14 +50,25 @@ 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');

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'] };
Expand All @@ -77,7 +88,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);

Expand All @@ -94,7 +105,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);

Expand All @@ -111,7 +122,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);

Expand All @@ -128,7 +139,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);

Expand All @@ -145,7 +156,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);

Expand All @@ -160,7 +171,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);
Expand Down

0 comments on commit c357584

Please sign in to comment.