From f3e4616771ee4927414f98ccd247b21e78fc419f Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Sun, 7 Aug 2016 23:26:20 -0700 Subject: [PATCH] fix: check aliases when guessing defaults for arguments fixes #41 (#43) --- index.js | 6 +++--- test/yargs-parser.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 6e15d4e2..26cbfd1d 100644 --- a/index.js +++ b/index.js @@ -594,9 +594,9 @@ function parse (args, opts) { function guessType (key, flags) { 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' + if (checkAllAliases(key, flags.strings)) type = 'string' + else if (checkAllAliases(key, flags.numbers)) type = 'number' + else if (checkAllAliases(key, flags.arrays)) type = 'array' return type } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 6e400d3d..f35d8ff4 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1903,4 +1903,34 @@ describe('yargs-parser', function () { }) }) }) + + // addresses: https://github.com/yargs/yargs-parser/issues/41 + it('defaults to empty array if array option is provided no values', function () { + var parsed = parser(['-f'], { + 'alias': { + 'f': 'files' + }, + 'array': ['files'] + }) + parsed.f.should.deep.equal([]) + parsed.files.should.deep.equal([]) + + parsed = parser(['--files'], { + 'alias': { + 'f': 'files' + }, + 'array': ['files'] + }) + parsed.f.should.deep.equal([]) + parsed.files.should.deep.equal([]) + + parsed = parser(['-f', '-y'], { + 'alias': { + 'f': 'files' + }, + 'array': ['files'] + }) + parsed.f.should.deep.equal([]) + parsed.files.should.deep.equal([]) + }) })