From 0f0fb2d13101ac1eb24479130a9290d5e431befc Mon Sep 17 00:00:00 2001 From: laggingreflex Date: Mon, 28 Nov 2016 00:38:39 +0530 Subject: [PATCH] feat: allow multiple arrays to be provided, rather than always combining (#71) --- index.js | 22 ++++++++++++++---- test/yargs-parser.js | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0d3ed26a..1031bf02 100644 --- a/index.js +++ b/index.js @@ -15,7 +15,9 @@ function parse (args, opts) { 'camel-case-expansion': true, 'dot-notation': true, 'parse-numbers': true, - 'boolean-negation': true + 'boolean-negation': true, + 'duplicate-arguments-array': true, + 'flatten-duplicate-arrays': true }, opts.configuration) var defaults = opts.default || {} var configObjects = opts.configObjects || [] @@ -310,15 +312,25 @@ function parse (args, opts) { // e.g., --foo apple banana cat becomes ["apple", "banana", "cat"] function eatArray (i, key, args) { var start = i + 1 + var argsToSet = [] + var multipleArrayFlag = i > 0 for (var ii = i + 1; ii < args.length; ii++) { if (/^-/.test(args[ii]) && !negative.test(args[ii])) { if (ii === start) { setArg(key, defaultForType('array')) } + multipleArrayFlag = true break } i = ii - setArg(key, args[ii]) + argsToSet.push(args[ii]) + } + if (multipleArrayFlag && !configuration['flatten-duplicate-arrays']) { + setArg(key, argsToSet) + } else { + argsToSet.forEach(function (arg) { + setArg(key, arg) + }) } return i @@ -539,13 +551,15 @@ function parse (args, opts) { if (value === increment) { o[key] = increment(o[key]) } else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) { - o[key] = Array.isArray(value) ? value : [value] + o[key] = Array.isArray(value) && configuration['flatten-duplicate-arrays'] ? value : [value] } else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) { o[key] = value } else if (Array.isArray(o[key])) { o[key].push(value) - } else { + } else if (configuration['duplicate-arguments-array']) { o[key] = [ o[key], value ] + } else { + o[key] = value } } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 8529d859..0e1778a9 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1933,6 +1933,60 @@ describe('yargs-parser', function () { expect(parsed.dice).to.equal(undefined) }) }) + + describe('duplicate arguments array', function () { + it('adds duplicate argument to array', function () { + var parsed = parser('-x a -x b', { + configuration: { + 'duplicate-arguments-array': true + } + }) + + parsed['x'].should.deep.equal(['a', 'b']) + }) + it('keeps only last argument', function () { + var parsed = parser('-x a -x b', { + configuration: { + 'duplicate-arguments-array': false + } + }) + + parsed['x'].should.equal('b') + }) + }) + + describe('flatten duplicate arrays', function () { + it('flattens duplicate array type', function () { + var parsed = parser('-x a b -x c d', { + array: ['x'], + configuration: { + 'flatten-duplicate-arrays': true + } + }) + + parsed['x'].should.deep.equal(['a', 'b', 'c', 'd']) + }) + it('nests duplicate array types', function () { + var parsed = parser('-x a b -x c d', { + array: ['x'], + configuration: { + 'flatten-duplicate-arrays': false + } + }) + + parsed['x'].should.deep.equal([['a', 'b'], ['c', 'd']]) + }) + it('doesn\'t nests single arrays', function () { + var parsed = parser('-x a b', { + array: ['x'], + configuration: { + 'flatten-duplicate-arrays': false + } + }) + + parsed['x'].should.deep.equal(['a', 'b']) + }) + }) }) // addresses: https://github.com/yargs/yargs-parser/issues/41