Repository: yargs/yargs-parser
Pull request #7 (2016-02-14 02:40:36)

New type number
This PR builds on top of @maxrimue #5 ; I wasn't able to push to his fork, so creating a new one. 

Summary: checks if value is not a number and assigns a `NaN`. 


Patch:
diff --git a/README.md b/README.md
index 44d2e00..4b76a99 100644
--- a/README.md
+++ b/README.md
@@ -59,6 +59,7 @@ 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 ccca5df..17e69fd 100644
--- a/index.js
+++ b/index.js
@@ -31,6 +31,7 @@ function parse (args, opts) {
     arrays: {},
     bools: {},
     strings: {},
+    numbers: {},
     counts: {},
     normalize: {},
     configs: {},
@@ -50,6 +51,10 @@ 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
   })
@@ -312,7 +317,11 @@ function parse (args, opts) {
       newAliases[c] = true
     }
 
-    var value = !checkAllAliases(key, flags.strings) && isNumber(val) ? Number(val) : val
+    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
+    }
 
     if (checkAllAliases(key, flags.counts)) {
       value = increment
@@ -527,6 +536,7 @@ function parse (args, opts) {
     var def = {
       boolean: true,
       string: '',
+      number: undefined,
       array: []
     }
 
@@ -538,6 +548,7 @@ 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
@@ -550,6 +561,10 @@ function parse (args, opts) {
     return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
   }
 
+  function isUndefined (num) {
+    return num === undefined
+  }
+
   return {
     argv: argv,
     error: error,


Test patch:
diff --git a/test/yargs-parser.js b/test/yargs-parser.js
index 7719454..8364d9b 100644
--- a/test/yargs-parser.js
+++ b/test/yargs-parser.js
@@ -175,6 +175,20 @@ 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' ], {