Skip to content

Commit

Permalink
[patch]: test for boolean in setarg should include aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Apr 9, 2023
1 parent 4e1a6e5 commit 23c0152
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ module.exports = function (args, opts) {
o = {};
}
if (o === Array.prototype) { o = []; }
if (o[lastKey] === undefined || flags.bools[lastKey] || typeof o[lastKey] === 'boolean') {
if (o[lastKey] === undefined || isBooleanKey(lastKey) || typeof o[lastKey] === 'boolean') {
o[lastKey] = value;
} else if (Array.isArray(o[lastKey])) {
o[lastKey].push(value);
Expand Down
50 changes: 50 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,53 @@ test('auto bool accumulates with auto string', function (t) {

t.end();
});

test('declared boolean overwrites string', function (t) {
var options = {
boolean: ['b'],
};

// Verify the setup, that can get a string into the option. (Can't do this for long options.)
var argv1 = parse(['-b=xyz'], options);
t.deepEqual(argv1, {
b: 'xyz',
_: [],
});

// Check that declared boolean overwrites string, and does not accumulate into array.
var argv2 = parse(['-b=xyz', '-b'], options);

t.deepEqual(argv2, {
b: true,
_: [],
});

t.end();
});

test('declared boolean alias overwrites string', function (t) {
// https://github.com/minimistjs/minimist/issues/31
var options = {
boolean: ['b'],
alias: { b: 'B' },
};

// Verify the setup, that can get a string into the option. (Can't do this for long options.)
var argv1 = parse(['-B=xyz'], options);
t.deepEqual(argv1, {
b: 'xyz',
B: 'xyz',
_: [],
});

// Check that declared boolean overwrites string, and does not accumulate into array.
var argv2 = parse(['-B=xyz', '-B'], options);

t.deepEqual(argv2, {
b: true,
B: true,
_: [],
});

t.end();
});

0 comments on commit 23c0152

Please sign in to comment.