Skip to content

Commit

Permalink
fix: default option accepts undefined as value
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Sep 24, 2022
1 parent 5d71d14 commit 0a210f3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/internal/util/parse_args/parse_args.js
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
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

0 comments on commit 0a210f3

Please sign in to comment.