Skip to content

Commit 662664d

Browse files
committed
chore: rename defaultValue option to default
1 parent eae0933 commit 662664d

File tree

5 files changed

+62
-66
lines changed

5 files changed

+62
-66
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ changes:
3030
times. If `true`, all values will be collected in an array. If
3131
`false`, values for the option are last-wins. **Default:** `false`.
3232
* `short` {string} A single character alias for the option.
33-
* `defaultValue` {string | boolean | string[] | boolean[]} The default option value when it is not set by args.
33+
* `default` {string | boolean | string[] | boolean[]} The default option value when it is not set by args.
3434
* `strict` {boolean} Should an error be thrown when unknown arguments
3535
are encountered, or when arguments are passed that do not match the
3636
`type` configured in `options`.

examples/is-default-value.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
const { parseArgs } = require('..'); // in repo
88

99
const options = {
10-
file: { short: 'f', type: 'string', defaultValue: 'FOO' },
10+
file: { short: 'f', type: 'string', default: 'FOO' },
1111
};
1212

1313
const { values, tokens } = parseArgs({ options, tokens: true });

index.js

+15-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
ArrayPrototypeForEach,
55
ArrayPrototypeIncludes,
66
ArrayPrototypeMap,
7-
ArrayPrototypeFilter,
87
ArrayPrototypePush,
98
ArrayPrototypePushApply,
109
ArrayPrototypeShift,
@@ -332,16 +331,16 @@ const parseArgs = (config = kEmptyObject) => {
332331
validateBoolean(multipleOption, `options.${longOption}.multiple`);
333332
}
334333

335-
if (ObjectHasOwn(optionConfig, 'defaultValue')) {
336-
const defaultValue = objectGetOwn(optionConfig, 'defaultValue');
334+
if (ObjectHasOwn(optionConfig, 'default')) {
335+
const defaultValue = objectGetOwn(optionConfig, 'default');
337336
if (optionType === 'string' && !multipleOption) {
338-
validateString(defaultValue, `options.${longOption}.defaultValue`);
337+
validateString(defaultValue, `options.${longOption}.default`);
339338
} else if (optionType === 'string' && multipleOption) {
340-
validateStringArray(defaultValue, `options.${longOption}.defaultValue`);
339+
validateStringArray(defaultValue, `options.${longOption}.default`);
341340
} else if (optionType === 'boolean' && !multipleOption) {
342-
validateBoolean(defaultValue, `options.${longOption}.defaultValue`);
341+
validateBoolean(defaultValue, `options.${longOption}.default`);
343342
} else if (optionType === 'boolean' && multipleOption) {
344-
validateBooleanArray(defaultValue, `options.${longOption}.defaultValue`);
343+
validateBooleanArray(defaultValue, `options.${longOption}.default`);
345344
}
346345
}
347346
}
@@ -374,21 +373,18 @@ const parseArgs = (config = kEmptyObject) => {
374373
});
375374

376375
// Phase 3: fill in default values for missing args
377-
const defaultValueOptions = ArrayPrototypeFilter(
378-
ObjectEntries(options), ({ 0: longOption,
379-
1: optionConfig }) => {
380-
return useDefaultValueOption(longOption, optionConfig, result.values);
381-
});
382-
383-
if (defaultValueOptions.length > 0) {
384-
ArrayPrototypeForEach(defaultValueOptions, ({ 0: longOption,
385-
1: optionConfig }) => {
376+
ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption,
377+
1: optionConfig }) => {
378+
const mustSetDefault = useDefaultValueOption(longOption,
379+
optionConfig,
380+
result.values);
381+
if (mustSetDefault) {
386382
storeDefaultOption(longOption,
387-
optionConfig.defaultValue,
383+
objectGetOwn(optionConfig, 'default'),
388384
result.values);
389-
});
385+
}
386+
});
390387

391-
}
392388

393389
return result;
394390
};

test/default-values.js

+44-44
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,75 @@
55
const test = require('tape');
66
const { parseArgs } = require('../index.js');
77

8-
test('defaultValue must be a boolean when option type is boolean', (t) => {
8+
test('default must be a boolean when option type is boolean', (t) => {
99
const args = [];
10-
const options = { alpha: { type: 'boolean', defaultValue: 'not a boolean' } };
10+
const options = { alpha: { type: 'boolean', default: 'not a boolean' } };
1111
t.throws(() => {
1212
parseArgs({ args, options });
13-
}, /alpha\.defaultValue must be Boolean/
13+
}, /alpha\.default must be Boolean/
1414
);
1515
t.end();
1616
});
1717

18-
test('defaultValue must be a boolean array when option type is boolean and multiple', (t) => {
18+
test('default must be a boolean array when option type is boolean and multiple', (t) => {
1919
const args = [];
20-
const options = { alpha: { type: 'boolean', multiple: true, defaultValue: 'not an array' } };
20+
const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } };
2121
t.throws(() => {
2222
parseArgs({ args, options });
23-
}, /alpha\.defaultValue must be Array/
23+
}, /alpha\.default must be Array/
2424
);
2525
t.end();
2626
});
2727

28-
test('defaultValue must be a boolean array when option type is string and multiple is true', (t) => {
28+
test('default must be a boolean array when option type is string and multiple is true', (t) => {
2929
const args = [];
30-
const options = { alpha: { type: 'boolean', multiple: true, defaultValue: [true, true, 42] } };
30+
const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } };
3131
t.throws(() => {
3232
parseArgs({ args, options });
33-
}, /alpha\.defaultValue\[2\] must be Boolean/
33+
}, /alpha\.default\[2\] must be Boolean/
3434
);
3535
t.end();
3636
});
3737

38-
test('defaultValue must be a string when option type is string', (t) => {
38+
test('default must be a string when option type is string', (t) => {
3939
const args = [];
40-
const options = { alpha: { type: 'string', defaultValue: true } };
40+
const options = { alpha: { type: 'string', default: true } };
4141
t.throws(() => {
4242
parseArgs({ args, options });
43-
}, /alpha\.defaultValue must be String/
43+
}, /alpha\.default must be String/
4444
);
4545
t.end();
4646
});
4747

48-
test('defaultValue must be an array when option type is string and multiple is true', (t) => {
48+
test('default must be an array when option type is string and multiple is true', (t) => {
4949
const args = [];
50-
const options = { alpha: { type: 'string', multiple: true, defaultValue: 'not an array' } };
50+
const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } };
5151
t.throws(() => {
5252
parseArgs({ args, options });
53-
}, /alpha\.defaultValue must be Array/
53+
}, /alpha\.default must be Array/
5454
);
5555
t.end();
5656
});
5757

58-
test('defaultValue must be a string array when option type is string and multiple is true', (t) => {
58+
test('default must be a string array when option type is string and multiple is true', (t) => {
5959
const args = [];
60-
const options = { alpha: { type: 'string', multiple: true, defaultValue: ['str', 42] } };
60+
const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } };
6161
t.throws(() => {
6262
parseArgs({ args, options });
63-
}, /alpha\.defaultValue\[1\] must be String/
63+
}, /alpha\.default\[1\] must be String/
6464
);
6565
t.end();
6666
});
6767

68-
test('defaultValue accepted input when multiple is true', (t) => {
68+
test('default accepted input when multiple is true', (t) => {
6969
const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr'];
7070
const options = {
71-
inputStringArr: { type: 'string', multiple: true, defaultValue: ['a', 'b'] },
72-
emptyStringArr: { type: 'string', multiple: true, defaultValue: [] },
73-
fullStringArr: { type: 'string', multiple: true, defaultValue: ['a', 'b'] },
74-
inputBoolArr: { type: 'boolean', multiple: true, defaultValue: [false, true, false] },
75-
emptyBoolArr: { type: 'boolean', multiple: true, defaultValue: [] },
76-
fullBoolArr: { type: 'boolean', multiple: true, defaultValue: [false, true, false] },
71+
inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] },
72+
emptyStringArr: { type: 'string', multiple: true, default: [] },
73+
fullStringArr: { type: 'string', multiple: true, default: ['a', 'b'] },
74+
inputBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] },
75+
emptyBoolArr: { type: 'boolean', multiple: true, default: [] },
76+
fullBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] },
7777
};
7878
const expected = { values: { __proto__: null,
7979
inputStringArr: ['c', 'd'],
@@ -88,12 +88,12 @@ test('defaultValue accepted input when multiple is true', (t) => {
8888
t.end();
8989
});
9090

91-
test('when defaultValue is set, the option must be added as result', (t) => {
91+
test('when default is set, the option must be added as result', (t) => {
9292
const args = [];
9393
const options = {
94-
a: { type: 'string', defaultValue: 'HELLO' },
95-
b: { type: 'boolean', defaultValue: false },
96-
c: { type: 'boolean', defaultValue: true }
94+
a: { type: 'string', default: 'HELLO' },
95+
b: { type: 'boolean', default: false },
96+
c: { type: 'boolean', default: true }
9797
};
9898
const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] };
9999

@@ -103,12 +103,12 @@ test('when defaultValue is set, the option must be added as result', (t) => {
103103
t.end();
104104
});
105105

106-
test('when defaultValue is set, the args value takes precedence', (t) => {
106+
test('when default is set, the args value takes precedence', (t) => {
107107
const args = ['--a', 'WORLD', '--b', '-c'];
108108
const options = {
109-
a: { type: 'string', defaultValue: 'HELLO' },
110-
b: { type: 'boolean', defaultValue: false },
111-
c: { type: 'boolean', defaultValue: true }
109+
a: { type: 'string', default: 'HELLO' },
110+
b: { type: 'boolean', default: false },
111+
c: { type: 'boolean', default: true }
112112
};
113113
const expected = { values: { __proto__: null, a: 'WORLD', b: true, c: true }, positionals: [] };
114114

@@ -118,12 +118,12 @@ test('when defaultValue is set, the args value takes precedence', (t) => {
118118
t.end();
119119
});
120120

121-
test('tokens should not include the defaultValue options', (t) => {
121+
test('tokens should not include the default options', (t) => {
122122
const args = [];
123123
const options = {
124-
a: { type: 'string', defaultValue: 'HELLO' },
125-
b: { type: 'boolean', defaultValue: false },
126-
c: { type: 'boolean', defaultValue: true }
124+
a: { type: 'string', default: 'HELLO' },
125+
b: { type: 'boolean', default: false },
126+
c: { type: 'boolean', default: true }
127127
};
128128

129129
const expectedTokens = [];
@@ -133,13 +133,13 @@ test('tokens should not include the defaultValue options', (t) => {
133133
t.end();
134134
});
135135

136-
test('tokens:true should not include the defaultValue options after the args input', (t) => {
136+
test('tokens:true should not include the default options after the args input', (t) => {
137137
const args = ['--z', 'zero', 'positional-item'];
138138
const options = {
139139
z: { type: 'string' },
140-
a: { type: 'string', defaultValue: 'HELLO' },
141-
b: { type: 'boolean', defaultValue: false },
142-
c: { type: 'boolean', defaultValue: true }
140+
a: { type: 'string', default: 'HELLO' },
141+
b: { type: 'boolean', default: false },
142+
c: { type: 'boolean', default: true }
143143
};
144144

145145
const expectedTokens = [
@@ -157,7 +157,7 @@ test('proto as default value must be ignored', (t) => {
157157
const options = Object.create(null);
158158

159159
// eslint-disable-next-line no-proto
160-
options.__proto__ = { type: 'string', defaultValue: 'HELLO' };
160+
options.__proto__ = { type: 'string', default: 'HELLO' };
161161

162162
const result = parseArgs({ args, options, allowPositionals: true });
163163
const expected = { values: { __proto__: null }, positionals: [] };
@@ -168,10 +168,10 @@ test('proto as default value must be ignored', (t) => {
168168

169169
test('multiple as false should expect a String and not an array', (t) => {
170170
const args = [];
171-
const options = { alpha: { type: 'string', multiple: false, defaultValue: 42 } };
171+
const options = { alpha: { type: 'string', multiple: false, default: 42 } };
172172
t.throws(() => {
173173
parseArgs({ args, options });
174-
}, /alpha\.defaultValue must be String/
174+
}, /alpha\.default must be String/
175175
);
176176
t.end();
177177
});

utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function findLongOptionForShort(shortOption, options) {
179179
* @param {object} values - option values returned in `values` by parseArgs
180180
*/
181181
function useDefaultValueOption(longOption, optionConfig, values) {
182-
return objectGetOwn(optionConfig, 'defaultValue') !== undefined &&
182+
return objectGetOwn(optionConfig, 'default') !== undefined &&
183183
values[longOption] === undefined;
184184
}
185185

0 commit comments

Comments
 (0)