Skip to content

Commit

Permalink
Fixed: Throw an exception if both lazyMultiple and multiple are set #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Joni Toyryla committed Nov 10, 2023
1 parent bfc3d9d commit 3a754b7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}
}

/**
Expand Down
10 changes: 10 additions & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}
}

/**
Expand Down
21 changes: 21 additions & 0 deletions dist/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}
}

/**
Expand Down Expand Up @@ -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 () {
Expand Down
10 changes: 10 additions & 0 deletions lib/option-definitions.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/multiple.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit 3a754b7

Please sign in to comment.