Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Require type to be specified for each supplied option #95

Merged
merged 5 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ process.mainArgs = process.argv.slice(process._exec ? 1 : 2)
* `args` {string[]} (Optional) Array of argument strings; defaults
to [`process.mainArgs`](process_argv)
* `options` {Object} (Optional) An object describing the known options to look for in `args`; `options` keys are the long names of the known options, and the values are objects with the following properties:
* `type` {'string'|'boolean'} (Optional) Type of known option; defaults to `'boolean'`;
* `type` {'string'|'boolean'} (Required) Type of known option
* `multiple` {boolean} (Optional) If true, when appearing one or more times in `args`, results are collected in an `Array`
* `short` {string} (Optional) A single character alias for an option; When appearing one or more times in `args`; Respects the `multiple` configuration
* `strict` {Boolean} (Optional) A `Boolean` on wheather or not to throw an error when unknown args are encountered
Expand Down Expand Up @@ -147,6 +147,7 @@ const args = ['-f', 'b'];
const options = {
foo: {
short: 'f',
type: 'boolean'
},
};
const { flags, values, positionals } = parseArgs({ args, options });
Expand Down
4 changes: 1 addition & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ const parseArgs = ({
({ 0: longOption, 1: optionConfig }) => {
validateObject(optionConfig, `options.${longOption}`);

if (ObjectHasOwn(optionConfig, 'type')) {
validateUnion(optionConfig.type, `options.${longOption}.type`, ['string', 'boolean']);
}
validateUnion(optionConfig.type, `options.${longOption}.type`, ['string', 'boolean']);

if (ObjectHasOwn(optionConfig, 'short')) {
const shortOption = optionConfig.short;
Expand Down
16 changes: 13 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ test('when short option `type: "string"` used with value then stored as value',

test('when short option listed in short used as flag then long option stored as flag', (t) => {
const passedArgs = ['-f'];
const passedOptions = { foo: { short: 'f' } };
const passedOptions = { foo: { short: 'f', type: 'boolean' } };
const expected = { flags: { foo: true }, values: { foo: undefined }, positionals: [] };
const args = parseArgs({ args: passedArgs, options: passedOptions });

Expand Down Expand Up @@ -114,7 +114,7 @@ test('handles short-option groups in conjunction with long-options', (t) => {

test('handles short-option groups with "short" alias configured', (t) => {
const passedArgs = ['-rf'];
const passedOptions = { remove: { short: 'r' } };
const passedOptions = { remove: { short: 'r', type: 'boolean' } };
const expected = { flags: { remove: true, f: true }, values: { remove: undefined, f: undefined }, positionals: [] };
const args = parseArgs({ args: passedArgs, options: passedOptions });
t.deepEqual(args, expected);
Expand Down Expand Up @@ -375,6 +375,16 @@ test('invalid argument passed for options', (t) => {
t.end();
});

test('then type property missing for option then throw', function(t) {
const knownOptions = { foo: { } };

t.throws(function() { parseArgs({ options: knownOptions }); }, {
code: 'ERR_INVALID_ARG_TYPE'
});

t.end();
});

test('boolean passed to "type" option', (t) => {
const passedArgs = ['--so=wat'];
const passedOptions = { foo: { type: true } };
Expand All @@ -399,7 +409,7 @@ test('invalid union value passed to "type" option', (t) => {

test('invalid short option length', (t) => {
const passedArgs = [];
const passedOptions = { foo: { short: 'fo' } };
const passedOptions = { foo: { short: 'fo', type: 'boolean' } };

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_INVALID_SHORT_OPTION'
Expand Down
2 changes: 1 addition & 1 deletion test/short-option-combined-with-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ test('when combine string short with value like negative number then parsed as v

test('when combine string short with value which matches configured flag then parsed as value', (t) => {
const passedArgs = ['-af'];
const passedOptions = { alpha: { short: 'a', type: 'string' }, file: { short: 'f' } };
const passedOptions = { alpha: { short: 'a', type: 'string' }, file: { short: 'f', type: 'boolean' } };
const expected = { flags: { alpha: true }, values: { alpha: 'f' }, positionals: [] };

const result = parseArgs({ args: passedArgs, options: passedOptions });
Expand Down
2 changes: 1 addition & 1 deletion test/short-option-groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('when pass zero-config group of booleans then parsed as booleans', (t) => {

test('when pass low-config group of booleans then parsed as booleans', (t) => {
const passedArgs = ['-rf', 'p'];
const passedOptions = { r: {}, f: {} };
const passedOptions = { r: { type: 'boolean' }, f: { type: 'boolean' } };
const expected = { flags: { r: true, f: true }, values: { r: undefined, f: undefined }, positionals: ['p'] };

const result = parseArgs({ args: passedArgs, options: passedOptions });
Expand Down