Skip to content

disallow specifying multiple:true for boolean arguments #92

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

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 8 additions & 1 deletion errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ class ERR_INVALID_SHORT_OPTION extends TypeError {
}
}

class ERR_MULTIPLE_FLAG extends TypeError {
constructor(longOption) {
super(`options.${longOption}.multiple cannot be used with \`type: 'boolean'\``);
this.code = 'ERR_MULTIPLE_FLAG';
}
}
module.exports = {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_SHORT_OPTION
ERR_INVALID_SHORT_OPTION,
ERR_MULTIPLE_FLAG
}
};
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const {
const {
codes: {
ERR_INVALID_SHORT_OPTION,
ERR_MULTIPLE_FLAG,
},
} = require('./errors');

Expand Down Expand Up @@ -111,7 +112,8 @@ const parseArgs = ({
({ 0: longOption, 1: optionConfig }) => {
validateObject(optionConfig, `options.${longOption}`);

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

Expand All @@ -125,6 +127,11 @@ const parseArgs = ({

if (ObjectHasOwn(optionConfig, 'multiple')) {
validateBoolean(optionConfig.multiple, `options.${longOption}.multiple`);
if (optionConfig.multiple &&
hasType &&
optionConfig.type === 'boolean') {
throw new ERR_MULTIPLE_FLAG(longOption);
}
}
}
);
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,14 @@ test('invalid short option length', function(t) {

t.end();
});

test('`type: boolean` used with `multiple: true`', function(t) {
const passedArgs = [];
const passedOptions = { foo: { type: 'boolean', multiple: true } };

t.throws(function() { parseArgs({ args: passedArgs, options: passedOptions }); }, {
code: 'ERR_MULTIPLE_FLAG'
});

t.end();
});