From b377ca949da93656de7c20eb02f8df32c53e3e8e Mon Sep 17 00:00:00 2001 From: bcoe Date: Sat, 8 Feb 2020 23:10:26 -0800 Subject: [PATCH 1/2] feat!: boolean arguments will not be collected into an implicit array BREAKING CHANGE: this reverts parsing behavior of booleans to yargs@14 --- index.js | 6 +++++- test/yargs-parser.js | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 0e2381dc..8005e981 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/yargs-parser.js b/test/yargs-parser.js index ebe0c89c..e3d1a463 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2557,7 +2557,8 @@ 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: { @@ -2565,7 +2566,7 @@ describe('yargs-parser', function () { 'flatten-duplicate-arrays': true } }) - parsed['x'].should.deep.equal([true, true, false]) + parsed['x'].should.deep.equal(false) }) }) }) @@ -2605,7 +2606,7 @@ 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: { @@ -2613,7 +2614,7 @@ describe('yargs-parser', function () { 'flatten-duplicate-arrays': false } }) - parsed['x'].should.deep.equal([true, true, false]) + parsed['x'].should.deep.equal(false) }) }) }) @@ -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', '--infinite', 'false'], { + boolean: 'infinite' + }) + parse.infinite.should.equal(false) + }) }) From a7d2707aae55335c86c3f8ab3f01dc87de2dcff3 Mon Sep 17 00:00:00 2001 From: bcoe Date: Sat, 8 Feb 2020 23:13:47 -0800 Subject: [PATCH 2/2] chore: include negation in test --- test/yargs-parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/yargs-parser.js b/test/yargs-parser.js index e3d1a463..d535a5fc 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -3518,7 +3518,7 @@ describe('yargs-parser', function () { // 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', '--infinite', 'false'], { + var parse = parser(['--infinite', 'true', '--infinite', 'true', '--no-infinite'], { boolean: 'infinite' }) parse.infinite.should.equal(false)