Skip to content

Commit 8620a5e

Browse files
committed
fix: unknown-options-as-args should treat negated boolean as unknown when boolean-negation:false
1 parent 553c808 commit 8620a5e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/yargs-parser.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -987,7 +987,10 @@ export class YargsParser {
987987
// e.g. '-a/usr/local'
988988
const flagEndingInNonWordCharacters = /^-+([^=]+?)\W+.*$/
989989
// check the different types of flag styles, including negatedBoolean, a pattern defined near the start of the parse method
990-
return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag, flagEndingInHyphen, flagEndingInDigits, flagEndingInNonWordCharacters)
990+
const patterns = [flagWithEquals, normalFlag, flagEndingInHyphen, flagEndingInDigits, flagEndingInNonWordCharacters];
991+
if (configuration['boolean-negation'])
992+
patterns.push(negatedBoolean);
993+
return !hasFlagsMatching(arg, ...patterns)
991994
}
992995

993996
// make a best effort to pick a default value

test/yargs-parser.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,6 +3053,18 @@ describe('yargs-parser', function () {
30533053
knownArg: false
30543054
})
30553055
})
3056+
it('should ignore negated known options when boolean-negation:false', function () {
3057+
const argv = parser('--no-known-arg --no-unknown-arg', {
3058+
boolean: ['known-arg'],
3059+
configuration: {
3060+
'unknown-options-as-args': true,
3061+
'boolean-negation': false
3062+
}
3063+
})
3064+
argv.should.deep.equal({
3065+
_: ['--no-known-arg', '--no-unknown-arg']
3066+
})
3067+
})
30563068
it('should ignore unknown options in long format separated by space', function () {
30573069
const argv = parser('--known-arg a --unknown-arg b', {
30583070
string: ['known-arg'],

0 commit comments

Comments
 (0)