From 17ca3bdaada25605ab06fe72946d2da4b52087fa Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Mon, 24 Jun 2019 23:43:25 +0200 Subject: [PATCH] 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. --- index.js | 29 +++--------------------- test/yargs-parser.js | 54 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 315d1aab..4fd05531 100644 --- a/index.js +++ b/index.js @@ -46,7 +46,6 @@ function parse (args, opts) { counts: {}, normalize: {}, configs: {}, - defaulted: {}, nargs: {}, coercions: {}, keys: [] @@ -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++) { @@ -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) @@ -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) } } @@ -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]) } } @@ -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 @@ -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) { diff --git a/test/yargs-parser.js b/test/yargs-parser.js index c908187b..abcfaafd 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -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 () { @@ -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') }) }) @@ -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 () { @@ -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 () { @@ -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 () { @@ -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]) + }) + }) }) })