Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only use arrays in results for multiples #42

Merged
merged 3 commits into from
Jan 23, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
20 changes: 10 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,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');
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);
Expand Down