From 84a401f0fa3095e0a19661670d1570d0c3b9d3c9 Mon Sep 17 00:00:00 2001 From: Juerg B <44573692+juergba@users.noreply.github.com> Date: Mon, 28 Oct 2019 23:35:02 +0100 Subject: [PATCH] feat!: populate error if incompatible narg/count or array/count options are used (#191) --- index.js | 16 ++++++++++++++++ test/yargs-parser.js | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/index.js b/index.js index 22d1c4ed..3178f61c 100644 --- a/index.js +++ b/index.js @@ -131,6 +131,8 @@ function parse (args, opts) { }) }) + checkConfiguration() + var argv = { _: [] } var notFlags = [] @@ -874,6 +876,20 @@ function parse (args, opts) { return num === undefined } + // check user configuration settings for inconsistencies + function checkConfiguration () { + // count keys should not be set as array/narg + Object.keys(flags.counts).find(key => { + if (checkAllAliases(key, flags.arrays)) { + error = Error(__('Invalid configuration: %s, opts.count excludes opts.array.', key)) + return true + } else if (checkAllAliases(key, flags.nargs)) { + error = Error(__('Invalid configuration: %s, opts.count excludes opts.narg.', key)) + return true + } + }) + } + return { argv: argv, error: error, diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 0dca6ba9..f4d5d865 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1487,6 +1487,24 @@ describe('yargs-parser', function () { ], { count: 'v' }) parsed.v.should.equal(8) }) + + it('should add an error if counter is also set as array', function () { + var argv = parser.detailed(['--counter', '--counter', '--counter'], { + count: ['counter'], + array: ['counter'] + }) + + argv.error.message.should.equal('Invalid configuration: counter, opts.count excludes opts.array.') + }) + + it('should add an error if counter is also set as narg', function () { + var argv = parser.detailed(['--counter', 'foo', 'bar'], { + count: ['counter'], + narg: { 'counter': 2 } + }) + + argv.error.message.should.equal('Invalid configuration: counter, opts.count excludes opts.narg.') + }) }) describe('array', function () {