From 3a754b72b120882334797380518817081c57019f Mon Sep 17 00:00:00 2001 From: Joni Toyryla Date: Fri, 10 Nov 2023 17:27:03 +0200 Subject: [PATCH] Fixed: Throw an exception if both lazyMultiple and multiple are set #106 --- dist/index.js | 10 ++++++++++ dist/index.mjs | 10 ++++++++++ dist/tests.js | 21 +++++++++++++++++++++ lib/option-definitions.mjs | 10 ++++++++++ test/multiple.mjs | 11 +++++++++++ 5 files changed, 62 insertions(+) diff --git a/dist/index.js b/dist/index.js index 90a0c5e..9a534cb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -947,6 +947,16 @@ class Definitions extends Array { `A boolean option ["${invalidOption.name}"] can not also be the defaultOption.` ); } + + const multipleAndLazyMultiple = this.some(def => { + return def.multiple && def.lazyMultiple + }); + if (multipleAndLazyMultiple) { + halt( + 'INVALID DEFINITIONS', + 'Multiple and LazyMultiple are exclusive options.' + ); + } } /** diff --git a/dist/index.mjs b/dist/index.mjs index 00683e4..e98a24d 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -943,6 +943,16 @@ class Definitions extends Array { `A boolean option ["${invalidOption.name}"] can not also be the defaultOption.` ); } + + const multipleAndLazyMultiple = this.some(def => { + return def.multiple && def.lazyMultiple + }); + if (multipleAndLazyMultiple) { + halt( + 'INVALID DEFINITIONS', + 'Multiple and LazyMultiple are exclusive options.' + ); + } } /** diff --git a/dist/tests.js b/dist/tests.js index 996cc72..6365317 100644 --- a/dist/tests.js +++ b/dist/tests.js @@ -949,6 +949,16 @@ class Definitions extends Array { `A boolean option ["${invalidOption.name}"] can not also be the defaultOption.` ); } + + const multipleAndLazyMultiple = this.some(def => { + return def.multiple && def.lazyMultiple + }); + if (multipleAndLazyMultiple) { + halt( + 'INVALID DEFINITIONS', + 'Multiple and LazyMultiple are exclusive options.' + ); + } } /** @@ -3218,6 +3228,17 @@ runner$l.test('multiple: string, defaultOption', function () { }); }); +runner$l.test('multiple: with lazyMultiple throws exception', function () { + const optionDefinitions = [ + { name: 'one', multiple: true, lazyMultiple: true } + ]; + const argv = []; + const errorThrowingWrapper = () => { + commandLineArgs(optionDefinitions, { argv }); + }; + a.throws(errorThrowingWrapper); +}); + const runner$m = new TestRunner(); runner$m.test('name-alias-mix: one of each', function () { diff --git a/lib/option-definitions.mjs b/lib/option-definitions.mjs index 0bc879c..a53172d 100644 --- a/lib/option-definitions.mjs +++ b/lib/option-definitions.mjs @@ -102,6 +102,16 @@ class Definitions extends Array { `A boolean option ["${invalidOption.name}"] can not also be the defaultOption.` ) } + + const multipleAndLazyMultiple = this.some(def => { + return def.multiple && def.lazyMultiple + }) + if (multipleAndLazyMultiple) { + halt( + 'INVALID DEFINITIONS', + 'Multiple and LazyMultiple are exclusive options.' + ) + } } /** diff --git a/test/multiple.mjs b/test/multiple.mjs index 835579a..9724e1d 100644 --- a/test/multiple.mjs +++ b/test/multiple.mjs @@ -74,3 +74,14 @@ runner.test('multiple: string, defaultOption', function () { one: ['1', '2'] }) }) + +runner.test('multiple: with lazyMultiple throws exception', function () { + const optionDefinitions = [ + { name: 'one', multiple: true, lazyMultiple: true } + ] + const argv = [] + const errorThrowingWrapper = () => { + commandLineArgs(optionDefinitions, { argv }) + } + a.throws(errorThrowingWrapper) +})