Skip to content

Commit f85e0c8

Browse files
committed
fix: unknown-options-as-args should treat negated boolean as unknown when boolean-negation:false
1 parent 214983c commit f85e0c8

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
@@ -981,7 +981,10 @@ export class YargsParser {
981981
// e.g. '-a' or '--arg'
982982
const normalFlag = /^-+([^=]+?)$/
983983
// check the different types of flag styles, including negatedBoolean, a pattern defined near the start of the parse method
984-
return !hasFlagsMatching(arg, flagWithEquals, negatedBoolean, normalFlag)
984+
const patterns = [flagWithEquals, normalFlag];
985+
if (configuration['boolean-negation'])
986+
patterns.push(negatedBoolean);
987+
return !hasFlagsMatching(arg, ...patterns)
985988
}
986989

987990
// 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
@@ -3071,6 +3071,18 @@ describe('yargs-parser', function () {
30713071
knownArg: false
30723072
})
30733073
})
3074+
it('should ignore negated known options when boolean-negation:false', function () {
3075+
const argv = parser('--no-known-arg --no-unknown-arg', {
3076+
boolean: ['known-arg'],
3077+
configuration: {
3078+
'unknown-options-as-args': true,
3079+
'boolean-negation': false
3080+
}
3081+
})
3082+
argv.should.deep.equal({
3083+
_: ['--no-known-arg', '--no-unknown-arg']
3084+
})
3085+
})
30743086
it('should ignore unknown options in long format separated by space', function () {
30753087
const argv = parser('--known-arg a --unknown-arg b', {
30763088
string: ['known-arg'],

0 commit comments

Comments
 (0)