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: always store value for a=b #43

Merged
merged 5 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const {
ArrayPrototypePush,
StringPrototypeCharAt,
StringPrototypeIncludes,
StringPrototypeIndexOf,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
} = require('./primordials');

Expand Down Expand Up @@ -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], '-')
) {
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] };
Expand All @@ -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'] };
Expand Down