Skip to content

Commit

Permalink
feat: Guard against short options longer than 1 char
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronccasanova committed Feb 28, 2022
1 parent 14866a3 commit b0b71c4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ const parseArgs = ({
}

if (ObjectHasOwn(optionConfig, 'short')) {
validateString(optionConfig.short, `options.${option}.short`);
const short = optionConfig.short;
validateString(short, `options.${option}.short`);
if (short.length !== 1) {
throw new Error(`options.${option}.short must be a single character got "${short}"`);
}
}

if (ObjectHasOwn(optionConfig, 'multiple')) {
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,12 @@ test('invalid union value passed to "type" option', function(t) {

t.end();
});

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

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

t.end();
});

0 comments on commit b0b71c4

Please sign in to comment.