[Pkg-javascript-commits] [node-yargs-parser] 01/04: New upstream version 8.0.0

Praveen Arimbrathodiyil praveen at moszumanska.debian.org
Tue Dec 5 03:53:55 UTC 2017


This is an automated email from the git hooks/post-receive script.

praveen pushed a commit to branch master
in repository node-yargs-parser.

commit 8c8a191914b6c5af7f89d9ead4a5948ae67ec5e2
Author: Pirate Praveen <praveen at debian.org>
Date:   Mon Dec 4 21:25:14 2017 +0530

    New upstream version 8.0.0
---
 .travis.yml                 |   5 +-
 CHANGELOG.md                | 115 +++++++++++++
 README.md                   |  87 +++++++++-
 appveyor.yml                |   2 +-
 index.js                    | 142 ++++++++++------
 lib/tokenize-arg-string.js  |   6 +-
 package.json                |  10 +-
 test/tokenize-arg-string.js |   8 +
 test/yargs-parser.js        | 387 +++++++++++++++++++++++++++++++++++++++++---
 9 files changed, 684 insertions(+), 78 deletions(-)

diff --git a/.travis.yml b/.travis.yml
index f3760a1..a0fa326 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,9 +2,8 @@ language: node_js
 os:
   - linux
 node_js:
-  - "0.10"
-  - "0.12"
-  - "4.1"
+  - "4"
+  - "6"
   - "node"
 after_script: npm run coverage
 deploy:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d6d504..f9e7c62 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,121 @@
 
 All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
 
+<a name="8.0.0"></a>
+# [8.0.0](https://github.com/yargs/yargs-parser/compare/v7.0.0...v8.0.0) (2017-10-05)
+
+
+### Bug Fixes
+
+* Ignore multiple spaces between arguments. ([#100](https://github.com/yargs/yargs-parser/issues/100)) ([d137227](https://github.com/yargs/yargs-parser/commit/d137227))
+
+
+### Features
+
+* allow configuration of prefix for boolean negation ([#94](https://github.com/yargs/yargs-parser/issues/94)) ([00bde7d](https://github.com/yargs/yargs-parser/commit/00bde7d))
+* reworking how numbers are parsed ([#104](https://github.com/yargs/yargs-parser/issues/104)) ([fba00eb](https://github.com/yargs/yargs-parser/commit/fba00eb))
+
+
+### BREAKING CHANGES
+
+* strings that fail `Number.isSafeInteger()` are no longer coerced into numbers. 
+
+
+
+<a name="7.0.0"></a>
+# [7.0.0](https://github.com/yargs/yargs-parser/compare/v6.0.1...v7.0.0) (2017-05-02)
+
+
+### Chores
+
+* revert populate-- logic ([#91](https://github.com/yargs/yargs-parser/issues/91)) ([6003e6d](https://github.com/yargs/yargs-parser/commit/6003e6d))
+
+
+### BREAKING CHANGES
+
+* populate-- now defaults to false.
+
+
+
+<a name="6.0.1"></a>
+## [6.0.1](https://github.com/yargs/yargs-parser/compare/v6.0.0...v6.0.1) (2017-05-01)
+
+
+### Bug Fixes
+
+* default '--' to undefined when not provided; this is closer to the array API ([#90](https://github.com/yargs/yargs-parser/issues/90)) ([4e739cc](https://github.com/yargs/yargs-parser/commit/4e739cc))
+
+
+
+<a name="6.0.0"></a>
+# [6.0.0](https://github.com/yargs/yargs-parser/compare/v4.2.1...v6.0.0) (2017-05-01)
+
+
+### Bug Fixes
+
+* environment variables should take precedence over config file ([#81](https://github.com/yargs/yargs-parser/issues/81)) ([76cee1f](https://github.com/yargs/yargs-parser/commit/76cee1f))
+* parsing hints should apply for dot notation keys ([#86](https://github.com/yargs/yargs-parser/issues/86)) ([3e47d62](https://github.com/yargs/yargs-parser/commit/3e47d62))
+
+
+### Chores
+
+* upgrade to newest version of camelcase ([#87](https://github.com/yargs/yargs-parser/issues/87)) ([f1903aa](https://github.com/yargs/yargs-parser/commit/f1903aa))
+
+
+### Features
+
+* add -- option which allows arguments after the -- flag to be returned separated from positional arguments ([#84](https://github.com/yargs/yargs-parser/issues/84)) ([2572ca8](https://github.com/yargs/yargs-parser/commit/2572ca8))
+* when parsing stops, we now populate "--" by default ([#88](https://github.com/yargs/yargs-parser/issues/88)) ([cd666db](https://github.com/yargs/yargs-parser/commit/cd666db))
+
+
+### BREAKING CHANGES
+
+* rather than placing arguments in "_", when parsing is stopped via "--"; we now populate an array called "--" by default.
+* camelcase now requires Node 4+.
+* environment variables will now override config files (args, env, config-file, config-object)
+
+
+
+<a name="5.0.0"></a>
+# [5.0.0](https://github.com/yargs/yargs-parser/compare/v4.2.1...v5.0.0) (2017-02-18)
+
+
+### Bug Fixes
+
+* environment variables should take precedence over config file ([#81](https://github.com/yargs/yargs-parser/issues/81)) ([76cee1f](https://github.com/yargs/yargs-parser/commit/76cee1f))
+
+
+### BREAKING CHANGES
+
+* environment variables will now override config files (args, env, config-file, config-object)
+
+
+
+<a name="4.2.1"></a>
+## [4.2.1](https://github.com/yargs/yargs-parser/compare/v4.2.0...v4.2.1) (2017-01-02)
+
+
+### Bug Fixes
+
+* flatten/duplicate regression ([#75](https://github.com/yargs/yargs-parser/issues/75)) ([68d68a0](https://github.com/yargs/yargs-parser/commit/68d68a0))
+
+
+
+<a name="4.2.0"></a>
+# [4.2.0](https://github.com/yargs/yargs-parser/compare/v4.1.0...v4.2.0) (2016-12-01)
+
+
+### Bug Fixes
+
+* inner objects in configs had their keys appended to top-level key when dot-notation was disabled ([#72](https://github.com/yargs/yargs-parser/issues/72)) ([0b1b5f9](https://github.com/yargs/yargs-parser/commit/0b1b5f9))
+
+
+### Features
+
+* allow multiple arrays to be provided, rather than always combining ([#71](https://github.com/yargs/yargs-parser/issues/71)) ([0f0fb2d](https://github.com/yargs/yargs-parser/commit/0f0fb2d))
+
+
+
 <a name="4.1.0"></a>
 # [4.1.0](https://github.com/yargs/yargs-parser/compare/v4.0.2...v4.1.0) (2016-11-07)
 
diff --git a/README.md b/README.md
index 01270d8..a368e63 100644
--- a/README.md
+++ b/README.md
@@ -11,7 +11,7 @@ The mighty option parser used by [yargs](https://github.com/yargs/yargs).
 
 visit the [yargs website](http://yargs.js.org/) for more examples, and thorough usage instructions.
 
-<img width="250" src="yargs-logo.png">
+<img width="250" src="https://raw.githubusercontent.com/yargs/yargs-parser/master/yargs-logo.png">
 
 ## Example
 
@@ -72,12 +72,14 @@ Parses command line arguments returning a simple mapping of keys and values.
   * `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.
+  * `opts['--']`: arguments after the end-of-options flag `--` will be set to the `argv.['--']` array instead of being set to the `argv._` array.
 
 **returns:**
 
 * `obj`: an object representing the parsed value of `args`
   * `key/value`: key value pairs for each argument and their aliases.
   * `_`: an array representing the positional arguments.
+  * [optional] `--`:  an array with arguments after the end-of-options flag `--`.
 
 ### require('yargs-parser').detailed(args, opts={})
 
@@ -100,6 +102,7 @@ yargs engine.
 * `configuration`: the configuration loaded from the `yargs` stanza in package.json.
 
 <a name="configuration"></a>
+
 ### Configuration
 
 The yargs-parser applies several automated transformations on the keys provided
@@ -174,7 +177,7 @@ node example.js --foo.bar
 ### parse numbers
 
 * default: `true`
-* key: 'parse-numbers'
+* key: `parse-numbers`
 
 Should keys that look like numbers be treated as such?
 
@@ -193,7 +196,7 @@ node example.js --foo=99.3
 ### boolean negation
 
 * default: `true`
-* key: 'boolean-negation'
+* key: `boolean-negation`
 
 Should variables prefixed with `--no` be treated as negations?
 
@@ -209,6 +212,84 @@ node example.js --no-foo
 { _: [], "no-foo": true }
 ```
 
+### duplicate arguments array
+
+* default: `true`
+* key: `duplicate-arguments-array`
+
+Should arguments be coerced into an array when duplicated:
+
+```sh
+node example.js -x 1 -x 2
+{ _: [], x: [1, 2] }
+```
+
+_if disabled:_
+
+```sh
+node example.js -x 1 -x 2
+{ _: [], x: 2 }
+```
+
+### flatten duplicate arrays
+
+* default: `true`
+* key: `flatten-duplicate-arrays`
+
+Should array arguments be coerced into a single array when duplicated:
+
+```sh
+node example.js -x 1 2 -x 3 4
+{ _: [], x: [1, 2, 3, 4] }
+```
+
+_if disabled:_
+
+```sh
+node example.js -x 1 2 -x 3 4
+{ _: [], x: [[1, 2], [3, 4]] }
+```
+
+### negation prefix
+
+* default: `no-`
+* key: `negation-prefix`
+
+The prefix to use for negated boolean variables.
+
+```sh
+node example.js --no-foo
+{ _: [], foo: false }
+```
+
+_if set to `quux`:_
+
+```sh
+node example.js --quuxfoo
+{ _: [], foo: false }
+```
+
+### populate --
+
+* default: `false`.
+* key: `populate--`
+
+Should unparsed flags be stored in `--` or `_`.
+
+_If disabled:_
+
+```sh
+node example.js a -b -- x y
+{ _: [ 'a', 'x', 'y' ], b: true }
+```
+
+_If enabled:_
+
+```sh
+node example.js a -b -- x y
+{ _: [ 'a' ], '--': [ 'x', 'y' ], b: true }
+```
+
 ## Special Thanks
 
 The yargs project evolves from optimist and minimist. It owes its
diff --git a/appveyor.yml b/appveyor.yml
index 2f85f7f..35dd574 100644
--- a/appveyor.yml
+++ b/appveyor.yml
@@ -2,7 +2,7 @@ environment:
   matrix:
     - nodejs_version: '5'
     - nodejs_version: '4'
-    - nodejs_version: '0.12'
+    - nodejs_version: '7'
 install:
   - ps: Install-Product node $env:nodejs_version
   - set CI=true
diff --git a/index.js b/index.js
index 0d3ed26..c06d937 100644
--- a/index.js
+++ b/index.js
@@ -15,11 +15,17 @@ function parse (args, opts) {
     'camel-case-expansion': true,
     'dot-notation': true,
     'parse-numbers': true,
-    'boolean-negation': true
+    'boolean-negation': true,
+    'negation-prefix': 'no-',
+    'duplicate-arguments-array': true,
+    'flatten-duplicate-arrays': true,
+    'populate--': false
   }, opts.configuration)
   var defaults = opts.default || {}
   var configObjects = opts.configObjects || []
   var envPrefix = opts.envPrefix
+  var notFlagsOption = configuration['populate--']
+  var notFlagsArgv = notFlagsOption ? '--' : '_'
   var newAliases = {}
   // allow a i18n handler to be passed in, default to a fake one (util.format).
   var __ = opts.__ || function (str) {
@@ -40,6 +46,7 @@ function parse (args, opts) {
     coercions: {}
   }
   var negative = /^-[0-9]+(\.[0-9]+)?/
+  var negatedBoolean = new RegExp('^--' + configuration['negation-prefix'] + '(.+)')
 
   ;[].concat(opts.array).filter(Boolean).forEach(function (key) {
     flags.arrays[key] = true
@@ -136,8 +143,8 @@ function parse (args, opts) {
       } else {
         setArg(m[1], m[2])
       }
-    } else if (arg.match(/^--no-.+/) && configuration['boolean-negation']) {
-      key = arg.match(/^--no-(.+)/)[1]
+    } else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
+      key = arg.match(negatedBoolean)[1]
       setArg(key, false)
 
     // -- seperated by space.
@@ -263,22 +270,20 @@ function parse (args, opts) {
         }
       }
     } else {
-      argv._.push(
-        flags.strings['_'] || !isNumber(arg) ? arg : Number(arg)
-      )
+      argv._.push(maybeCoerceNumber('_', arg))
     }
   }
 
   // order of precedence:
   // 1. command line arg
-  // 2. value from config file
-  // 3. value from config objects
-  // 4. value from env var
+  // 2. value from env var
+  // 3. value from config file
+  // 4. value from config objects
   // 5. configured default value
   applyEnvVars(argv, true) // special case: check env vars that point to config file
+  applyEnvVars(argv, false)
   setConfig(argv)
   setConfigObjects()
-  applyEnvVars(argv, false)
   applyDefaultsAndAliases(argv, flags.aliases, defaults)
   applyCoercions(argv)
 
@@ -287,8 +292,10 @@ function parse (args, opts) {
     if (!hasKey(argv, key.split('.'))) setArg(key, 0)
   })
 
+  // '--' defaults to undefined.
+  if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = []
   notFlags.forEach(function (key) {
-    argv._.push(key)
+    argv[notFlagsArgv].push(key)
   })
 
   // how many arguments should we consume, based
@@ -310,15 +317,27 @@ function parse (args, opts) {
   // e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
   function eatArray (i, key, args) {
     var start = i + 1
+    var argsToSet = []
+    var multipleArrayFlag = i > 0
     for (var ii = i + 1; ii < args.length; ii++) {
       if (/^-/.test(args[ii]) && !negative.test(args[ii])) {
         if (ii === start) {
           setArg(key, defaultForType('array'))
         }
+        multipleArrayFlag = true
         break
       }
       i = ii
-      setArg(key, args[ii])
+      argsToSet.push(args[ii])
+    }
+    if (multipleArrayFlag) {
+      setArg(key, argsToSet.map(function (arg) {
+        return processValue(key, arg)
+      }))
+    } else {
+      argsToSet.forEach(function (arg) {
+        setArg(key, arg)
+      })
     }
 
     return i
@@ -327,33 +346,11 @@ function parse (args, opts) {
   function setArg (key, val) {
     unsetDefaulted(key)
 
-    // handle parsing boolean arguments --foo=true --bar false.
-    if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
-      if (typeof val === 'string') val = val === 'true'
+    if (/-/.test(key) && configuration['camel-case-expansion']) {
+      addNewAlias(key, camelCase(key))
     }
 
-    if (/-/.test(key) && !(flags.aliases[key] && flags.aliases[key].length) && configuration['camel-case-expansion']) {
-      var c = camelCase(key)
-      flags.aliases[key] = [c]
-      newAliases[c] = true
-    }
-
-    var value = val
-    if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
-      if (isNumber(val)) value = Number(val)
-      if (!isUndefined(val) && !isNumber(val) && checkAllAliases(key, flags.numbers)) value = NaN
-    }
-
-    // increment a count given as arg (either no value or value parsed as boolean)
-    if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) {
-      value = increment
-    }
-
-    // Set normalized value when key is in 'normalize' and in 'arrays'
-    if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) {
-      if (Array.isArray(val)) value = val.map(path.normalize)
-      else value = path.normalize(val)
-    }
+    var value = processValue(key, val)
 
     var splitKey = key.split('.')
     setKey(argv, splitKey, value)
@@ -395,6 +392,45 @@ function parse (args, opts) {
     }
   }
 
+  function addNewAlias (key, alias) {
+    if (!(flags.aliases[key] && flags.aliases[key].length)) {
+      flags.aliases[key] = [alias]
+      newAliases[alias] = true
+    }
+    if (!(flags.aliases[alias] && flags.aliases[alias].length)) {
+      addNewAlias(alias, key)
+    }
+  }
+
+  function processValue (key, val) {
+    // handle parsing boolean arguments --foo=true --bar false.
+    if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) {
+      if (typeof val === 'string') val = val === 'true'
+    }
+
+    var value = maybeCoerceNumber(key, val)
+
+    // increment a count given as arg (either no value or value parsed as boolean)
+    if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) {
+      value = increment
+    }
+
+    // Set normalized value when key is in 'normalize' and in 'arrays'
+    if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) {
+      if (Array.isArray(val)) value = val.map(path.normalize)
+      else value = path.normalize(val)
+    }
+    return value
+  }
+
+  function maybeCoerceNumber (key, value) {
+    if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) {
+      const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (Number.isSafeInteger(parseInt(value)))
+      if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) value = Number(value)
+    }
+    return value
+  }
+
   // set args from config.json file, this should be
   // applied last so that defaults can be applied.
   function setConfig (argv) {
@@ -440,7 +476,10 @@ function parse (args, opts) {
       var value = config[key]
       var fullKey = prev ? prev + '.' + key : key
 
-      if (Object.prototype.toString.call(value) === '[object Object]') {
+      // if the value is an inner object and we have dot-notation
+      // enabled, treat inner objects in config the same as
+      // heavily nested dot notations (foo.bar.apple).
+      if (typeof value === 'object' && !Array.isArray(value) && configuration['dot-notation']) {
         // if the value is an object but not an array, check nested object
         setConfigObject(value, fullKey)
       } else {
@@ -536,16 +575,26 @@ function parse (args, opts) {
 
     var key = keys[keys.length - 1]
 
+    var isTypeArray = checkAllAliases(keys.join('.'), flags.arrays)
+    var isValueArray = Array.isArray(value)
+    var duplicate = configuration['duplicate-arguments-array']
+
     if (value === increment) {
       o[key] = increment(o[key])
-    } else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) {
-      o[key] = Array.isArray(value) ? value : [value]
-    } else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) {
-      o[key] = value
     } else if (Array.isArray(o[key])) {
-      o[key].push(value)
-    } else {
+      if (duplicate && isTypeArray && isValueArray) {
+        o[key] = configuration['flatten-duplicate-arrays'] ? o[key].concat(value) : [o[key]].concat([value])
+      } else if (!duplicate && Boolean(isTypeArray) === Boolean(isValueArray)) {
+        o[key] = value
+      } else {
+        o[key] = o[key].concat([value])
+      }
+    } else if (o[key] === undefined && isTypeArray) {
+      o[key] = isValueArray ? value : [value]
+    } else if (duplicate && !(o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts))) {
       o[key] = [ o[key], value ]
+    } else {
+      o[key] = value
     }
   }
 
@@ -563,7 +612,9 @@ function parse (args, opts) {
         flags.aliases[key].concat(key).forEach(function (x) {
           if (/-/.test(x) && configuration['camel-case-expansion']) {
             var c = camelCase(x)
-            flags.aliases[key].push(c)
+            if (flags.aliases[key].indexOf(c) === -1) {
+              flags.aliases[key].push(c)
+            }
             newAliases[c] = true
           }
         })
@@ -625,7 +676,6 @@ function parse (args, opts) {
   }
 
   function isNumber (x) {
-    if (!configuration['parse-numbers']) return false
     if (typeof x === 'number') return true
     if (/^0x[0-9a-f]+$/i.test(x)) return true
     return /^[-+]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x)
diff --git a/lib/tokenize-arg-string.js b/lib/tokenize-arg-string.js
index 23d39e1..7015442 100644
--- a/lib/tokenize-arg-string.js
+++ b/lib/tokenize-arg-string.js
@@ -3,16 +3,20 @@ module.exports = function (argString) {
   if (Array.isArray(argString)) return argString
 
   var i = 0
+  var prevC = null
   var c = null
   var opening = null
   var args = []
 
   for (var ii = 0; ii < argString.length; ii++) {
+    prevC = c
     c = argString.charAt(ii)
 
     // split on spaces unless we're in quotes.
     if (c === ' ' && !opening) {
-      i++
+      if (!(prevC === ' ')) {
+        i++
+      }
       continue
     }
 
diff --git a/package.json b/package.json
index a2f4baa..9717e9e 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "yargs-parser",
-  "version": "4.1.0",
+  "version": "8.0.0",
   "description": "the mighty option parser used by yargs",
   "main": "index.js",
   "scripts": {
@@ -29,12 +29,12 @@
     "chai": "^3.5.0",
     "coveralls": "^2.11.12",
     "mocha": "^3.0.1",
-    "nyc": "^8.1.0",
-    "standard": "^8.0.0",
-    "standard-version": "^3.0.0"
+    "nyc": "^11.2.1",
+    "standard": "^10.0.2",
+    "standard-version": "^4.0.0"
   },
   "dependencies": {
-    "camelcase": "^3.0.0"
+    "camelcase": "^4.1.0"
   },
   "files": [
     "lib",
diff --git a/test/tokenize-arg-string.js b/test/tokenize-arg-string.js
index 9b9f9a0..63ad295 100644
--- a/test/tokenize-arg-string.js
+++ b/test/tokenize-arg-string.js
@@ -3,6 +3,7 @@
 var tokenizeArgString = require('../lib/tokenize-arg-string')
 
 require('chai').should()
+var expect = require('chai').expect
 
 describe('TokenizeArgString', function () {
   it('handles unquoted string', function () {
@@ -37,4 +38,11 @@ describe('TokenizeArgString', function () {
     args[1].should.equal('hello \'world\'')
     args[2].should.equal('--bar=foo "bar"')
   })
+
+  it('multiple spaces only counted in quotes', function () {
+    var args = tokenizeArgString('foo  bar  "foo  bar"')
+    args[0].should.equal('foo')
+    expect(args[1]).equal('bar')
+    expect(args[2]).equal('foo  bar')
+  })
 })
diff --git a/test/yargs-parser.js b/test/yargs-parser.js
index 8529d85..d2d5654 100644
--- a/test/yargs-parser.js
+++ b/test/yargs-parser.js
@@ -10,12 +10,14 @@ var path = require('path')
 describe('yargs-parser', function () {
   it('should parse a "short boolean"', function () {
     var parse = parser([ '-b' ])
+    parse.should.not.have.property('--')
     parse.should.have.property('b').to.be.ok.and.be.a('boolean')
     parse.should.have.property('_').with.length(0)
   })
 
   it('should parse a "long boolean"', function () {
     var parse = parser('--bool')
+    parse.should.not.have.property('--')
     parse.should.have.property('bool', true)
     parse.should.have.property('_').with.length(0)
   })
@@ -94,7 +96,11 @@ describe('yargs-parser', function () {
       '--key', 'value',
       '-b', '--bool', '--no-meep', '--multi=baz',
       '--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
-    ])
+    ], {
+      configuration: {
+        'populate--': false
+      }
+    })
     parse.should.have.property('c', true)
     parse.should.have.property('a', true)
     parse.should.have.property('t', true)
@@ -955,7 +961,7 @@ describe('yargs-parser', function () {
         boolean: ['b']
       })
       // Assert
-      result.should.have.property('b').that.is.a('boolean').and.is.true
+      result.should.have.property('b').that.is.a('boolean').and.is.true // eslint-disable-line
       result.should.have.property('_').and.deep.equal([123])
     })
   })
@@ -1047,11 +1053,11 @@ describe('yargs-parser', function () {
           }
 
           it('should set true if --flag in arg', function () {
-            parser(['--flag'], opts).flag.should.be.true
+            parser(['--flag'], opts).flag.should.be.true // eslint-disable-line
           })
 
           it('should set false if --no-flag in arg', function () {
-            parser(['--no-flag'], opts).flag.should.be.false
+            parser(['--no-flag'], opts).flag.should.be.false // eslint-disable-line
           })
 
           it('should set ' + def + ' if no flag in arg', function () {
@@ -1070,15 +1076,15 @@ describe('yargs-parser', function () {
         })
 
         it('should set true if --flag in arg', function () {
-          parser(['--flag'], opts).flag.should.be.true
+          parser(['--flag'], opts).flag.should.be.true // eslint-disable-line
         })
 
         it('should set false if --no-flag in arg', function () {
-          parser(['--no-flag'], opts).flag.should.be.false
+          parser(['--no-flag'], opts).flag.should.be.false // eslint-disable-line
         })
 
         it('should set false if no flag in arg', function () {
-          parser([], opts).flag.should.be.false
+          parser([], opts).flag.should.be.false // eslint-disable-line
         })
       })
 
@@ -1135,8 +1141,8 @@ describe('yargs-parser', function () {
         it('should provide options with dashes as camelCase properties', function () {
           var result = parser(['--some-option'])
 
-          result.should.have.property('some-option').that.is.a('boolean').and.is.true
-          result.should.have.property('someOption').that.is.a('boolean').and.is.true
+          result.should.have.property('some-option').that.is.a('boolean').and.is.true // eslint-disable-line
+          result.should.have.property('someOption').that.is.a('boolean').and.is.true // eslint-disable-line
         })
       }
 
@@ -1156,8 +1162,8 @@ describe('yargs-parser', function () {
           }
         })
 
-        result.should.have.property('some-option').that.is.a('boolean').and.is.true
-        result.should.have.property('someOption').that.is.a('boolean').and.is.true
+        result.should.have.property('some-option').that.is.a('boolean').and.is.true // eslint-disable-line
+        result.should.have.property('someOption').that.is.a('boolean').and.is.true // eslint-disable-line
       })
 
       it('should provide defaults of options with dashes as camelCase properties', function () {
@@ -1329,13 +1335,13 @@ describe('yargs-parser', function () {
         count: 'v',
         default: { v: undefined }
       })
-      expect(parsed.v).to.be.undefined
+      expect(parsed.v).to.be.undefined // eslint-disable-line
 
       parsed = parser([], {
         count: 'v',
         default: { v: null }
       })
-      expect(parsed.v).to.be.null
+      expect(parsed.v).to.be.null // eslint-disable-line
 
       parsed = parser([], {
         count: 'v',
@@ -1374,11 +1380,16 @@ describe('yargs-parser', function () {
   })
 
   describe('array', function () {
-    it('should group values into an array if the same option is specified multiple times', function () {
-      var parse = parser(['-v', 'a', '-v', 'b', '-v', 'c'])
+    it('should group values into an array if the same option is specified multiple times (duplicate-arguments-array=true)', function () {
+      var parse = parser(['-v', 'a', '-v', 'b', '-v', 'c'], {configuration: {'duplicate-arguments-array': true}})
       parse.should.have.property('v').and.deep.equal(['a', 'b', 'c'])
       parse.should.have.property('_').with.length(0)
     })
+    it('should keep only the last value if the same option is specified multiple times (duplicate-arguments-false)', function () {
+      var parse = parser(['-v', 'a', '-v', 'b', '-v', 'c'], {configuration: {'duplicate-arguments-array': false}})
+      parse.should.have.property('v').and.equal('c')
+      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'], {
@@ -1623,8 +1634,8 @@ describe('yargs-parser', function () {
 
       result.eggs.should.equal('sam')
       result.ham.should.equal('iam')
-      expect(result.oneFish).to.be.undefined
-      expect(result.redFish).to.be.undefined
+      expect(result.oneFish).to.be.undefined // eslint-disable-line
+      expect(result.redFish).to.be.undefined // eslint-disable-line
     })
 
     it('should set aliases for options defined by env var', function () {
@@ -1692,7 +1703,7 @@ describe('yargs-parser', function () {
     })
 
     var jsonPath = path.resolve(__dirname, './fixtures/config.json')
-    it('should prefer config file value over env var', function () {
+    it('should prefer environment variables over config file', function () {
       process.env.CFG_HERP = 'zerp'
       var result = parser(['--cfg', jsonPath], {
         envPrefix: 'CFG',
@@ -1703,7 +1714,7 @@ describe('yargs-parser', function () {
         }
       })
 
-      result.herp.should.equal('derp')
+      result.herp.should.equal('zerp')
     })
 
     it('should support an env var value as config file option', function () {
@@ -1886,6 +1897,31 @@ describe('yargs-parser', function () {
         expect(parsed['foo.bar']).to.equal('banana')
         expect(parsed).not.to.include.keys('f.bar')
       })
+
+      // addresses https://github.com/yargs/yargs/issues/716
+      it('does not append nested-object keys from config to top-level key', function () {
+        var parsed = parser([], {
+          alias: {
+            'foo': ['f']
+          },
+          configuration: {
+            'dot-notation': false
+          },
+          configObjects: [
+            {
+              'website.com': {
+                a: 'b',
+                b: 'c'
+              }
+            }
+          ]
+        })
+
+        parsed['website.com'].should.deep.equal({
+          a: 'b',
+          b: 'c'
+        })
+      })
     })
 
     describe('parse numbers', function () {
@@ -1919,6 +1955,17 @@ describe('yargs-parser', function () {
         })
         expect(parsed._[0]).to.equal('5')
       })
+
+      it('parses number if option explicitly set to number type', function () {
+        var parsed = parser(['--foo', '5', '--bar', '6'], {
+          number: 'bar',
+          configuration: {
+            'parse-numbers': false
+          }
+        })
+        expect(parsed['foo']).to.equal('5')
+        expect(parsed['bar']).to.equal(6)
+      })
     })
 
     describe('boolean negation', function () {
@@ -1932,6 +1979,295 @@ describe('yargs-parser', function () {
         parsed['no-dice'].should.equal(true)
         expect(parsed.dice).to.equal(undefined)
       })
+
+      it('negates boolean arguments with correct prefix', function () {
+        var parsed = parser(['--foodice'], {
+          configuration: {
+            'negation-prefix': 'foo'
+          }
+        })
+
+        expect(parsed['dice']).to.equal(false)
+      })
+    })
+
+    describe('duplicate arguments array', function () {
+      it('adds duplicate argument to array', function () {
+        var parsed = parser('-x a -x b', {
+          configuration: {
+            'duplicate-arguments-array': true
+          }
+        })
+
+        parsed['x'].should.deep.equal(['a', 'b'])
+      })
+      it('keeps only last argument', function () {
+        var parsed = parser('-x a -x b', {
+          configuration: {
+            'duplicate-arguments-array': false
+          }
+        })
+
+        parsed['x'].should.equal('b')
+      })
+    })
+
+    describe('flatten duplicate arrays', function () {
+      it('flattens duplicate array type', function () {
+        var parsed = parser('-x a b -x c d', {
+          array: ['x'],
+          configuration: {
+            'flatten-duplicate-arrays': true
+          }
+        })
+
+        parsed['x'].should.deep.equal(['a', 'b', 'c', 'd'])
+      })
+      it('nests duplicate array types', function () {
+        var parsed = parser('-x a b -x c d', {
+          array: ['x'],
+          configuration: {
+            'flatten-duplicate-arrays': false
+          }
+        })
+
+        parsed['x'].should.deep.equal([['a', 'b'], ['c', 'd']])
+      })
+      it('doesn\'t nests single arrays', function () {
+        var parsed = parser('-x a b', {
+          array: ['x'],
+          configuration: {
+            'flatten-duplicate-arrays': false
+          }
+        })
+
+        parsed['x'].should.deep.equal(['a', 'b'])
+      })
+
+      it('flattens duplicate array type, when argument uses dot notation', function () {
+        var parsed = parser('-x.foo a -x.foo b', {
+          array: ['x.foo'],
+          configuration: {
+            'flatten-duplicate-arrays': true
+          }
+        })
+
+        parsed['x'].should.deep.equal({foo: ['a', 'b']})
+      })
+    })
+
+    describe('duplicate-arguments-array VS flatten-duplicate-arrays', function () {
+      /*
+        duplicate=false, flatten=false
+          type=array
+            [-x 1 2 3]          => [1, 2, 3]
+            [-x 1 2 3 -x 2 3 4] => [2, 3, 4]
+          type=string/number/etc
+            [-x 1 -x 2 -x 3]    => 3
+
+        duplicate=false, flatten=true
+          type=array
+            [-x 1 2 3]          => [1, 2, 3]
+            [-x 1 2 3 -x 2 3 4] => [2, 3, 4]
+          type=string/number/etc
+            [-x 1 -x 2 -x 3]    => 3
+
+        duplicate=true, flatten=true
+          type=array
+            [-x 1 2 3]          => [1, 2, 3]
+            [-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]
+          type=string/number/etc
+            [-x 1 -x 2 -x 3]    => [1, 2, 3]
+
+        duplicate=true, flatten=false
+          type=array
+            [-x 1 2 3]          => [1, 2, 3]
+            [-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [2, 3, 4]]
+          type=string/number/etc
+            [-x 1 -x 2 -x 3]    => [1, 2, 3]
+      */
+      describe('duplicate=false, flatten=false,', function () {
+        describe('type=array', function () {
+          it('[-x 1 2 3] => [1, 2, 3]', function () {
+            var parsed = parser('-x 1 2 3', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': false,
+                'flatten-duplicate-arrays': false
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3])
+          })
+          it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () {
+            var parsed = parser('-x 1 2 3 -x 2 3 4', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': false,
+                'flatten-duplicate-arrays': false
+              }
+            })
+            parsed['x'].should.deep.equal([2, 3, 4])
+          })
+        })
+        describe('type=number', function () {
+          it('[-x 1 -x 2 -x 3] => 3', function () {
+            var parsed = parser('-x 1 -x 2 -x 3', {
+              number: 'x',
+              configuration: {
+                'duplicate-arguments-array': false,
+                'flatten-duplicate-arrays': false
+              }
+            })
+            parsed['x'].should.deep.equal(3)
+          })
+        })
+      })
+      describe('duplicate=false, flatten=true,', function () {
+        describe('type=array', function () {
+          it('[-x 1 2 3] => [1, 2, 3]', function () {
+            var parsed = parser('-x 1 2 3', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': false,
+                'flatten-duplicate-arrays': true
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3])
+          })
+          it('[-x 1 2 3 -x 2 3 4] => [2, 3, 4]', function () {
+            var parsed = parser('-x 1 2 3 -x 2 3 4', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': false,
+                'flatten-duplicate-arrays': true
+              }
+            })
+            parsed['x'].should.deep.equal([2, 3, 4])
+          })
+        })
+        describe('type=number', function () {
+          it('[-x 1 -x 2 -x 3] => 3', function () {
+            var parsed = parser('-x 1 -x 2 -x 3', {
+              number: 'x',
+              configuration: {
+                'duplicate-arguments-array': false,
+                'flatten-duplicate-arrays': true
+              }
+            })
+            parsed['x'].should.deep.equal(3)
+          })
+        })
+      })
+      describe('duplicate=true, flatten=true,', function () {
+        describe('type=array', function () {
+          it('[-x 1 2 3] => [1, 2, 3]', function () {
+            var parsed = parser('-x 1 2 3', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': true,
+                'flatten-duplicate-arrays': true
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3])
+          })
+          it('[-x 1 2 3 -x 2 3 4] => [1, 2, 3, 2, 3, 4]', function () {
+            var parsed = parser('-x 1 2 3 -x 2 3 4', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': true,
+                'flatten-duplicate-arrays': true
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3, 2, 3, 4])
+          })
+        })
+        describe('type=number', function () {
+          it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () {
+            var parsed = parser('-x 1 -x 2 -x 3', {
+              number: 'x',
+              configuration: {
+                'duplicate-arguments-array': true,
+                'flatten-duplicate-arrays': true
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3])
+          })
+        })
+      })
+      describe('duplicate=true, flatten=false,', function () {
+        describe('type=array', function () {
+          it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () {
+            var parsed = parser('-x 1 -x 2 -x 3', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': true,
+                'flatten-duplicate-arrays': false
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3])
+          })
+          it('[-x 1 2 3 -x 2 3 4] => [[1, 2, 3], [ 2, 3, 4]]', function () {
+            var parsed = parser('-x 1 2 3 -x 2 3 4', {
+              array: ['x'],
+              configuration: {
+                'duplicate-arguments-array': true,
+                'flatten-duplicate-arrays': false
+              }
+            })
+            parsed['x'].should.deep.equal([[1, 2, 3], [2, 3, 4]])
+          })
+        })
+        describe('type=number', function () {
+          it('[-x 1 -x 2 -x 3] => [1, 2, 3]', function () {
+            var parsed = parser('-x 1 -x 2 -x 3', {
+              number: 'x',
+              configuration: {
+                'duplicate-arguments-array': true,
+                'flatten-duplicate-arrays': false
+              }
+            })
+            parsed['x'].should.deep.equal([1, 2, 3])
+          })
+        })
+      })
+    })
+
+    describe('populate--', function () {
+      it('should populate "_" by default', function () {
+        var result = parser([
+          'bare',
+          '--', '-h', 'eek', '--'
+        ])
+        result.should.have.property('_').and.deep.equal(['bare', '-h', 'eek', '--'])
+        result.should.not.have.property('--')
+      })
+
+      it('should populate the "--" if populate-- is "true"', function () {
+        var result = parser([
+          '--name=meowmers', 'bare', '-cats', 'woo', 'moxy',
+          '-h', 'awesome', '--multi=quux',
+          '--key', 'value',
+          '-b', '--bool', '--no-meep', '--multi=baz',
+          '--', '--not-a-flag', '-', '-h', '-multi', '--', 'eek'
+        ], {
+          configuration: {
+            'populate--': true
+          }
+        })
+        result.should.have.property('c', true)
+        result.should.have.property('a', true)
+        result.should.have.property('t', true)
+        result.should.have.property('s', 'woo')
+        result.should.have.property('h', 'awesome')
+        result.should.have.property('b', true)
+        result.should.have.property('bool', true)
+        result.should.have.property('key', 'value')
+        result.should.have.property('multi').and.deep.equal(['quux', 'baz'])
+        result.should.have.property('meep', false)
+        result.should.have.property('name', 'meowmers')
+        result.should.have.property('_').and.deep.equal(['bare', 'moxy'])
+        result.should.have.property('--').and.deep.equal(['--not-a-flag', '-', '-h', '-multi', '--', 'eek'])
+      })
     })
   })
 
@@ -2115,4 +2451,17 @@ describe('yargs-parser', function () {
     })
     argv.a.should.deep.equal(['a.txt', 'b.txt'])
   })
+
+  // see: https://github.com/yargs/yargs/issues/963
+  it('does not magically convert numeric strings larger than Number.MAX_SAFE_INTEGER', () => {
+    const argv = parser([ '--foo', '93940495950949399948393' ])
+    argv.foo.should.equal('93940495950949399948393')
+  })
+
+  it('converts numeric options larger than Number.MAX_SAFE_INTEGER to number', () => {
+    const argv = parser([ '--foo', '93940495950949399948393' ], {
+      number: ['foo']
+    })
+    argv.foo.should.equal(9.39404959509494e+22)
+  })
 })

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-yargs-parser.git



More information about the Pkg-javascript-commits mailing list