+40
-5
@@ -28,3 +28,5 @@ const abbrev = require('abbrev') | ||
| typeDefs, | ||
| invalidHandler, | ||
| invalidHandler, // opt is configured but its value does not validate against given type | ||
| unknownHandler, // opt is not configured | ||
| abbrevHandler, // opt is being expanded via abbrev | ||
| typeDefault, | ||
@@ -42,3 +44,5 @@ dynamicTypes, | ||
| parse(args, data, argv.remain, { typeDefs, types, dynamicTypes, shorthands }) | ||
| parse(args, data, argv.remain, { | ||
| typeDefs, types, dynamicTypes, shorthands, unknownHandler, abbrevHandler, | ||
| }) | ||
@@ -252,2 +256,4 @@ // now data is full | ||
| dynamicTypes, | ||
| unknownHandler, | ||
| abbrevHandler, | ||
| } = {}) { | ||
@@ -288,3 +294,3 @@ const StringType = typeDefs.String?.type | ||
| // if so, splice and back up to re-parse it. | ||
| const shRes = resolveShort(arg, shortAbbr, abbrevs, { shorthands }) | ||
| const shRes = resolveShort(arg, shortAbbr, abbrevs, { shorthands, abbrevHandler }) | ||
| debug('arg=%j shRes=%j', arg, shRes) | ||
@@ -305,3 +311,9 @@ if (shRes) { | ||
| if (abbrevs[arg]) { | ||
| // abbrev includes the original full string in its abbrev list | ||
| if (abbrevs[arg] && abbrevs[arg] !== arg) { | ||
| if (abbrevHandler) { | ||
| abbrevHandler(arg, abbrevs[arg]) | ||
| } else if (abbrevHandler !== false) { | ||
| debug(`abbrev: ${arg} -> ${abbrevs[arg]}`) | ||
| } | ||
| arg = abbrevs[arg] | ||
@@ -339,2 +351,19 @@ } | ||
| if (typeof argType === 'undefined') { | ||
| // la is going to unexpectedly be parsed outside the context of this arg | ||
| const hangingLa = !hadEq && la && !la?.startsWith('-') && !['true', 'false'].includes(la) | ||
| if (unknownHandler) { | ||
| if (hangingLa) { | ||
| unknownHandler(arg, la) | ||
| } else { | ||
| unknownHandler(arg) | ||
| } | ||
| } else if (unknownHandler !== false) { | ||
| debug(`unknown: ${arg}`) | ||
| if (hangingLa) { | ||
| debug(`unknown: ${la} parsed as normal opt`) | ||
| } | ||
| } | ||
| } | ||
| if (isBool) { | ||
@@ -429,3 +458,3 @@ // just set and move along | ||
| function resolveShort (arg, ...rest) { | ||
| const { types = {}, shorthands = {} } = rest.length ? rest.pop() : {} | ||
| const { abbrevHandler, types = {}, shorthands = {} } = rest.length ? rest.pop() : {} | ||
| const shortAbbr = rest[0] ?? abbrev(Object.keys(shorthands)) | ||
@@ -467,3 +496,9 @@ const abbrevs = rest[1] ?? abbrev(Object.keys(types)) | ||
| // if it's an abbr for a shorthand, then use that | ||
| // exact match has already happened so we don't need to account for that here | ||
| if (shortAbbr[arg]) { | ||
| if (abbrevHandler) { | ||
| abbrevHandler(arg, shortAbbr[arg]) | ||
| } else if (abbrevHandler !== false) { | ||
| debug(`abbrev: ${arg} -> ${shortAbbr[arg]}`) | ||
| } | ||
| arg = shortAbbr[arg] | ||
@@ -470,0 +505,0 @@ } |
+4
-0
@@ -21,2 +21,4 @@ const lib = require('./nopt-lib') | ||
| invalidHandler: exports.invalidHandler, | ||
| unknownHandler: exports.unknownHandler, | ||
| abbrevHandler: exports.abbrevHandler, | ||
| }) | ||
@@ -30,3 +32,5 @@ } | ||
| invalidHandler: exports.invalidHandler, | ||
| unknownHandler: exports.unknownHandler, | ||
| abbrevHandler: exports.abbrevHandler, | ||
| }) | ||
| } |
+4
-4
| { | ||
| "name": "nopt", | ||
| "version": "8.0.0", | ||
| "version": "8.1.0", | ||
| "description": "Option parsing for Node, supporting types, shorthands, etc. Used by npm.", | ||
@@ -26,7 +26,7 @@ "author": "GitHub Inc.", | ||
| "dependencies": { | ||
| "abbrev": "^2.0.0" | ||
| "abbrev": "^3.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@npmcli/eslint-config": "^5.0.0", | ||
| "@npmcli/template-oss": "4.23.3", | ||
| "@npmcli/template-oss": "4.23.6", | ||
| "tap": "^16.3.0" | ||
@@ -50,5 +50,5 @@ }, | ||
| "windowsCI": false, | ||
| "version": "4.23.3", | ||
| "version": "4.23.6", | ||
| "publish": true | ||
| } | ||
| } |
+8
-7
@@ -144,11 +144,12 @@ If you want to write an option parser, and have it be good, there are | ||
| By default, nopt outputs a warning to standard error when invalid values for | ||
| known options are found. You can change this behavior by assigning a method | ||
| to `nopt.invalidHandler`. This method will be called with | ||
| the offending `nopt.invalidHandler(key, val, types)`. | ||
| By default nopt logs debug messages if `DEBUG_NOPT` or `NOPT_DEBUG` are set in the environment. | ||
| If no `nopt.invalidHandler` is assigned, then it will console.error | ||
| its whining. If it is assigned to boolean `false` then the warning is | ||
| suppressed. | ||
| You can assign the following methods to `nopt` for a more granular notification of invalid, unknown, and expanding options: | ||
| `nopt.invalidHandler(key, value, type, data)` - Called when a value is invalid for its option. | ||
| `nopt.unknownHandler(key, next)` - Called when an option is found that has no configuration. In certain situations the next option on the command line will be parsed on its own instead of as part of the unknown option. In this case `next` will contain that option. | ||
| `nopt.abbrevHandler(short, long)` - Called when an option is automatically translated via abbreviations. | ||
| You can also set any of these to `false` to disable the debugging messages that they generate. | ||
| ## Abbreviations | ||
@@ -155,0 +156,0 @@ |
28128
7.26%600
6.76%215
0.47%+ Added
- Removed
Updated