Skip to content

Commit 0a210f3

Browse files
committed
fix: default option accepts undefined as value
1 parent 5d71d14 commit 0a210f3

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

lib/internal/util/parse_args/parse_args.js

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

334-
if (ObjectHasOwn(optionConfig, 'default')) {
335-
const defaultValue = objectGetOwn(optionConfig, 'default');
334+
const defaultValue = objectGetOwn(optionConfig, 'default');
335+
if (defaultValue !== undefined) {
336336
if (optionType === 'string' && !multipleOption) {
337337
validateString(defaultValue, `options.${longOption}.default`);
338338
} else if (optionType === 'string' && multipleOption) {

test/parallel/test-parse-args.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,19 @@ test('default must be a boolean when option type is boolean', () => {
833833
);
834834
});
835835

836+
test('default must accept undefined value', () => {
837+
const args = [];
838+
const options = { alpha: { type: 'boolean', default: undefined } };
839+
const result = parseArgs({ args, options });
840+
const expected = {
841+
values: {
842+
__proto__: null,
843+
},
844+
positionals: []
845+
};
846+
assert.deepStrictEqual(result, expected);
847+
});
848+
836849
test('default must be a boolean array when option type is boolean and multiple', () => {
837850
const args = [];
838851
const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } };

0 commit comments

Comments
 (0)