Repository: yargs/yargs-parser
Pull request #21 (2016-04-04 21:36:18)

feat(normalize): allow normalize to work with arrays
Options with 'normalize' and 'array' no longer fail when passing more than 1 path.

The previous behavior of getter and setter is only applied when the option doesn't use the 'array' flag.
In the future, we can use ES2015's proxies to also apply the same effect of the getter and setter to all the elements in the array.

Note: For this to work, you need to set your option to 'array', it doesn't work with implicit arrays.

Closes yargs/yargs#430

coveralls: 
[![Coverage Status](https://coveralls.io/builds/5659818/badge)](https://coveralls.io/builds/5659818)

Coverage remained the same at 100.0% when pulling **cd1d7b90a9aeacb4098ec31968147532cd770215 on elas7:fix-normalize** into **48b1e6a338f80417b3fd0fc3e39ae181332fdcb5 on yargs:master**.


Patch:
diff --git a/index.js b/index.js
index 834f8da..2594350 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
+        })
+      })
     }
   }
 


Test patch:
diff --git a/test/yargs-parser.js b/test/yargs-parser.js
index ca1476c..00e5b53 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 () {