From 4e739ccb9e21ffe861dcae1e0b9799e65d50724d Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Sun, 30 Apr 2017 22:55:06 -0700 Subject: [PATCH] fix: default '--' to undefined when not provided; this is closer to the array API (#90) --- index.js | 6 ++---- test/yargs-parser.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 52763066..15936de8 100644 --- a/index.js +++ b/index.js @@ -101,10 +101,6 @@ function parse (args, opts) { var argv = { _: [] } - if (notFlagsOption) { - argv[notFlagsArgv] = [] - } - Object.keys(flags.bools).forEach(function (key) { setArg(key, !(key in defaults) ? false : defaults[key]) setDefaulted(key) @@ -296,6 +292,8 @@ 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[notFlagsArgv].push(key) }) diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 02fb779c..23c8e642 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) }) @@ -113,6 +115,16 @@ describe('yargs-parser', function () { parse.should.have.property('_').and.deep.equal(['bare', '--not-a-flag', '-', '-h', '-multi', '--', 'eek']) }) + it('should not populate "--" if parsing was not stopped', function () { + var parse = parser([ '-b' ]) + parse.should.not.have.property('--') + }) + + it('should populate "--" if parsing is stopped', function () { + var parse = parser([ '-b', '--', 'foo bar' ]) + parse.should.have.property('--') + }) + it('should parse numbers appropriately', function () { var argv = parser([ '-x', '1234',