@@ -25,7 +25,9 @@ function nopt (args, {
25
25
types,
26
26
shorthands,
27
27
typeDefs,
28
-
invalidHandler,
28
+
invalidHandler, // opt is configured but its value does not validate against given type
29
+
unknownHandler, // opt is not configured
30
+
abbrevHandler, // opt is being expanded via abbrev
29
31
typeDefault,
30
32
dynamicTypes,
31
33
} = {}) {
@@ -38,7 +40,9 @@ function nopt (args, {
38
40
original: args.slice(0),
39
41
}
40
42
41
-
parse(args, data, argv.remain, { typeDefs, types, dynamicTypes, shorthands })
43
+
parse(args, data, argv.remain, {
44
+
typeDefs, types, dynamicTypes, shorthands, unknownHandler, abbrevHandler,
45
+
})
42
46
43
47
// now data is full
44
48
clean(data, { types, dynamicTypes, typeDefs, invalidHandler, typeDefault })
@@ -247,6 +251,8 @@ function parse (args, data, remain, {
247
251
typeDefs = {},
248
252
shorthands = {},
249
253
dynamicTypes,
254
+
unknownHandler,
255
+
abbrevHandler,
250
256
} = {}) {
251
257
const StringType = typeDefs.String?.type
252
258
const NumberType = typeDefs.Number?.type
@@ -282,7 +288,7 @@ function parse (args, data, remain, {
282
288
283
289
// see if it's a shorthand
284
290
// if so, splice and back up to re-parse it.
285
-
const shRes = resolveShort(arg, shortAbbr, abbrevs, { shorthands })
291
+
const shRes = resolveShort(arg, shortAbbr, abbrevs, { shorthands, abbrevHandler })
286
292
debug('arg=%j shRes=%j', arg, shRes)
287
293
if (shRes) {
288
294
args.splice.apply(args, [i, 1].concat(shRes))
@@ -298,7 +304,13 @@ function parse (args, data, remain, {
298
304
arg = arg.slice(3)
299
305
}
300
306
301
-
if (abbrevs[arg]) {
307
+
// abbrev includes the original full string in its abbrev list
308
+
if (abbrevs[arg] && abbrevs[arg] !== arg) {
309
+
if (abbrevHandler) {
310
+
abbrevHandler(arg, abbrevs[arg])
311
+
} else if (abbrevHandler !== false) {
312
+
debug(`abbrev: ${arg} -> ${abbrevs[arg]}`)
313
+
}
302
314
arg = abbrevs[arg]
303
315
}
304
316
@@ -331,6 +343,23 @@ function parse (args, data, remain, {
331
343
(argType === null ||
332
344
isTypeArray && ~argType.indexOf(null)))
333
345
346
+
if (typeof argType === 'undefined') {
347
+
// la is going to unexpectedly be parsed outside the context of this arg
348
+
const hangingLa = !hadEq && la && !la?.startsWith('-') && !['true', 'false'].includes(la)
349
+
if (unknownHandler) {
350
+
if (hangingLa) {
351
+
unknownHandler(arg, la)
352
+
} else {
353
+
unknownHandler(arg)
354
+
}
355
+
} else if (unknownHandler !== false) {
356
+
debug(`unknown: ${arg}`)
357
+
if (hangingLa) {
358
+
debug(`unknown: ${la} parsed as normal opt`)
359
+
}
360
+
}
361
+
}
362
+
334
363
if (isBool) {
335
364
// just set and move along
336
365
val = !no
@@ -420,7 +449,7 @@ const singleCharacters = (arg, shorthands) => {
420
449
}
421
450
422
451
function resolveShort (arg, ...rest) {
423
-
const { types = {}, shorthands = {} } = rest.length ? rest.pop() : {}
452
+
const { abbrevHandler, types = {}, shorthands = {} } = rest.length ? rest.pop() : {}
424
453
const shortAbbr = rest[0] ?? abbrev(Object.keys(shorthands))
425
454
const abbrevs = rest[1] ?? abbrev(Object.keys(types))
426
455
@@ -457,7 +486,13 @@ function resolveShort (arg, ...rest) {
457
486
}
458
487
459
488
// if it's an abbr for a shorthand, then use that
489
+
// exact match has already happened so we don't need to account for that here
460
490
if (shortAbbr[arg]) {
491
+
if (abbrevHandler) {
492
+
abbrevHandler(arg, shortAbbr[arg])
493
+
} else if (abbrevHandler !== false) {
494
+
debug(`abbrev: ${arg} -> ${shortAbbr[arg]}`)
495
+
}
461
496
arg = shortAbbr[arg]
462
497
}
463
498
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4