diff --git a/index.js b/index.js index 834f8da7..25943502 100644 --- a/index.js +++ b/index.js @@ -335,6 +335,11 @@ function parse (args, opts) { value = increment } + // Set normalized value when key is in 'normalize' and in 'arrays' + if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) { + value = path.normalize(val) + } + var splitKey = key.split('.') setKey(argv, splitKey, value) @@ -361,20 +366,18 @@ function parse (args, opts) { setKey(argv, x, value) }) - var keys = [key].concat(flags.aliases[key] || []) - for (var i = 0, l = keys.length; i < l; i++) { - if (flags.normalize[keys[i]]) { - keys.forEach(function (key) { - argv.__defineSetter__(key, function (v) { - val = path.normalize(v) - }) - - argv.__defineGetter__(key, function () { - return typeof val === 'string' ? path.normalize(val) : val - }) + // Set normalize getter and setter when key is in 'normalize' but isn't an array + if (checkAllAliases(key, flags.normalize) && !checkAllAliases(key, flags.arrays)) { + var keys = [key].concat(flags.aliases[key] || []) + keys.forEach(function (key) { + argv.__defineSetter__(key, function (v) { + val = path.normalize(v) }) - break - } + + argv.__defineGetter__(key, function () { + return typeof val === 'string' ? path.normalize(val) : val + }) + }) } } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index ca1476c7..00e5b53d 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -236,6 +236,19 @@ describe('yargs-parser', function () { a.s = ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep) a.s.should.equal(['', 'path', 'to', ''].join(path.sep)) }) + + it('should normalize when key is also an array', function () { + var a = parser([ '-s', ['', 'tmp', '..', ''].join(path.sep), ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep) ], { + alias: { + s: ['save'] + }, + normalize: 's', + array: 's' + }) + var expected = [path.sep, ['', 'path', 'to', ''].join(path.sep)] + a.should.have.property('s').and.deep.equal(expected) + a.should.have.property('save').and.deep.equal(expected) + }) }) describe('alias', function () {