Skip to content
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

[patch]: include aliases when looking for defaults for booleans #32

Merged
merged 2 commits into from
Apr 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 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 @@ -129,10 +135,14 @@ module.exports = function (args, opts) {
});
}

// Set booleans to false by default.
Object.keys(flags.bools).forEach(function (key) {
setArg(key, defaults[key] === undefined ? false : defaults[key]);
setArg(key, false);
});
// Set booleans to user defined default if supplied.
Object.keys(defaults).filter(isBooleanKey).forEach(function (key) {
setArg(key, defaults[key]);
});

var notFlags = [];

if (args.indexOf('--') !== -1) {
Expand Down Expand Up @@ -165,9 +175,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 @@ -218,8 +227,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
16 changes: 16 additions & 0 deletions test/bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,19 @@ test('boolean using something similar to true', function (t) {
t.same(result, expected);
t.end();
});

test('supplied default for boolean using alias', function (t) {
var argv = parse(['moo'], {
boolean: ['bool'],
alias: { bool: 'b' },
default: { b: true },
});

t.deepEqual(argv, {
bool: true,
b: true,
_: ['moo'],
});

t.end();
});