Skip to content

Commit

Permalink
fix: always store value for a=b (#43)
Browse files Browse the repository at this point in the history
Co-authored-by: Benjamin E. Coe <bencoe@google.com>
  • Loading branch information
shadowspawn and bcoe committed Feb 4, 2022
1 parent ab6d1cc commit a85e8dc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
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

0 comments on commit a85e8dc

Please sign in to comment.