Skip to content

Commit

Permalink
chore: rename defaultValue option to default
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Aug 20, 2022
1 parent eae0933 commit 662664d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ changes:
times. If `true`, all values will be collected in an array. If
`false`, values for the option are last-wins. **Default:** `false`.
* `short` {string} A single character alias for the option.
* `defaultValue` {string | boolean | string[] | boolean[]} The default option value when it is not set by args.
* `default` {string | boolean | string[] | boolean[]} The default option value when it is not set by args.
* `strict` {boolean} Should an error be thrown when unknown arguments
are encountered, or when arguments are passed that do not match the
`type` configured in `options`.
Expand Down
2 changes: 1 addition & 1 deletion examples/is-default-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const { parseArgs } = require('..'); // in repo

const options = {
file: { short: 'f', type: 'string', defaultValue: 'FOO' },
file: { short: 'f', type: 'string', default: 'FOO' },
};

const { values, tokens } = parseArgs({ options, tokens: true });
Expand Down
34 changes: 15 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const {
ArrayPrototypeForEach,
ArrayPrototypeIncludes,
ArrayPrototypeMap,
ArrayPrototypeFilter,
ArrayPrototypePush,
ArrayPrototypePushApply,
ArrayPrototypeShift,
Expand Down Expand Up @@ -332,16 +331,16 @@ const parseArgs = (config = kEmptyObject) => {
validateBoolean(multipleOption, `options.${longOption}.multiple`);
}

if (ObjectHasOwn(optionConfig, 'defaultValue')) {
const defaultValue = objectGetOwn(optionConfig, 'defaultValue');
if (ObjectHasOwn(optionConfig, 'default')) {
const defaultValue = objectGetOwn(optionConfig, 'default');
if (optionType === 'string' && !multipleOption) {
validateString(defaultValue, `options.${longOption}.defaultValue`);
validateString(defaultValue, `options.${longOption}.default`);
} else if (optionType === 'string' && multipleOption) {
validateStringArray(defaultValue, `options.${longOption}.defaultValue`);
validateStringArray(defaultValue, `options.${longOption}.default`);
} else if (optionType === 'boolean' && !multipleOption) {
validateBoolean(defaultValue, `options.${longOption}.defaultValue`);
validateBoolean(defaultValue, `options.${longOption}.default`);
} else if (optionType === 'boolean' && multipleOption) {
validateBooleanArray(defaultValue, `options.${longOption}.defaultValue`);
validateBooleanArray(defaultValue, `options.${longOption}.default`);
}
}
}
Expand Down Expand Up @@ -374,21 +373,18 @@ const parseArgs = (config = kEmptyObject) => {
});

// Phase 3: fill in default values for missing args
const defaultValueOptions = ArrayPrototypeFilter(
ObjectEntries(options), ({ 0: longOption,
1: optionConfig }) => {
return useDefaultValueOption(longOption, optionConfig, result.values);
});

if (defaultValueOptions.length > 0) {
ArrayPrototypeForEach(defaultValueOptions, ({ 0: longOption,
1: optionConfig }) => {
ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption,
1: optionConfig }) => {
const mustSetDefault = useDefaultValueOption(longOption,
optionConfig,
result.values);
if (mustSetDefault) {
storeDefaultOption(longOption,
optionConfig.defaultValue,
objectGetOwn(optionConfig, 'default'),
result.values);
});
}
});

}

return result;
};
Expand Down
88 changes: 44 additions & 44 deletions test/default-values.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,75 @@
const test = require('tape');
const { parseArgs } = require('../index.js');

test('defaultValue must be a boolean when option type is boolean', (t) => {
test('default must be a boolean when option type is boolean', (t) => {
const args = [];
const options = { alpha: { type: 'boolean', defaultValue: 'not a boolean' } };
const options = { alpha: { type: 'boolean', default: 'not a boolean' } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue must be Boolean/
}, /alpha\.default must be Boolean/
);
t.end();
});

test('defaultValue must be a boolean array when option type is boolean and multiple', (t) => {
test('default must be a boolean array when option type is boolean and multiple', (t) => {
const args = [];
const options = { alpha: { type: 'boolean', multiple: true, defaultValue: 'not an array' } };
const options = { alpha: { type: 'boolean', multiple: true, default: 'not an array' } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue must be Array/
}, /alpha\.default must be Array/
);
t.end();
});

test('defaultValue must be a boolean array when option type is string and multiple is true', (t) => {
test('default must be a boolean array when option type is string and multiple is true', (t) => {
const args = [];
const options = { alpha: { type: 'boolean', multiple: true, defaultValue: [true, true, 42] } };
const options = { alpha: { type: 'boolean', multiple: true, default: [true, true, 42] } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue\[2\] must be Boolean/
}, /alpha\.default\[2\] must be Boolean/
);
t.end();
});

test('defaultValue must be a string when option type is string', (t) => {
test('default must be a string when option type is string', (t) => {
const args = [];
const options = { alpha: { type: 'string', defaultValue: true } };
const options = { alpha: { type: 'string', default: true } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue must be String/
}, /alpha\.default must be String/
);
t.end();
});

test('defaultValue must be an array when option type is string and multiple is true', (t) => {
test('default must be an array when option type is string and multiple is true', (t) => {
const args = [];
const options = { alpha: { type: 'string', multiple: true, defaultValue: 'not an array' } };
const options = { alpha: { type: 'string', multiple: true, default: 'not an array' } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue must be Array/
}, /alpha\.default must be Array/
);
t.end();
});

test('defaultValue must be a string array when option type is string and multiple is true', (t) => {
test('default must be a string array when option type is string and multiple is true', (t) => {
const args = [];
const options = { alpha: { type: 'string', multiple: true, defaultValue: ['str', 42] } };
const options = { alpha: { type: 'string', multiple: true, default: ['str', 42] } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue\[1\] must be String/
}, /alpha\.default\[1\] must be String/
);
t.end();
});

test('defaultValue accepted input when multiple is true', (t) => {
test('default accepted input when multiple is true', (t) => {
const args = ['--inputStringArr', 'c', '--inputStringArr', 'd', '--inputBoolArr', '--inputBoolArr'];
const options = {
inputStringArr: { type: 'string', multiple: true, defaultValue: ['a', 'b'] },
emptyStringArr: { type: 'string', multiple: true, defaultValue: [] },
fullStringArr: { type: 'string', multiple: true, defaultValue: ['a', 'b'] },
inputBoolArr: { type: 'boolean', multiple: true, defaultValue: [false, true, false] },
emptyBoolArr: { type: 'boolean', multiple: true, defaultValue: [] },
fullBoolArr: { type: 'boolean', multiple: true, defaultValue: [false, true, false] },
inputStringArr: { type: 'string', multiple: true, default: ['a', 'b'] },
emptyStringArr: { type: 'string', multiple: true, default: [] },
fullStringArr: { type: 'string', multiple: true, default: ['a', 'b'] },
inputBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] },
emptyBoolArr: { type: 'boolean', multiple: true, default: [] },
fullBoolArr: { type: 'boolean', multiple: true, default: [false, true, false] },
};
const expected = { values: { __proto__: null,
inputStringArr: ['c', 'd'],
Expand All @@ -88,12 +88,12 @@ test('defaultValue accepted input when multiple is true', (t) => {
t.end();
});

test('when defaultValue is set, the option must be added as result', (t) => {
test('when default is set, the option must be added as result', (t) => {
const args = [];
const options = {
a: { type: 'string', defaultValue: 'HELLO' },
b: { type: 'boolean', defaultValue: false },
c: { type: 'boolean', defaultValue: true }
a: { type: 'string', default: 'HELLO' },
b: { type: 'boolean', default: false },
c: { type: 'boolean', default: true }
};
const expected = { values: { __proto__: null, a: 'HELLO', b: false, c: true }, positionals: [] };

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

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

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

test('tokens should not include the defaultValue options', (t) => {
test('tokens should not include the default options', (t) => {
const args = [];
const options = {
a: { type: 'string', defaultValue: 'HELLO' },
b: { type: 'boolean', defaultValue: false },
c: { type: 'boolean', defaultValue: true }
a: { type: 'string', default: 'HELLO' },
b: { type: 'boolean', default: false },
c: { type: 'boolean', default: true }
};

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

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

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

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

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

test('multiple as false should expect a String and not an array', (t) => {
const args = [];
const options = { alpha: { type: 'string', multiple: false, defaultValue: 42 } };
const options = { alpha: { type: 'string', multiple: false, default: 42 } };
t.throws(() => {
parseArgs({ args, options });
}, /alpha\.defaultValue must be String/
}, /alpha\.default must be String/
);
t.end();
});
2 changes: 1 addition & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function findLongOptionForShort(shortOption, options) {
* @param {object} values - option values returned in `values` by parseArgs
*/
function useDefaultValueOption(longOption, optionConfig, values) {
return objectGetOwn(optionConfig, 'defaultValue') !== undefined &&
return objectGetOwn(optionConfig, 'default') !== undefined &&
values[longOption] === undefined;
}

Expand Down

0 comments on commit 662664d

Please sign in to comment.