Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat!: boolean arguments will not be collected into an implicit array (
…#236)

BREAKING CHANGE: this reverts parsing behavior of booleans to that of yargs@14
  • Loading branch information
bcoe committed Feb 10, 2020
1 parent 11ea8d1 commit 34c4e19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
6 changes: 5 additions & 1 deletion index.js
Expand Up @@ -713,7 +713,11 @@ function parse (args, opts) {
}
} else if (o[key] === undefined && isTypeArray) {
o[key] = isValueArray ? value : [value]
} else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.counts))) {
} else if (duplicate && !(
o[key] === undefined ||
checkAllAliases(key, flags.counts) ||
checkAllAliases(key, flags.bools)
)) {
o[key] = [ o[key], value ]
} else {
o[key] = value
Expand Down
17 changes: 13 additions & 4 deletions test/yargs-parser.js
Expand Up @@ -2557,15 +2557,16 @@ describe('yargs-parser', function () {
})
})
describe('type=boolean', function () {
it('[-x true -x true -x false] => [true, true, false]', function () {
// in the casse of boolean arguments, only the last argument is used:
it('[-x true -x true -x false] => 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])
parsed['x'].should.deep.equal(false)
})
})
})
Expand Down Expand Up @@ -2605,15 +2606,15 @@ describe('yargs-parser', function () {
})
})
describe('type=boolean', function () {
it('[-x true -x true -x false] => [true, true, false]', 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': true,
'flatten-duplicate-arrays': false
}
})
parsed['x'].should.deep.equal([true, true, false])
parsed['x'].should.deep.equal(false)
})
})
})
Expand Down Expand Up @@ -3530,4 +3531,12 @@ describe('yargs-parser', function () {
parse.toString.should.equal(66)
})
})

// See: https://github.com/facebook/jest/issues/9517
it('does not collect arguments configured as booleans into implicit array', () => {
var parse = parser(['--infinite', 'true', '--infinite', 'true', '--no-infinite'], {
boolean: 'infinite'
})
parse.infinite.should.equal(false)
})
})

0 comments on commit 34c4e19

Please sign in to comment.