From c872e66e32e244b1b951e67f5eb6c11daf192803 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Fri, 14 Jun 2019 10:29:56 -0400 Subject: [PATCH] don't coerce number, change to "parse-unknown-options: false", fix node 6 support --- index.js | 39 ++++++++++++++++++++------------------- test/yargs-parser.js | 26 +++++++++++++------------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/index.js b/index.js index 3d65758a..690a2f5e 100644 --- a/index.js +++ b/index.js @@ -27,7 +27,7 @@ function parse (args, opts) { 'halt-at-non-option': false, 'strip-aliased': false, 'strip-dashed': false, - 'ignore-unknown-options': false + 'parse-unknown-options': true }, opts.configuration) var defaults = opts.default || {} var configObjects = opts.configObjects || [] @@ -169,17 +169,17 @@ function parse (args, opts) { } else if (checkAllAliases(m[1], flags.arrays) && args.length > i + 1) { args.splice(i + 1, 0, m[2]) i = eatArray(i, m[1], args) - } else if (!configuration['ignore-unknown-options'] || hasAnyFlag(m[1])) { + } else if (configuration['parse-unknown-options'] || hasAnyFlag(m[1])) { setArg(m[1], m[2]) } else { - argv._.push(maybeCoerceNumber('_', arg)) + argv._.push(arg) } } else if (arg.match(negatedBoolean) && configuration['boolean-negation']) { key = arg.match(negatedBoolean)[1] - if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) { + if (configuration['parse-unknown-options'] || hasAnyFlag(key)) { setArg(key, false) } else { - argv._.push(maybeCoerceNumber('_', arg)) + argv._.push(arg) } // -- seperated by space. @@ -194,7 +194,7 @@ function parse (args, opts) { // array format = '--foo a b c' } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) { i = eatArray(i, key, args) - } else if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) { + } else if (configuration['parse-unknown-options'] || hasAnyFlag(key)) { next = flags.nargs[key] === 0 ? undefined : args[i + 1] if (next !== undefined && (!next.match(/^-/) || @@ -210,16 +210,16 @@ function parse (args, opts) { setArg(key, defaultValue(key)) } } else { - argv._.push(maybeCoerceNumber('_', arg)) + argv._.push(arg) } // dot-notation flag seperated by '='. } else if (arg.match(/^-.\..+=/)) { m = arg.match(/^-([^=]+)=([\s\S]*)$/) - if (!configuration['ignore-unknown-options'] || hasAnyFlag(m[1])) { + if (configuration['parse-unknown-options'] || hasAnyFlag(m[1])) { setArg(m[1], m[2]) } else { - argv._.push(maybeCoerceNumber('_', arg)) + argv._.push(arg) } // dot-notation flag seperated by space. @@ -227,7 +227,7 @@ function parse (args, opts) { next = args[i + 1] key = arg.match(/^-(.\..+)/)[1] - if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) { + if (configuration['parse-unknown-options'] || hasAnyFlag(key)) { if (next !== undefined && !next.match(/^-/) && !checkAllAliases(key, flags.bools) && !checkAllAliases(key, flags.counts)) { @@ -237,7 +237,7 @@ function parse (args, opts) { setArg(key, defaultValue(key)) } } else { - argv._.push(maybeCoerceNumber('_', arg)) + argv._.push(arg) } } else if (arg.match(/^-[^-]+/) && !arg.match(negative)) { letters = arg.slice(1, -1).split('') @@ -259,7 +259,7 @@ function parse (args, opts) { } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) { args.splice(i + 1, 0, value) i = eatArray(i, key, args) - } else if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) { + } else if (configuration['parse-unknown-options'] || hasAnyFlag(key)) { setArg(key, value) } else { unmatched.push(key) @@ -272,7 +272,7 @@ function parse (args, opts) { } if (next === '-') { - if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) { + if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) { setArg(letters[j], next) } else { unmatched.push(letters[j]) @@ -284,7 +284,7 @@ function parse (args, opts) { // current letter is an alphabetic character and next value is a number if (/[A-Za-z]/.test(letters[j]) && /^-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) { - if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) { + if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) { setArg(letters[j], next) } else { unmatched.push(letters[j]) @@ -295,7 +295,7 @@ function parse (args, opts) { } if (letters[j + 1] && letters[j + 1].match(/\W/)) { - if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) { + if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) { setArg(letters[j], next) } else { unmatched.push(letters[j]) @@ -303,7 +303,7 @@ function parse (args, opts) { } broken = true break - } else if (!configuration['ignore-unknown-options'] || hasAnyFlag(letters[j])) { + } else if (configuration['parse-unknown-options'] || hasAnyFlag(letters[j])) { setArg(letters[j], defaultValue(letters[j])) } else { unmatched.push(letters[j]) @@ -319,7 +319,7 @@ function parse (args, opts) { // array format = '-f a b c' } else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) { i = eatArray(i, key, args) - } else if (!configuration['ignore-unknown-options'] || hasAnyFlag(key)) { + } else if (configuration['parse-unknown-options'] || hasAnyFlag(key)) { next = args[i + 1] if (next !== undefined && (!/^(-|--)[^-]/.test(next) || @@ -339,7 +339,7 @@ function parse (args, opts) { } } if (unmatched.length > 0) { - argv._.push(maybeCoerceNumber('_', ['-', ...unmatched].join(''))) + argv._.push(['-', ...unmatched].join('')) } } else if (arg === '--') { notFlags = args.slice(i + 1) @@ -808,7 +808,8 @@ function parse (args, opts) { function hasAnyFlag (key) { var isSet = false - var toCheck = [].concat(Object.values(flags)) + // XXX Switch to [].concat(...Object.values(flags)) once node.js 6 is dropped + var toCheck = [].concat(...Object.keys(flags).map(k => flags[k])) toCheck.forEach(function (flag) { if (flag[key]) isSet = flag[key] diff --git a/test/yargs-parser.js b/test/yargs-parser.js index c4f890b8..67ad8274 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2963,12 +2963,12 @@ describe('yargs-parser', function () { }) }) }) - describe('ignore-unknown-options', function () { + describe('parse-unknown-options = false', function () { it('should ignore unknown options in long format separated by =', function () { const argv = parser('--known-arg=1 --unknown-arg=2', { number: ['known-arg'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -2981,7 +2981,7 @@ describe('yargs-parser', function () { const argv = parser('--no-known-arg --no-unknown-arg', { boolean: ['known-arg'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -2994,7 +2994,7 @@ describe('yargs-parser', function () { const argv = parser('--known-arg 1 --unknown-arg 2', { number: ['known-arg'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3007,7 +3007,7 @@ describe('yargs-parser', function () { const argv = parser('-k.arg=1 -u.arg=2', { number: ['k.arg'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3021,7 +3021,7 @@ describe('yargs-parser', function () { const argv = parser('-k.arg 1 -u.arg 2', { number: ['k.arg'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3035,7 +3035,7 @@ describe('yargs-parser', function () { const argv = parser('-k=1 -u=2', { number: ['k'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3047,7 +3047,7 @@ describe('yargs-parser', function () { const argv = parser('-k- -u-', { string: ['k'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3059,7 +3059,7 @@ describe('yargs-parser', function () { const argv = parser('-k 1 -u 2', { number: ['k'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3071,7 +3071,7 @@ describe('yargs-parser', function () { const argv = parser('-k1 -u2', { number: ['k'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3083,7 +3083,7 @@ describe('yargs-parser', function () { const argv = parser('-k/1/ -u/2/', { string: ['k'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3096,7 +3096,7 @@ describe('yargs-parser', function () { const argv = parser('-kuv', { boolean: ['k', 'v'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({ @@ -3108,7 +3108,7 @@ describe('yargs-parser', function () { const argv = parser('-kvu', { boolean: ['k', 'v'], configuration: { - 'ignore-unknown-options': true + 'parse-unknown-options': false } }) argv.should.deep.equal({