Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: default option accepts undefined as value
  • Loading branch information
Eomm committed Sep 29, 2022
commit 21fd4fd3f259ac6ebb77d12c9178652e3fc82bf4
4 changes: 2 additions & 2 deletions lib/internal/util/parse_args/parse_args.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ const parseArgs = (config = kEmptyObject) => {
validateBoolean(multipleOption, `options.${longOption}.multiple`);
}

if (ObjectHasOwn(optionConfig, 'default')) {
const defaultValue = objectGetOwn(optionConfig, 'default');
const defaultValue = objectGetOwn(optionConfig, 'default');
if (defaultValue !== undefined) {
if (optionType === 'string' && !multipleOption) {
validateString(defaultValue, `options.${longOption}.default`);
} else if (optionType === 'string' && multipleOption) {
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-parse-args.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,19 @@ test('default must be a boolean when option type is boolean', () => {
);
});

test('default must accept undefined value', () => {
const args = [];
const options = { alpha: { type: 'boolean', default: undefined } };
const result = parseArgs({ args, options });
const expected = {
values: {
__proto__: null,
},
positionals: []
};
assert.deepStrictEqual(result, expected);
});

test('default must be a boolean array when option type is boolean and multiple', () => {
const args = [];
const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } };
Expand Down