Skip to content
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();
});