@@ -94,14 +94,24 @@ To specify an option argument starting with a dash use ${example}.`;
94
94
* @param {object } token - from tokens as available from parseArgs
95
95
*/
96
96
function checkOptionUsage ( config , token ) {
97
- if ( ! ObjectHasOwn ( config . options , token . name ) ) {
98
- throw new ERR_PARSE_ARGS_UNKNOWN_OPTION (
99
- token . rawName , config . allowPositionals ) ;
97
+ let tokenName = token . name ;
98
+ if ( ! ObjectHasOwn ( config . options , tokenName ) ) {
99
+ // Check for negated boolean option.
100
+ if ( config . allowNegative && StringPrototypeStartsWith ( tokenName , 'no-' ) ) {
101
+ tokenName = StringPrototypeSlice ( tokenName , 3 ) ;
102
+ if ( ! ObjectHasOwn ( config . options , tokenName ) || optionsGetOwn ( config . options , tokenName , 'type' ) !== 'boolean' ) {
103
+ throw new ERR_PARSE_ARGS_UNKNOWN_OPTION (
104
+ token . rawName , config . allowPositionals ) ;
105
+ }
106
+ } else {
107
+ throw new ERR_PARSE_ARGS_UNKNOWN_OPTION (
108
+ token . rawName , config . allowPositionals ) ;
109
+ }
100
110
}
101
111
102
- const short = optionsGetOwn ( config . options , token . name , 'short' ) ;
103
- const shortAndLong = `${ short ? `-${ short } , ` : '' } --${ token . name } ` ;
104
- const type = optionsGetOwn ( config . options , token . name , 'type' ) ;
112
+ const short = optionsGetOwn ( config . options , tokenName , 'short' ) ;
113
+ const shortAndLong = `${ short ? `-${ short } , ` : '' } --${ tokenName } ` ;
114
+ const type = optionsGetOwn ( config . options , tokenName , 'type' ) ;
105
115
if ( type === 'string' && typeof token . value !== 'string' ) {
106
116
throw new ERR_PARSE_ARGS_INVALID_OPTION_VALUE ( `Option '${ shortAndLong } <value>' argument missing` ) ;
107
117
}
@@ -118,12 +128,22 @@ function checkOptionUsage(config, token) {
118
128
* @param {string|undefined } optionValue - value from user args
119
129
* @param {object } options - option configs, from parseArgs({ options })
120
130
* @param {object } values - option values returned in `values` by parseArgs
131
+ * @param {boolean } allowNegative - allow negative optinons if true
121
132
*/
122
- function storeOption ( longOption , optionValue , options , values ) {
133
+ function storeOption ( longOption , optionValue , options , values , allowNegative ) {
123
134
if ( longOption === '__proto__' ) {
124
135
return ; // No. Just no.
125
136
}
126
137
138
+ if ( allowNegative && StringPrototypeStartsWith ( longOption , 'no-' ) ) {
139
+ // Boolean option negation: --no-foo
140
+ const longOptionWithoutPrefixNo = StringPrototypeSlice ( longOption , 3 ) ;
141
+ if ( optionsGetOwn ( options , longOptionWithoutPrefixNo , 'type' ) !== 'string' ) {
142
+ longOption = StringPrototypeSlice ( longOption , 3 ) ;
143
+ optionValue = false ;
144
+ }
145
+ }
146
+
127
147
// We store based on the option value rather than option type,
128
148
// preserving the users intent for author to deal with.
129
149
const newValue = optionValue ?? true ;
@@ -290,15 +310,17 @@ const parseArgs = (config = kEmptyObject) => {
290
310
const strict = objectGetOwn ( config , 'strict' ) ?? true ;
291
311
const allowPositionals = objectGetOwn ( config , 'allowPositionals' ) ?? ! strict ;
292
312
const returnTokens = objectGetOwn ( config , 'tokens' ) ?? false ;
313
+ const allowNegative = objectGetOwn ( config , 'allowNegative' ) ?? false ;
293
314
const options = objectGetOwn ( config , 'options' ) ?? { __proto__ : null } ;
294
315
// Bundle these up for passing to strict-mode checks.
295
- const parseConfig = { args, strict, options, allowPositionals } ;
316
+ const parseConfig = { args, strict, options, allowPositionals, allowNegative } ;
296
317
297
318
// Validate input configuration.
298
319
validateArray ( args , 'args' ) ;
299
320
validateBoolean ( strict , 'strict' ) ;
300
321
validateBoolean ( allowPositionals , 'allowPositionals' ) ;
301
322
validateBoolean ( returnTokens , 'tokens' ) ;
323
+ validateBoolean ( allowNegative , 'allowNegative' ) ;
302
324
validateObject ( options , 'options' ) ;
303
325
ArrayPrototypeForEach (
304
326
ObjectEntries ( options ) ,
@@ -360,7 +382,7 @@ const parseArgs = (config = kEmptyObject) => {
360
382
checkOptionUsage ( parseConfig , token ) ;
361
383
checkOptionLikeValue ( token ) ;
362
384
}
363
- storeOption ( token . name , token . value , options , result . values ) ;
385
+ storeOption ( token . name , token . value , options , result . values , parseConfig . allowNegative ) ;
364
386
} else if ( token . kind === 'positional' ) {
365
387
if ( ! allowPositionals ) {
366
388
throw new ERR_PARSE_ARGS_UNEXPECTED_POSITIONAL ( token . value ) ;
0 commit comments