diff --git a/index.js b/index.js index 5fdb78e5..2e5ddd32 100644 --- a/index.js +++ b/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') @@ -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 diff --git a/package.json b/package.json index 74b2efee..933ca217 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,8 @@ "standard-version": "^4.3.0" }, "dependencies": { - "camelcase": "^4.1.0" + "camelcase": "^4.1.0", + "decamelize": "^2.0.0" }, "files": [ "lib", diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 24a8f121..62a4965f 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -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 () {