Skip to content

Commit

Permalink
Add implemention for opts.number
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Jun 27, 2023
1 parent d4de1ab commit 3419b15
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
30 changes: 24 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ module.exports = function (args, opts) {

var flags = {
bools: {},
known: {},
// known: {},
numbers: {},
strings: {},
unknownFn: null,
};
Expand Down Expand Up @@ -65,6 +66,7 @@ module.exports = function (args, opts) {
});
});

// populating flags.strings with explicit keys and aliases
[].concat(opts.string).filter(Boolean).forEach(function (key) {
flags.strings[key] = true;
if (aliases[key]) {
Expand All @@ -74,19 +76,30 @@ module.exports = function (args, opts) {
}
});

[].concat(opts.known).filter(Boolean).forEach(function (key) {
flags.known[key] = true;
// populating flags.numbers with explicit keys and aliases
[].concat(opts.number).filter(Boolean).forEach(function (key) {
flags.numbers[key] = true;
if (aliases[key]) {
[].concat(aliases[key]).forEach(function (k) {
flags.numbers[k] = true;
});
}
});

// [].concat(opts.known).filter(Boolean).forEach(function (key) {
// flags.known[key] = true;
// });

var defaults = opts.default || {};

var argv = { _: [] };

function keyDefined(key) {
return flags.strings[key]
|| flags.numbers[key]
|| flags.bools[key]
|| aliases[key]
|| flags.known[key];
|| aliases[key];
// || flags.known[key];
}

function argDefined(key, arg) {
Expand Down Expand Up @@ -140,6 +153,9 @@ module.exports = function (args, opts) {
if (flags.strings[key] && val === true) {
throw new Error('Missing option value for option "' + key + '"');
}
if (flags.numbers[key] && !isNumber(val)) {
throw new Error('Expecting number value for option "' + key + '"');
}
if (isBooleanKey(key) && typeof val === 'string' && !(/^(true|false)$/).test(val)) {
throw new Error('Unexpected option value for option "' + key + '"');
}
Expand All @@ -150,7 +166,9 @@ module.exports = function (args, opts) {

// coercion
var value = val;
if (!flags.strings[key] && isNumber(val)) {
if (flags.numbers[key]) {
value = Number(val);
} else if (!flags.strings[key] && isNumber(val)) {
value = Number(val);
}
if (flags.strings[key] && val === true) {
Expand Down
26 changes: 13 additions & 13 deletions test/strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@ test('strict unknown option: opt.alias is known', function (t) {
t.end();
});

test('strict unknown option: opt.known is known (of course!)', function (t) {
t.doesNotThrow(function () {
// try known as a string and array of strings, with and without option values
parse(['--aaa'], { known: 'aaa', strict: true });
parse(['--aaa=value'], { known: 'aaa', strict: true });
parse(['--aaa', 'value'], { known: 'aaa', strict: true });
parse(['--bbb'], { known: ['aaa', 'bbb'], strict: true });
parse(['-s'], { known: ['s'], strict: true });
parse(['-s=123'], { known: ['s'], strict: true });
parse(['-abc'], { known: ['a', 'b', 'c'], strict: true });
});
t.end();
});
// test('strict unknown option: opt.known is known (of course!)', function (t) {
// t.doesNotThrow(function () {
// // try known as a string and array of strings, with and without option values
// parse(['--aaa'], { known: 'aaa', strict: true });
// parse(['--aaa=value'], { known: 'aaa', strict: true });
// parse(['--aaa', 'value'], { known: 'aaa', strict: true });
// parse(['--bbb'], { known: ['aaa', 'bbb'], strict: true });
// parse(['-s'], { known: ['s'], strict: true });
// parse(['-s=123'], { known: ['s'], strict: true });
// parse(['-abc'], { known: ['a', 'b', 'c'], strict: true });
// });
// t.end();
// });

test('strict unknown option: opts.unknown returns false', function (t) {
// Mirror non-strict and skip argument processing if opts.unknown returns false.
Expand Down

0 comments on commit 3419b15

Please sign in to comment.