Repository: yargs/yargs-parser
Pull request #8 (2016-02-17 18:55:23)

Fix behaviour for arrays when passed with no value and as the first option
This fixes [yargs/#383](https://github.com/bcoe/yargs/issues/383#issuecomment-185270189).

If passed as the first option, without grouping and value, an array option would not get defined as `eatArray()` does simply abort if it doesn't see any value passed. This fix makes `eatArray()` assign the array option the default value if no value is provided.

There might be a more elegant solution, though...


Patch:
diff --git a/README.md b/README.md
index 4b76a99..44d2e00 100644
--- a/README.md
+++ b/README.md
@@ -59,7 +59,6 @@ Parses command line arguments returning a simple mapping of keys and values.
   * `opts.normalize`: `path.normalize()` will be applied to values set to this key.
   * `opts.string`: keys should be treated as strings (even if they resemble a number `-x 33`).
   * `opts.configuration`: provide configuration options to the yargs-parser (see: [configuration](#configuration)).
-  * `opts.number`: keys should be treated as numbers.
 
 **returns:**
 
diff --git a/index.js b/index.js
index 17e69fd..1b08f92 100644
--- a/index.js
+++ b/index.js
@@ -31,7 +31,6 @@ function parse (args, opts) {
     arrays: {},
     bools: {},
     strings: {},
-    numbers: {},
     counts: {},
     normalize: {},
     configs: {},
@@ -51,10 +50,6 @@ function parse (args, opts) {
     flags.strings[key] = true
   })
 
-  ;[].concat(opts.number).filter(Boolean).forEach(function (key) {
-    flags.numbers[key] = true
-  })
-
   ;[].concat(opts.count).filter(Boolean).forEach(function (key) {
     flags.counts[key] = true
   })
@@ -294,8 +289,14 @@ function parse (args, opts) {
   // following it... YUM!
   // e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
   function eatArray (i, key, args) {
+    var start = i + 1
     for (var ii = i + 1; ii < args.length; ii++) {
-      if (/^-/.test(args[ii])) break
+      if (/^-/.test(args[ii])) {
+        if (ii === start) {
+          setArg(key, defaultForType('array'))
+        }
+        break
+      }
       i = ii
       setArg(key, args[ii])
     }
@@ -317,11 +318,7 @@ function parse (args, opts) {
       newAliases[c] = true
     }
 
-    var value = val
-    if (!checkAllAliases(key, flags.strings)) {
-      if (isNumber(val)) value = Number(val)
-      if (!isUndefined(val) && !isNumber(val) && checkAllAliases(key, flags.numbers)) value = NaN
-    }
+    var value = !checkAllAliases(key, flags.strings) && isNumber(val) ? Number(val) : val
 
     if (checkAllAliases(key, flags.counts)) {
       value = increment
@@ -536,7 +533,6 @@ function parse (args, opts) {
     var def = {
       boolean: true,
       string: '',
-      number: undefined,
       array: []
     }
 
@@ -548,7 +544,6 @@ function parse (args, opts) {
     var type = 'boolean'
 
     if (flags.strings && flags.strings[key]) type = 'string'
-    else if (flags.numbers && flags.numbers[key]) type = 'number'
     else if (flags.arrays && flags.arrays[key]) type = 'array'
 
     return type
@@ -561,10 +556,6 @@ function parse (args, opts) {
     return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
   }
 
-  function isUndefined (num) {
-    return num === undefined
-  }
-
   return {
     argv: argv,
     error: error,
diff --git a/package.json b/package.json
index 4d364e3..c823a52 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "yargs-parser",
-  "version": "2.1.0",
+  "version": "2.0.0",
   "description": "the mighty option parser used by yargs",
   "main": "index.js",
   "scripts": {


Test patch:
diff --git a/test/yargs-parser.js b/test/yargs-parser.js
index 8364d9b..4019735 100644
--- a/test/yargs-parser.js
+++ b/test/yargs-parser.js
@@ -175,20 +175,6 @@ describe('yargs-parser', function () {
     x.should.be.a('string').and.equal('56')
   })
 
-  it('should default numbers to undefined', function () {
-    var n = parser([ '-n' ], {
-      number: ['n']
-    }).n
-    expect(n).to.equal(undefined)
-  })
-
-  it('should default number to NaN if value is not a valid number', function () {
-    var n = parser([ '-n', 'string' ], {
-      number: ['n']
-    }).n
-    expect(n).to.deep.equal(NaN)
-  })
-
   // Fixes: https://github.com/bcoe/yargs/issues/68
   it('should parse flag arguments with no right-hand-value as strings, if defined as strings', function () {
     var s = parser([ '-s' ], {
@@ -1124,11 +1110,25 @@ describe('yargs-parser', function () {
       parse.should.have.property('_').with.length(0)
     })
 
+    it('should default an array to an empty array if passed as first option followed by another', function () {
+      var result = parser(['-a', '-b'], {
+        array: 'a'
+      })
+      result.should.have.property('a').and.deep.equal([])
+    })
+
+    it('should not attempt to default array if an element has already been populated', function () {
+      var result = parser(['-a', 'foo', 'bar', '-b'], {
+        array: 'a'
+      })
+      result.should.have.property('a').and.deep.equal(['foo', 'bar'])
+    })
+
     it('should default argument to empty array if no value given', function () {
       var result = parser(['-b'], {
         array: 'b'
       })
-      Array.isArray(result.b).should.equal(true)
+      result.should.have.property('b').and.deep.equal([])
     })
 
     it('should place value of argument in array, when one argument provided', function () {