Skip to content

Commit b2f6a44

Browse files
committed
chore: update after merge into node.js core
1 parent 7f68c46 commit b2f6a44

File tree

4 files changed

+65
-64
lines changed

4 files changed

+65
-64
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ 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-
* `default` {string | boolean | string[] | boolean[]} The default option value when it is not set by args.
33+
* `default` {string | boolean | string\[] | boolean\[]} The default option
34+
value when it is not set by args. It must be of the same type as the
35+
the `type` property. When `multiple` is `true`, it must be an array.
3436
* `strict` {boolean} Should an error be thrown when unknown arguments
3537
are encountered, or when arguments are passed that do not match the
3638
`type` configured in `options`.

index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,19 @@ const parseArgs = (config = kEmptyObject) => {
331331
validateBoolean(multipleOption, `options.${longOption}.multiple`);
332332
}
333333

334-
if (ObjectHasOwn(optionConfig, 'default')) {
335-
const defaultValue = objectGetOwn(optionConfig, 'default');
336-
if (optionType === 'string' && !multipleOption) {
337-
validateString(defaultValue, `options.${longOption}.default`);
338-
} else if (optionType === 'string' && multipleOption) {
339-
validateStringArray(defaultValue, `options.${longOption}.default`);
340-
} else if (optionType === 'boolean' && !multipleOption) {
341-
validateBoolean(defaultValue, `options.${longOption}.default`);
342-
} else if (optionType === 'boolean' && multipleOption) {
343-
validateBooleanArray(defaultValue, `options.${longOption}.default`);
334+
const defaultValue = objectGetOwn(optionConfig, 'default');
335+
if (defaultValue !== undefined) {
336+
let validator;
337+
switch (optionType) {
338+
case 'string':
339+
validator = multipleOption ? validateStringArray : validateString;
340+
break;
341+
342+
case 'boolean':
343+
validator = multipleOption ? validateBooleanArray : validateBoolean;
344+
break;
344345
}
346+
validator(defaultValue, `options.${longOption}.default`);
345347
}
346348
}
347349
);

test/default-values.js

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,78 @@
1-
'use strict';
2-
1+
/* global assert */
32
/* eslint max-len: 0 */
3+
'use strict';
44

5-
const test = require('tape');
5+
const { test } = require('./utils');
66
const { parseArgs } = require('../index.js');
77

8-
test('default must be a boolean when option type is boolean', (t) => {
8+
test('default must be a boolean when option type is boolean', () => {
99
const args = [];
1010
const options = { alpha: { type: 'boolean', default: 'not a boolean' } };
11-
t.throws(() => {
11+
assert.throws(() => {
1212
parseArgs({ args, options });
13-
}, /alpha\.default must be Boolean/
13+
}, /options\.alpha\.default must be Boolean/
1414
);
15-
t.end();
1615
});
1716

18-
test('default must be a boolean array when option type is boolean and multiple', (t) => {
17+
test('default must accept undefined value', () => {
18+
const args = [];
19+
const options = { alpha: { type: 'boolean', default: undefined } };
20+
const result = parseArgs({ args, options });
21+
const expected = {
22+
values: {
23+
__proto__: null,
24+
},
25+
positionals: []
26+
};
27+
assert.deepStrictEqual(result, expected);
28+
});
29+
30+
test('default must be a boolean array when option type is boolean and multiple', () => {
1931
const args = [];
2032
const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } };
21-
t.throws(() => {
33+
assert.throws(() => {
2234
parseArgs({ args, options });
23-
}, /alpha\.default must be Array/
35+
}, /options\.alpha\.default must be Array/
2436
);
25-
t.end();
2637
});
2738

28-
test('default must be a boolean array when option type is string and multiple is true', (t) => {
39+
test('default must be a boolean array when option type is string and multiple is true', () => {
2940
const args = [];
3041
const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } };
31-
t.throws(() => {
42+
assert.throws(() => {
3243
parseArgs({ args, options });
33-
}, /alpha\.default\[2\] must be Boolean/
44+
}, /options\.alpha\.default\[2\] must be Boolean/
3445
);
35-
t.end();
3646
});
3747

38-
test('default must be a string when option type is string', (t) => {
48+
test('default must be a string when option type is string', () => {
3949
const args = [];
4050
const options = { alpha: { type: 'string', default: true } };
41-
t.throws(() => {
51+
assert.throws(() => {
4252
parseArgs({ args, options });
43-
}, /alpha\.default must be String/
53+
}, /options\.alpha\.default must be String/
4454
);
45-
t.end();
4655
});
4756

48-
test('default must be an array when option type is string and multiple is true', (t) => {
57+
test('default must be an array when option type is string and multiple is true', () => {
4958
const args = [];
5059
const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } };
51-
t.throws(() => {
60+
assert.throws(() => {
5261
parseArgs({ args, options });
53-
}, /alpha\.default must be Array/
62+
}, /options\.alpha\.default must be Array/
5463
);
55-
t.end();
5664
});
5765

58-
test('default must be a string array when option type is string and multiple is true', (t) => {
66+
test('default must be a string array when option type is string and multiple is true', () => {
5967
const args = [];
6068
const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } };
61-
t.throws(() => {
69+
assert.throws(() => {
6270
parseArgs({ args, options });
63-
}, /alpha\.default\[1\] must be String/
71+
}, /options\.alpha\.default\[1\] must be String/
6472
);
65-
t.end();
6673
});
6774

68-
test('default accepted input when multiple is true', (t) => {
75+
test('default accepted input when multiple is true', () => {
6976
const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr'];
7077
const options = {
7178
inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] },
@@ -84,11 +91,10 @@ test('default accepted input when multiple is true', (t) => {
8491
fullBoolArr: [false, true, false] },
8592
positionals: [] };
8693
const result = parseArgs({ args, options });
87-
t.deepEqual(result, expected);
88-
t.end();
94+
assert.deepStrictEqual(result, expected);
8995
});
9096

91-
test('when default is set, the option must be added as result', (t) => {
97+
test('when default is set, the option must be added as result', () => {
9298
const args = [];
9399
const options = {
94100
a: { type: 'string', default: 'HELLO' },
@@ -98,12 +104,10 @@ test('when default is set, the option must be added as result', (t) => {
98104
const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] };
99105

100106
const result = parseArgs({ args, options });
101-
102-
t.deepEqual(result, expected);
103-
t.end();
107+
assert.deepStrictEqual(result, expected);
104108
});
105109

106-
test('when default is set, the args value takes precedence', (t) => {
110+
test('when default is set, the args value takes precedence', () => {
107111
const args = ['--a', 'WORLD', '--b', '-c'];
108112
const options = {
109113
a: { type: 'string', default: 'HELLO' },
@@ -113,12 +117,10 @@ test('when default is set, the args value takes precedence', (t) => {
113117
const expected = { values: { __proto__: null, a: 'WORLD', b: true, c: true }, positionals: [] };
114118

115119
const result = parseArgs({ args, options });
116-
117-
t.deepEqual(result, expected);
118-
t.end();
120+
assert.deepStrictEqual(result, expected);
119121
});
120122

121-
test('tokens should not include the default options', (t) => {
123+
test('tokens should not include the default options', () => {
122124
const args = [];
123125
const options = {
124126
a: { type: 'string', default: 'HELLO' },
@@ -129,11 +131,10 @@ test('tokens should not include the default options', (t) => {
129131
const expectedTokens = [];
130132

131133
const { tokens } = parseArgs({ args, options, tokens: true });
132-
t.deepEqual(tokens, expectedTokens);
133-
t.end();
134+
assert.deepStrictEqual(tokens, expectedTokens);
134135
});
135136

136-
test('tokens:true should not include the default options after the args input', (t) => {
137+
test('tokens:true should not include the default options after the args input', () => {
137138
const args = ['--z', 'zero', 'positional-item'];
138139
const options = {
139140
z: { type: 'string' },
@@ -148,11 +149,10 @@ test('tokens:true should not include the default options after the args input',
148149
];
149150

150151
const { tokens } = parseArgs({ args, options, tokens: true, allowPositionals: true });
151-
t.deepEqual(tokens, expectedTokens);
152-
t.end();
152+
assert.deepStrictEqual(tokens, expectedTokens);
153153
});
154154

155-
test('proto as default value must be ignored', (t) => {
155+
test('proto as default value must be ignored', () => {
156156
const args = [];
157157
const options = Object.create(null);
158158

@@ -161,17 +161,14 @@ test('proto as default value must be ignored', (t) => {
161161

162162
const result = parseArgs({ args, options, allowPositionals: true });
163163
const expected = { values: { __proto__: null }, positionals: [] };
164-
t.deepEqual(result, expected);
165-
t.end();
164+
assert.deepStrictEqual(result, expected);
166165
});
167166

168167

169-
test('multiple as false should expect a String', (t) => {
168+
test('multiple as false should expect a String', () => {
170169
const args = [];
171170
const options = { alpha: { type: 'string', multiple: false, default: ['array'] } };
172-
t.throws(() => {
171+
assert.throws(() => {
173172
parseArgs({ args, options });
174-
}, /alpha\.default must be String/
175-
);
176-
t.end();
173+
}, / must be String got array/);
177174
});

utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ function findLongOptionForShort(shortOption, options) {
180180
*/
181181
function useDefaultValueOption(longOption, optionConfig, values) {
182182
return objectGetOwn(optionConfig, 'default') !== undefined &&
183-
values[longOption] === undefined;
183+
values[longOption] === undefined;
184184
}
185185

186186
module.exports = {

0 commit comments

Comments
 (0)