Skip to content

Commit

Permalink
Refactor: Move full boolean check into routine
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Mar 19, 2023
1 parent 5dbc8ae commit f4ecc2a
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ module.exports = function (args, opts) {

var aliases = {};

function aliasIsBoolean(key) {
function isBooleanKey(key) {
if (flags.bools[key]) {
return true;
}
if (!aliases[key]) {
return false;
}
return aliases[key].some(function (x) {
return flags.bools[x];
});
Expand Down Expand Up @@ -134,9 +140,7 @@ module.exports = function (args, opts) {
setArg(key, false);
});
// Overwrite booleans with user supplied defaults.
Object.keys(defaults).filter(function (key) {
return flags.bools[key] || (aliases[key] ? aliasIsBoolean(key) : false);
}).forEach(function (key) {
Object.keys(defaults).filter(isBooleanKey).forEach(function (key) {
setArg(key, defaults[key]);
});

Expand Down Expand Up @@ -172,9 +176,8 @@ module.exports = function (args, opts) {
if (
next !== undefined
&& !(/^(-|--)[^-]/).test(next)
&& !flags.bools[key]
&& !isBooleanKey(key)
&& !flags.allBools
&& (aliases[key] ? !aliasIsBoolean(key) : true)
) {
setArg(key, next, arg);
i += 1;
Expand Down Expand Up @@ -225,8 +228,7 @@ module.exports = function (args, opts) {
if (
args[i + 1]
&& !(/^(-|--)[^-]/).test(args[i + 1])
&& !flags.bools[key]
&& (aliases[key] ? !aliasIsBoolean(key) : true)
&& !isBooleanKey(key)
) {
setArg(key, args[i + 1], arg);
i += 1;
Expand Down

0 comments on commit f4ecc2a

Please sign in to comment.