Skip to content

Commit

Permalink
feat: array.type can now be provided, supporting coercion (yargs#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
iilei authored and bcoe committed Oct 6, 2018
1 parent 6dc42a1 commit 4b8cfce
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion index.js
Expand Up @@ -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)
})
Expand Down
57 changes: 57 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -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 () {
Expand Down

0 comments on commit 4b8cfce

Please sign in to comment.