Skip to content

Commit 17ca3bd

Browse files
juergbabcoe
authored andcommitted
fix!: boolean now behaves the same as other array types (#184)
BREAKING CHANGE: we have dropped the broken "defaulted" functionality; we would like to revisit adding this in the future.
1 parent 2f26436 commit 17ca3bd

File tree

2 files changed

+54
-29
lines changed

2 files changed

+54
-29
lines changed

index.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function parse (args, opts) {
4646
counts: {},
4747
normalize: {},
4848
configs: {},
49-
defaulted: {},
5049
nargs: {},
5150
coercions: {},
5251
keys: []
@@ -132,14 +131,6 @@ function parse (args, opts) {
132131
})
133132

134133
var argv = { _: [] }
135-
136-
Object.keys(flags.bools).forEach(function (key) {
137-
if (Object.prototype.hasOwnProperty.call(defaults, key)) {
138-
setArg(key, defaults[key])
139-
setDefaulted(key)
140-
}
141-
})
142-
143134
var notFlags = []
144135

145136
for (var i = 0; i < args.length; i++) {
@@ -406,8 +397,6 @@ function parse (args, opts) {
406397
}
407398

408399
function setArg (key, val) {
409-
unsetDefaulted(key)
410-
411400
if (/-/.test(key) && configuration['camel-case-expansion']) {
412401
var alias = key.split('.').map(function (prop) {
413402
return camelCase(prop)
@@ -560,7 +549,7 @@ function parse (args, opts) {
560549
} else {
561550
// setting arguments via CLI takes precedence over
562551
// values within the config file.
563-
if (!hasKey(argv, fullKey.split('.')) || (flags.defaulted[fullKey]) || (flags.arrays[fullKey] && configuration['combine-arrays'])) {
552+
if (!hasKey(argv, fullKey.split('.')) || (flags.arrays[fullKey] && configuration['combine-arrays'])) {
564553
setArg(fullKey, value)
565554
}
566555
}
@@ -589,7 +578,7 @@ function parse (args, opts) {
589578
return camelCase(key)
590579
})
591580

592-
if (((configOnly && flags.configs[keys.join('.')]) || !configOnly) && (!hasKey(argv, keys) || flags.defaulted[keys.join('.')])) {
581+
if (((configOnly && flags.configs[keys.join('.')]) || !configOnly) && !hasKey(argv, keys)) {
593582
setArg(keys.join('.'), process.env[envVar])
594583
}
595584
}
@@ -704,7 +693,7 @@ function parse (args, opts) {
704693
}
705694
} else if (o[key] === undefined && isTypeArray) {
706695
o[key] = isValueArray ? value : [value]
707-
} else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts))) {
696+
} else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.counts))) {
708697
o[key] = [ o[key], value ]
709698
} else {
710699
o[key] = value
@@ -762,18 +751,6 @@ function parse (args, opts) {
762751
return isSet
763752
}
764753

765-
function setDefaulted (key) {
766-
[].concat(flags.aliases[key] || [], key).forEach(function (k) {
767-
flags.defaulted[k] = true
768-
})
769-
}
770-
771-
function unsetDefaulted (key) {
772-
[].concat(flags.aliases[key] || [], key).forEach(function (k) {
773-
delete flags.defaulted[k]
774-
})
775-
}
776-
777754
// make a best effor to pick a default value
778755
// for an option based on name and type.
779756
function defaultValue (key) {

test/yargs-parser.js

+51-3
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ describe('yargs-parser', function () {
11081108
})
11091109
})
11101110

1111-
describe('with implied false default', function () {
1111+
describe('without any default value', function () {
11121112
var opts = null
11131113

11141114
beforeEach(function () {
@@ -1125,8 +1125,8 @@ describe('yargs-parser', function () {
11251125
parser(['--no-flag'], opts).flag.should.be.false // eslint-disable-line
11261126
})
11271127

1128-
it('should set false if no flag in arg', function () {
1129-
expect(parser([], opts).flag).to.be.undefined // eslint-disable-line
1128+
it('should not add property if no flag in arg', function () {
1129+
parser([''], opts).should.not.have.property('flag')
11301130
})
11311131
})
11321132

@@ -2334,6 +2334,18 @@ describe('yargs-parser', function () {
23342334
parsed['x'].should.deep.equal(3)
23352335
})
23362336
})
2337+
describe('type=boolean', function () {
2338+
it('[-x true -x true -x false] => false', function () {
2339+
var parsed = parser('-x true -x true -x false', {
2340+
boolean: ['x'],
2341+
configuration: {
2342+
'duplicate-arguments-array': false,
2343+
'flatten-duplicate-arrays': false
2344+
}
2345+
})
2346+
parsed['x'].should.deep.equal(false)
2347+
})
2348+
})
23372349
})
23382350
describe('duplicate=false, flatten=true,', function () {
23392351
describe('type=array', function () {
@@ -2370,6 +2382,18 @@ describe('yargs-parser', function () {
23702382
parsed['x'].should.deep.equal(3)
23712383
})
23722384
})
2385+
describe('type=boolean', function () {
2386+
it('[-x true -x true -x false] => false', function () {
2387+
var parsed = parser('-x true -x true -x false', {
2388+
boolean: ['x'],
2389+
configuration: {
2390+
'duplicate-arguments-array': false,
2391+
'flatten-duplicate-arrays': true
2392+
}
2393+
})
2394+
parsed['x'].should.deep.equal(false)
2395+
})
2396+
})
23732397
})
23742398
describe('duplicate=true, flatten=true,', function () {
23752399
describe('type=array', function () {
@@ -2406,6 +2430,18 @@ describe('yargs-parser', function () {
24062430
parsed['x'].should.deep.equal([1, 2, 3])
24072431
})
24082432
})
2433+
describe('type=boolean', function () {
2434+
it('[-x true -x true -x false] => [true, true, false]', function () {
2435+
var parsed = parser('-x true -x true -x false', {
2436+
boolean: ['x'],
2437+
configuration: {
2438+
'duplicate-arguments-array': true,
2439+
'flatten-duplicate-arrays': true
2440+
}
2441+
})
2442+
parsed['x'].should.deep.equal([true, true, false])
2443+
})
2444+
})
24092445
})
24102446
describe('duplicate=true, flatten=false,', function () {
24112447
describe('type=array', function () {
@@ -2442,6 +2478,18 @@ describe('yargs-parser', function () {
24422478
parsed['x'].should.deep.equal([1, 2, 3])
24432479
})
24442480
})
2481+
describe('type=boolean', function () {
2482+
it('[-x true -x true -x false] => [true, true, false]', function () {
2483+
var parsed = parser('-x true -x true -x false', {
2484+
boolean: ['x'],
2485+
configuration: {
2486+
'duplicate-arguments-array': true,
2487+
'flatten-duplicate-arrays': false
2488+
}
2489+
})
2490+
parsed['x'].should.deep.equal([true, true, false])
2491+
})
2492+
})
24452493
})
24462494
})
24472495

0 commit comments

Comments
 (0)