Skip to content

Commit a85e8dc

Browse files
shadowspawnBenjamin E. Coe
andauthored
fix: always store value for a=b (#43)
Co-authored-by: Benjamin E. Coe <[email protected]>
1 parent ab6d1cc commit a85e8dc

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ const {
88
ArrayPrototypePush,
99
StringPrototypeCharAt,
1010
StringPrototypeIncludes,
11+
StringPrototypeIndexOf,
1112
StringPrototypeSlice,
12-
StringPrototypeSplit,
1313
StringPrototypeStartsWith,
1414
} = require('./primordials');
1515

@@ -110,15 +110,16 @@ const parseArgs = (
110110
arg = StringPrototypeSlice(arg, 2); // remove leading --
111111

112112
if (StringPrototypeIncludes(arg, '=')) {
113-
// withValue equals(=) case
114-
const argParts = StringPrototypeSplit(arg, '=');
115-
116-
// If withValue option is specified, take 2nd part after '=' as value,
117-
// else set value as undefined
118-
const val = options.withValue &&
119-
ArrayPrototypeIncludes(options.withValue, argParts[0]) ?
120-
argParts[1] : undefined;
121-
storeOptionValue(options, argParts[0], val, result);
113+
// Store option=value same way independent of `withValue` as:
114+
// - looks like a value, store as a value
115+
// - match the intention of the user
116+
// - preserve information for author to process further
117+
const index = StringPrototypeIndexOf(arg, '=');
118+
storeOptionValue(
119+
options,
120+
StringPrototypeSlice(arg, 0, index),
121+
StringPrototypeSlice(arg, index + 1),
122+
result);
122123
} else if (pos + 1 < argv.length &&
123124
!StringPrototypeStartsWith(argv[pos + 1], '-')
124125
) {

test/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ test('args equals are passed "withValue"', function(t) {
4747
t.end();
4848
});
4949

50+
test('zero config args equals are parsed as if "withValue"', function(t) {
51+
const passedArgs = ['--so=wat'];
52+
const passedOptions = { };
53+
const expected = { flags: { so: true }, values: { so: 'wat' }, positionals: [] };
54+
const args = parseArgs(passedArgs, passedOptions);
55+
56+
t.deepEqual(args, expected, 'arg value is passed');
57+
58+
t.end();
59+
});
60+
5061
test('same arg is passed twice "withValue" and last value is recorded', function(t) {
5162
const passedArgs = ['--foo=a', '--foo', 'b'];
5263
const passedOptions = { withValue: ['foo'] };
@@ -58,6 +69,17 @@ test('same arg is passed twice "withValue" and last value is recorded', function
5869
t.end();
5970
});
6071

72+
test('args equals pass string including more equals', function(t) {
73+
const passedArgs = ['--so=wat=bing'];
74+
const passedOptions = { withValue: ['so'] };
75+
const expected = { flags: { so: true }, values: { so: 'wat=bing' }, positionals: [] };
76+
const args = parseArgs(passedArgs, passedOptions);
77+
78+
t.deepEqual(args, expected, 'arg value is passed');
79+
80+
t.end();
81+
});
82+
6183
test('first arg passed for "withValue" and "multiples" is in array', function(t) {
6284
const passedArgs = ['--foo=a'];
6385
const passedOptions = { withValue: ['foo'], multiples: ['foo'] };

0 commit comments

Comments
 (0)