Skip to content

Commit 4b8cfce

Browse files
iileibcoe
authored andcommitted
feat: array.type can now be provided, supporting coercion (#132)
1 parent 6dc42a1 commit 4b8cfce

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ Parses command line arguments returning a simple mapping of keys and values.
6060
* `opts`: provide a set of hints indicating how `args` should be parsed:
6161
* `opts.alias`: an object representing the set of aliases for a key: `{alias: {foo: ['f']}}`.
6262
* `opts.array`: indicate that keys should be parsed as an array: `{array: ['foo', 'bar']}`.
63+
Indicate that keys should be parsed as an array and coerced to booleans / numbers:
64+
`{array: [{ key: 'foo', boolean: true }, {key: 'bar', number: true}]}`.
6365
* `opts.boolean`: arguments should be parsed as booleans: `{boolean: ['x', 'y']}`.
6466
* `opts.config`: indicate a key that represents a path to a configuration file (this file will be loaded and parsed).
6567
* `opts.coerce`: provide a custom synchronous function that returns a coerced value from the argument provided

index.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,23 @@ function parse (args, opts) {
5252
var negative = /^-[0-9]+(\.[0-9]+)?/
5353
var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')
5454

55-
;[].concat(opts.array).filter(Boolean).forEach(function (key) {
55+
;[].concat(opts.array).filter(Boolean).forEach(function (opt) {
56+
var key = opt.key || opt
57+
58+
// assign to flags[bools|strings|numbers]
59+
const assignment = Object.keys(opt).map(function (key) {
60+
return ({
61+
boolean: 'bools',
62+
string: 'strings',
63+
number: 'numbers'
64+
})[key]
65+
}).filter(Boolean).pop()
66+
67+
// assign key to be coerced
68+
if (assignment) {
69+
flags[assignment][key] = true
70+
}
71+
5672
flags.arrays[key] = true
5773
flags.keys.push(key)
5874
})

test/yargs-parser.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,63 @@ describe('yargs-parser', function () {
15671567
Array.isArray(result['someOption']).should.equal(true)
15681568
result['someOption'].should.deep.equal([1, 2])
15691569
})
1570+
1571+
// see https://github.com/yargs/yargs-parser/issues/6
1572+
it('should respect the type `boolean` option for arrays', function () {
1573+
var result = parser(['-x=true', 'false'], {
1574+
array: [{ key: 'x', boolean: true }]
1575+
})
1576+
result.should.have.property('x').that.is.an('array').and.to.deep.equal([true, false])
1577+
})
1578+
1579+
it('should respect the type `number` option for arrays', function () {
1580+
var result = parser(['-x=5', '2'], {
1581+
array: [{ key: 'x', number: true }]
1582+
})
1583+
result.should.have.property('x').that.is.an('array').and.to.deep.equal([5, 2])
1584+
})
1585+
1586+
it('should respect the type `string` option for arrays', function () {
1587+
var result = parser(['-x=5', '2'], {
1588+
configuration: {
1589+
'parse-numbers': true
1590+
},
1591+
array: [{ key: 'x', string: true }]
1592+
})
1593+
result.should.have.property('x').that.is.an('array').and.to.deep.equal(['5', '2'])
1594+
})
1595+
1596+
it('should eat non-hyphenated arguments until hyphenated option is hit - combined with coercion', function () {
1597+
var result = parser([
1598+
'-a=hello', 'world',
1599+
'-b', '33', '22',
1600+
'--foo', 'true', 'false',
1601+
'--bar=cat', 'dog'
1602+
], {
1603+
array: [
1604+
'a',
1605+
{ key: 'b', integer: true },
1606+
{ key: 'foo', boolean: true },
1607+
'bar'
1608+
]
1609+
})
1610+
1611+
Array.isArray(result.a).should.equal(true)
1612+
result.a.should.include('hello')
1613+
result.a.should.include('world')
1614+
1615+
Array.isArray(result.b).should.equal(true)
1616+
result.b.should.include(33)
1617+
result.b.should.include(22)
1618+
1619+
Array.isArray(result.foo).should.equal(true)
1620+
result.foo.should.include(true)
1621+
result.foo.should.include(false)
1622+
1623+
Array.isArray(result.bar).should.equal(true)
1624+
result.bar.should.include('cat')
1625+
result.bar.should.include('dog')
1626+
})
15701627
})
15711628

15721629
describe('nargs', function () {

0 commit comments

Comments
 (0)