Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: boolean arguments will not be collected into an implicit array #236

Merged
merged 2 commits into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -3514,4 +3515,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)
})
})