From 4b8cfce511edce5fe6f3c5a6d7eb147b56cd8f52 Mon Sep 17 00:00:00 2001 From: iilei Date: Sat, 6 Oct 2018 23:15:13 +0200 Subject: [PATCH] feat: array.type can now be provided, supporting coercion (#132) --- README.md | 2 ++ index.js | 18 +++++++++++++- test/yargs-parser.js | 57 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 138f73c8..1cccd3ad 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ Parses command line arguments returning a simple mapping of keys and values. * `opts`: provide a set of hints indicating how `args` should be parsed: * `opts.alias`: an object representing the set of aliases for a key: `{alias: {foo: ['f']}}`. * `opts.array`: indicate that keys should be parsed as an array: `{array: ['foo', 'bar']}`. + Indicate that keys should be parsed as an array and coerced to booleans / numbers: + `{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}`. * `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`. * `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed). * `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided diff --git a/index.js b/index.js index e0054fd0..d003297a 100644 --- a/index.js +++ b/index.js @@ -52,7 +52,23 @@ function parse (args, opts) { var negative = /^-[0-9]+(\.[0-9]+)?/ var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)') - ;[].concat(opts.array).filter(Boolean).forEach(function (key) { + ;[].concat(opts.array).filter(Boolean).forEach(function (opt) { + var key = opt.key || opt + + // assign to flags[bools|strings|numbers] + const assignment = Object.keys(opt).map(function (key) { + return ({ + boolean: 'bools', + string: 'strings', + number: 'numbers' + })[key] + }).filter(Boolean).pop() + + // assign key to be coerced + if (assignment) { + flags[assignment][key] = true + } + flags.arrays[key] = true flags.keys.push(key) }) diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 7f220a17..7b52a0be 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1567,6 +1567,63 @@ describe('yargs-parser', function () { Array.isArray(result['someOption']).should.equal(true) result['someOption'].should.deep.equal([1, 2]) }) + + // see https://github.com/yargs/yargs-parser/issues/6 + it('should respect the type `boolean` option for arrays', function () { + var result = parser(['-x=true', 'false'], { + array: [{ key: 'x', boolean: true }] + }) + result.should.have.property('x').that.is.an('array').and.to.deep.equal([true, false]) + }) + + it('should respect the type `number` option for arrays', function () { + var result = parser(['-x=5', '2'], { + array: [{ key: 'x', number: true }] + }) + result.should.have.property('x').that.is.an('array').and.to.deep.equal([5, 2]) + }) + + it('should respect the type `string` option for arrays', function () { + var result = parser(['-x=5', '2'], { + configuration: { + 'parse-numbers': true + }, + array: [{ key: 'x', string: true }] + }) + result.should.have.property('x').that.is.an('array').and.to.deep.equal(['5', '2']) + }) + + it('should eat non-hyphenated arguments until hyphenated option is hit - combined with coercion', function () { + var result = parser([ + '-a=hello', 'world', + '-b', '33', '22', + '--foo', 'true', 'false', + '--bar=cat', 'dog' + ], { + array: [ + 'a', + { key: 'b', integer: true }, + { key: 'foo', boolean: true }, + 'bar' + ] + }) + + Array.isArray(result.a).should.equal(true) + result.a.should.include('hello') + result.a.should.include('world') + + Array.isArray(result.b).should.equal(true) + result.b.should.include(33) + result.b.should.include(22) + + Array.isArray(result.foo).should.equal(true) + result.foo.should.include(true) + result.foo.should.include(false) + + Array.isArray(result.bar).should.equal(true) + result.bar.should.include('cat') + result.bar.should.include('dog') + }) }) describe('nargs', function () {