Skip to content

Commit

Permalink
[patch] include aliases when setting defaults for booleans
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
shadowspawn authored and ljharb committed Apr 8, 2023
1 parent 84316a9 commit 2758c33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,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
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();
});

0 comments on commit 2758c33

Please sign in to comment.