Skip to content

Commit

Permalink
feat: also add camelCase array options (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex authored and bcoe committed Oct 6, 2018
1 parent 2bc395f commit 08c0117
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
11 changes: 11 additions & 0 deletions index.js
@@ -1,4 +1,5 @@
var camelCase = require('camelcase')
var decamelize = require('decamelize')
var path = require('path')
var tokenizeArgString = require('./lib/tokenize-arg-string')
var util = require('util')
Expand Down Expand Up @@ -674,6 +675,16 @@ function parse (args, opts) {
}
}
})
// For "--optionName", also set argv['option-name']
flags.aliases[key].concat(key).forEach(function (x) {
if (/[A-Z]/.test(x) && configuration['camel-case-expansion']) {
var c = decamelize(x, '-')
if (c !== key && flags.aliases[key].indexOf(c) === -1) {
flags.aliases[key].push(c)
newAliases[c] = true
}
}
})
flags.aliases[key].forEach(function (x) {
flags.aliases[x] = [key].concat(flags.aliases[key].filter(function (y) {
return x !== y
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -34,7 +34,8 @@
"standard-version": "^4.3.0"
},
"dependencies": {
"camelcase": "^4.1.0"
"camelcase": "^4.1.0",
"decamelize": "^2.0.0"
},
"files": [
"lib",
Expand Down
29 changes: 29 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -1529,6 +1529,35 @@ describe('yargs-parser', function () {
var result = parser(['-x', 'val1', '-x', 'val1'])
result.should.have.property('x').that.is.an('array').and.to.deep.equal(['val1', 'val1'])
})

it('should eat camelCase switch with camelCase array option', function () {
var result = parser(['--someOption', '1', '2'], {
array: ['someOption']
})
Array.isArray(result.someOption).should.equal(true)
result.someOption.should.deep.equal([1, 2])
})
it('should eat hyphenated switch with hyphenated array option', function () {
var result = parser(['--some-option', '1', '2'], {
array: ['some-option']
})
Array.isArray(result['some-option']).should.equal(true)
result['some-option'].should.deep.equal([1, 2])
})
it('should eat camelCase switch with hyphenated array option', function () {
var result = parser(['--someOption', '1', '2'], {
array: ['some-option']
})
Array.isArray(result['some-option']).should.equal(true)
result['some-option'].should.deep.equal([1, 2])
})
it('should eat hyphenated switch with camelCase array option', function () {
var result = parser(['--some-option', '1', '2'], {
array: ['someOption']
})
Array.isArray(result['someOption']).should.equal(true)
result['someOption'].should.deep.equal([1, 2])
})
})

describe('nargs', function () {
Expand Down

0 comments on commit 08c0117

Please sign in to comment.