Skip to content

Commit

Permalink
fix!: boolean now behaves the same as other array types (#184)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: we have dropped the broken "defaulted" functionality; we would like to revisit adding this in the future.
  • Loading branch information
juergba authored and bcoe committed Jun 24, 2019
1 parent 2f26436 commit 17ca3bd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
29 changes: 3 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ function parse (args, opts) {
counts: {},
normalize: {},
configs: {},
defaulted: {},
nargs: {},
coercions: {},
keys: []
Expand Down Expand Up @@ -132,14 +131,6 @@ function parse (args, opts) {
})

var argv = { _: [] }

Object.keys(flags.bools).forEach(function (key) {
if (Object.prototype.hasOwnProperty.call(defaults, key)) {
setArg(key, defaults[key])
setDefaulted(key)
}
})

var notFlags = []

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

function setArg (key, val) {
unsetDefaulted(key)

if (/-/.test(key) && configuration['camel-case-expansion']) {
var alias = key.split('.').map(function (prop) {
return camelCase(prop)
Expand Down Expand Up @@ -560,7 +549,7 @@ function parse (args, opts) {
} else {
// setting arguments via CLI takes precedence over
// values within the config file.
if (!hasKey(argv, fullKey.split('.')) || (flags.defaulted[fullKey]) || (flags.arrays[fullKey] && configuration['combine-arrays'])) {
if (!hasKey(argv, fullKey.split('.')) || (flags.arrays[fullKey] && configuration['combine-arrays'])) {
setArg(fullKey, value)
}
}
Expand Down Expand Up @@ -589,7 +578,7 @@ function parse (args, opts) {
return camelCase(key)
})

if (((configOnly && flags.configs[keys.join('.')]) || !configOnly) && (!hasKey(argv, keys) || flags.defaulted[keys.join('.')])) {
if (((configOnly && flags.configs[keys.join('.')]) || !configOnly) && !hasKey(argv, keys)) {
setArg(keys.join('.'), process.env[envVar])
}
}
Expand Down Expand Up @@ -704,7 +693,7 @@ function parse (args, opts) {
}
} else if (o[key] === undefined && isTypeArray) {
o[key] = isValueArray ? value : [value]
} else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts))) {
} else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.counts))) {
o[key] = [ o[key], value ]
} else {
o[key] = value
Expand Down Expand Up @@ -762,18 +751,6 @@ function parse (args, opts) {
return isSet
}

function setDefaulted (key) {
[].concat(flags.aliases[key] || [], key).forEach(function (k) {
flags.defaulted[k] = true
})
}

function unsetDefaulted (key) {
[].concat(flags.aliases[key] || [], key).forEach(function (k) {
delete flags.defaulted[k]
})
}

// make a best effor to pick a default value
// for an option based on name and type.
function defaultValue (key) {
Expand Down
54 changes: 51 additions & 3 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ describe('yargs-parser', function () {
})
})

describe('with implied false default', function () {
describe('without any default value', function () {
var opts = null

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

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

Expand Down Expand Up @@ -2334,6 +2334,18 @@ describe('yargs-parser', function () {
parsed['x'].should.deep.equal(3)
})
})
describe('type=boolean', function () {
it('[-x true -x true -x false] => false', function () {
var parsed = parser('-x true -x true -x false', {
boolean: ['x'],
configuration: {
'duplicate-arguments-array': false,
'flatten-duplicate-arrays': false
}
})
parsed['x'].should.deep.equal(false)
})
})
})
describe('duplicate=false, flatten=true,', function () {
describe('type=array', function () {
Expand Down Expand Up @@ -2370,6 +2382,18 @@ describe('yargs-parser', function () {
parsed['x'].should.deep.equal(3)
})
})
describe('type=boolean', function () {
it('[-x true -x true -x false] => false', function () {
var parsed = parser('-x true -x true -x false', {
boolean: ['x'],
configuration: {
'duplicate-arguments-array': false,
'flatten-duplicate-arrays': true
}
})
parsed['x'].should.deep.equal(false)
})
})
})
describe('duplicate=true, flatten=true,', function () {
describe('type=array', function () {
Expand Down Expand Up @@ -2406,6 +2430,18 @@ describe('yargs-parser', function () {
parsed['x'].should.deep.equal([1, 2, 3])
})
})
describe('type=boolean', function () {
it('[-x true -x true -x false] => [true, true, false]', function () {
var parsed = parser('-x true -x true -x false', {
boolean: ['x'],
configuration: {
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': true
}
})
parsed['x'].should.deep.equal([true, true, false])
})
})
})
describe('duplicate=true, flatten=false,', function () {
describe('type=array', function () {
Expand Down Expand Up @@ -2442,6 +2478,18 @@ describe('yargs-parser', function () {
parsed['x'].should.deep.equal([1, 2, 3])
})
})
describe('type=boolean', function () {
it('[-x true -x true -x false] => [true, true, false]', function () {
var parsed = parser('-x true -x true -x false', {
boolean: ['x'],
configuration: {
'duplicate-arguments-array': true,
'flatten-duplicate-arrays': false
}
})
parsed['x'].should.deep.equal([true, true, false])
})
})
})
})

Expand Down

0 comments on commit 17ca3bd

Please sign in to comment.