Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to allow coercion of array items #132

Merged
merged 3 commits into from
Oct 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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