| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | // info about each config option. |
| 2 | |
| 3 | var debug = process.env.DEBUG_NOPT || process.env.NOPT_DEBUG |
| 4 | ? function () { console.error.apply(console, arguments) } |
| 5 | : function () {} |
| 6 | |
| 7 | var url = require("url") |
| 8 | , path = require("path") |
| 9 | , Stream = require("stream").Stream |
| 10 | , abbrev = require("abbrev") |
| 11 | |
| 12 | module.exports = exports = nopt |
| 13 | exports.clean = clean |
| 14 | |
| 15 | exports.typeDefs = |
| 16 | { String : { type: String, validate: validateString } |
| 17 | , Boolean : { type: Boolean, validate: validateBoolean } |
| 18 | , url : { type: url, validate: validateUrl } |
| 19 | , Number : { type: Number, validate: validateNumber } |
| 20 | , path : { type: path, validate: validatePath } |
| 21 | , Stream : { type: Stream, validate: validateStream } |
| 22 | , Date : { type: Date, validate: validateDate } |
| 23 | } |
| 24 | |
| 25 | function nopt (types, shorthands, args, slice) { |
| 26 | args = args || process.argv |
| 27 | types = types || {} |
| 28 | shorthands = shorthands || {} |
| 29 | if (typeof slice !== "number") slice = 2 |
| 30 | |
| 31 | debug(types, shorthands, args, slice) |
| 32 | |
| 33 | args = args.slice(slice) |
| 34 | var data = {} |
| 35 | , key |
| 36 | , remain = [] |
| 37 | , cooked = args |
| 38 | , original = args.slice(0) |
| 39 | |
| 40 | parse(args, data, remain, types, shorthands) |
| 41 | // now data is full |
| 42 | clean(data, types, exports.typeDefs) |
| 43 | data.argv = {remain:remain,cooked:cooked,original:original} |
| 44 | Object.defineProperty(data.argv, 'toString', { value: function () { |
| 45 | return this.original.map(JSON.stringify).join(" ") |
| 46 | }, enumerable: false }) |
| 47 | return data |
| 48 | } |
| 49 | |
| 50 | function clean (data, types, typeDefs) { |
| 51 | typeDefs = typeDefs || exports.typeDefs |
| 52 | var remove = {} |
| 53 | , typeDefault = [false, true, null, String, Array] |
| 54 | |
| 55 | Object.keys(data).forEach(function (k) { |
| 56 | if (k === "argv") return |
| 57 | var val = data[k] |
| 58 | , isArray = Array.isArray(val) |
| 59 | , type = types[k] |
| 60 | if (!isArray) val = [val] |
| 61 | if (!type) type = typeDefault |
| 62 | if (type === Array) type = typeDefault.concat(Array) |
| 63 | if (!Array.isArray(type)) type = [type] |
| 64 | |
| 65 | debug("val=%j", val) |
| 66 | debug("types=", type) |
| 67 | val = val.map(function (val) { |
| 68 | // if it's an unknown value, then parse false/true/null/numbers/dates |
| 69 | if (typeof val === "string") { |
| 70 | debug("string %j", val) |
| 71 | val = val.trim() |
| 72 | if ((val === "null" && ~type.indexOf(null)) |
| 73 | || (val === "true" && |
| 74 | (~type.indexOf(true) || ~type.indexOf(Boolean))) |
| 75 | || (val === "false" && |
| 76 | (~type.indexOf(false) || ~type.indexOf(Boolean)))) { |
| 77 | val = JSON.parse(val) |
| 78 | debug("jsonable %j", val) |
| 79 | } else if (~type.indexOf(Number) && !isNaN(val)) { |
| 80 | debug("convert to number", val) |
| 81 | val = +val |
| 82 | } else if (~type.indexOf(Date) && !isNaN(Date.parse(val))) { |
| 83 | debug("convert to date", val) |
| 84 | val = new Date(val) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (!types.hasOwnProperty(k)) { |
| 89 | return val |
| 90 | } |
| 91 | |
| 92 | // allow `--no-blah` to set 'blah' to null if null is allowed |
| 93 | if (val === false && ~type.indexOf(null) && |
| 94 | !(~type.indexOf(false) || ~type.indexOf(Boolean))) { |
| 95 | val = null |
| 96 | } |
| 97 | |
| 98 | var d = {} |
| 99 | d[k] = val |
| 100 | debug("prevalidated val", d, val, types[k]) |
| 101 | if (!validate(d, k, val, types[k], typeDefs)) { |
| 102 | if (exports.invalidHandler) { |
| 103 | exports.invalidHandler(k, val, types[k], data) |
| 104 | } else if (exports.invalidHandler !== false) { |
| 105 | debug("invalid: "+k+"="+val, types[k]) |
| 106 | } |
| 107 | return remove |
| 108 | } |
| 109 | debug("validated val", d, val, types[k]) |
| 110 | return d[k] |
| 111 | }).filter(function (val) { return val !== remove }) |
| 112 | |
| 113 | if (!val.length) delete data[k] |
| 114 | else if (isArray) { |
| 115 | debug(isArray, data[k], val) |
| 116 | data[k] = val |
| 117 | } else data[k] = val[0] |
| 118 | |
| 119 | debug("k=%s val=%j", k, val, data[k]) |
| 120 | }) |
| 121 | } |
| 122 | |
| 123 | function validateString (data, k, val) { |
| 124 | data[k] = String(val) |
| 125 | } |
| 126 | |
| 127 | function validatePath (data, k, val) { |
| 128 | if (val === true) return false |
| 129 | if (val === null) return true |
| 130 | |
| 131 | val = String(val) |
| 132 | var homePattern = process.platform === 'win32' ? /^~(\/|\\)/ : /^~\// |
| 133 | if (val.match(homePattern) && process.env.HOME) { |
| 134 | val = path.resolve(process.env.HOME, val.substr(2)) |
| 135 | } |
| 136 | data[k] = path.resolve(String(val)) |
| 137 | return true |
| 138 | } |
| 139 | |
| 140 | function validateNumber (data, k, val) { |
| 141 | debug("validate Number %j %j %j", k, val, isNaN(val)) |
| 142 | if (isNaN(val)) return false |
| 143 | data[k] = +val |
| 144 | } |
| 145 | |
| 146 | function validateDate (data, k, val) { |
| 147 | debug("validate Date %j %j %j", k, val, Date.parse(val)) |
| 148 | var s = Date.parse(val) |
| 149 | if (isNaN(s)) return false |
| 150 | data[k] = new Date(val) |
| 151 | } |
| 152 | |
| 153 | function validateBoolean (data, k, val) { |
| 154 | if (val instanceof Boolean) val = val.valueOf() |
| 155 | else if (typeof val === "string") { |
| 156 | if (!isNaN(val)) val = !!(+val) |
| 157 | else if (val === "null" || val === "false") val = false |
| 158 | else val = true |
| 159 | } else val = !!val |
| 160 | data[k] = val |
| 161 | } |
| 162 | |
| 163 | function validateUrl (data, k, val) { |
| 164 | val = url.parse(String(val)) |
| 165 | if (!val.host) return false |
| 166 | data[k] = val.href |
| 167 | } |
| 168 | |
| 169 | function validateStream (data, k, val) { |
| 170 | if (!(val instanceof Stream)) return false |
| 171 | data[k] = val |
| 172 | } |
| 173 | |
| 174 | function validate (data, k, val, type, typeDefs) { |
| 175 | // arrays are lists of types. |
| 176 | if (Array.isArray(type)) { |
| 177 | for (var i = 0, l = type.length; i < l; i ++) { |
| 178 | if (type[i] === Array) continue |
| 179 | if (validate(data, k, val, type[i], typeDefs)) return true |
| 180 | } |
| 181 | delete data[k] |
| 182 | return false |
| 183 | } |
| 184 | |
| 185 | // an array of anything? |
| 186 | if (type === Array) return true |
| 187 | |
| 188 | // NaN is poisonous. Means that something is not allowed. |
| 189 | if (type !== type) { |
| 190 | debug("Poison NaN", k, val, type) |
| 191 | delete data[k] |
| 192 | return false |
| 193 | } |
| 194 | |
| 195 | // explicit list of values |
| 196 | if (val === type) { |
| 197 | debug("Explicitly allowed %j", val) |
| 198 | // if (isArray) (data[k] = data[k] || []).push(val) |
| 199 | // else data[k] = val |
| 200 | data[k] = val |
| 201 | return true |
| 202 | } |
| 203 | |
| 204 | // now go through the list of typeDefs, validate against each one. |
| 205 | var ok = false |
| 206 | , types = Object.keys(typeDefs) |
| 207 | for (var i = 0, l = types.length; i < l; i ++) { |
| 208 | debug("test type %j %j %j", k, val, types[i]) |
| 209 | var t = typeDefs[types[i]] |
| 210 | if (t && |
| 211 | ((type && type.name && t.type && t.type.name) ? (type.name === t.type.name) : (type === t.type))) { |
| 212 | var d = {} |
| 213 | ok = false !== t.validate(d, k, val) |
| 214 | val = d[k] |
| 215 | if (ok) { |
| 216 | // if (isArray) (data[k] = data[k] || []).push(val) |
| 217 | // else data[k] = val |
| 218 | data[k] = val |
| 219 | break |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | debug("OK? %j (%j %j %j)", ok, k, val, types[i]) |
| 224 | |
| 225 | if (!ok) delete data[k] |
| 226 | return ok |
| 227 | } |
| 228 | |
| 229 | function parse (args, data, remain, types, shorthands) { |
| 230 | debug("parse", args, data, remain) |
| 231 | |
| 232 | var key = null |
| 233 | , abbrevs = abbrev(Object.keys(types)) |
| 234 | , shortAbbr = abbrev(Object.keys(shorthands)) |
| 235 | |
| 236 | for (var i = 0; i < args.length; i ++) { |
| 237 | var arg = args[i] |
| 238 | debug("arg", arg) |
| 239 | |
| 240 | if (arg.match(/^-{2,}$/)) { |
| 241 | // done with keys. |
| 242 | // the rest are args. |
| 243 | remain.push.apply(remain, args.slice(i + 1)) |
| 244 | args[i] = "--" |
| 245 | break |
| 246 | } |
| 247 | var hadEq = false |
| 248 | if (arg.charAt(0) === "-" && arg.length > 1) { |
| 249 | if (arg.indexOf("=") !== -1) { |
| 250 | hadEq = true |
| 251 | var v = arg.split("=") |
| 252 | arg = v.shift() |
| 253 | v = v.join("=") |
| 254 | args.splice.apply(args, [i, 1].concat([arg, v])) |
| 255 | } |
| 256 | |
| 257 | // see if it's a shorthand |
| 258 | // if so, splice and back up to re-parse it. |
| 259 | var shRes = resolveShort(arg, shorthands, shortAbbr, abbrevs) |
| 260 | debug("arg=%j shRes=%j", arg, shRes) |
| 261 | if (shRes) { |
| 262 | debug(arg, shRes) |
| 263 | args.splice.apply(args, [i, 1].concat(shRes)) |
| 264 | if (arg !== shRes[0]) { |
| 265 | i -- |
| 266 | continue |
| 267 | } |
| 268 | } |
| 269 | arg = arg.replace(/^-+/, "") |
| 270 | var no = null |
| 271 | while (arg.toLowerCase().indexOf("no-") === 0) { |
| 272 | no = !no |
| 273 | arg = arg.substr(3) |
| 274 | } |
| 275 | |
| 276 | if (abbrevs[arg]) arg = abbrevs[arg] |
| 277 | |
| 278 | var isArray = types[arg] === Array || |
| 279 | Array.isArray(types[arg]) && types[arg].indexOf(Array) !== -1 |
| 280 | |
| 281 | // allow unknown things to be arrays if specified multiple times. |
| 282 | if (!types.hasOwnProperty(arg) && data.hasOwnProperty(arg)) { |
| 283 | if (!Array.isArray(data[arg])) |
| 284 | data[arg] = [data[arg]] |
| 285 | isArray = true |
| 286 | } |
| 287 | |
| 288 | var val |
| 289 | , la = args[i + 1] |
| 290 | |
| 291 | var isBool = typeof no === 'boolean' || |
| 292 | types[arg] === Boolean || |
| 293 | Array.isArray(types[arg]) && types[arg].indexOf(Boolean) !== -1 || |
| 294 | (typeof types[arg] === 'undefined' && !hadEq) || |
| 295 | (la === "false" && |
| 296 | (types[arg] === null || |
| 297 | Array.isArray(types[arg]) && ~types[arg].indexOf(null))) |
| 298 | |
| 299 | if (isBool) { |
| 300 | // just set and move along |
| 301 | val = !no |
| 302 | // however, also support --bool true or --bool false |
| 303 | if (la === "true" || la === "false") { |
| 304 | val = JSON.parse(la) |
| 305 | la = null |
| 306 | if (no) val = !val |
| 307 | i ++ |
| 308 | } |
| 309 | |
| 310 | // also support "foo":[Boolean, "bar"] and "--foo bar" |
| 311 | if (Array.isArray(types[arg]) && la) { |
| 312 | if (~types[arg].indexOf(la)) { |
| 313 | // an explicit type |
| 314 | val = la |
| 315 | i ++ |
| 316 | } else if ( la === "null" && ~types[arg].indexOf(null) ) { |
| 317 | // null allowed |
| 318 | val = null |
| 319 | i ++ |
| 320 | } else if ( !la.match(/^-{2,}[^-]/) && |
| 321 | !isNaN(la) && |
| 322 | ~types[arg].indexOf(Number) ) { |
| 323 | // number |
| 324 | val = +la |
| 325 | i ++ |
| 326 | } else if ( !la.match(/^-[^-]/) && ~types[arg].indexOf(String) ) { |
| 327 | // string |
| 328 | val = la |
| 329 | i ++ |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (isArray) (data[arg] = data[arg] || []).push(val) |
| 334 | else data[arg] = val |
| 335 | |
| 336 | continue |
| 337 | } |
| 338 | |
| 339 | if (types[arg] === String && la === undefined) |
| 340 | la = "" |
| 341 | |
| 342 | if (la && la.match(/^-{2,}$/)) { |
| 343 | la = undefined |
| 344 | i -- |
| 345 | } |
| 346 | |
| 347 | val = la === undefined ? true : la |
| 348 | if (isArray) (data[arg] = data[arg] || []).push(val) |
| 349 | else data[arg] = val |
| 350 | |
| 351 | i ++ |
| 352 | continue |
| 353 | } |
| 354 | remain.push(arg) |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | function resolveShort (arg, shorthands, shortAbbr, abbrevs) { |
| 359 | // handle single-char shorthands glommed together, like |
| 360 | // npm ls -glp, but only if there is one dash, and only if |
| 361 | // all of the chars are single-char shorthands, and it's |
| 362 | // not a match to some other abbrev. |
| 363 | arg = arg.replace(/^-+/, '') |
| 364 | |
| 365 | // if it's an exact known option, then don't go any further |
| 366 | if (abbrevs[arg] === arg) |
| 367 | return null |
| 368 | |
| 369 | // if it's an exact known shortopt, same deal |
| 370 | if (shorthands[arg]) { |
| 371 | // make it an array, if it's a list of words |
| 372 | if (shorthands[arg] && !Array.isArray(shorthands[arg])) |
| 373 | shorthands[arg] = shorthands[arg].split(/\s+/) |
| 374 | |
| 375 | return shorthands[arg] |
| 376 | } |
| 377 | |
| 378 | // first check to see if this arg is a set of single-char shorthands |
| 379 | var singles = shorthands.___singles |
| 380 | if (!singles) { |
| 381 | singles = Object.keys(shorthands).filter(function (s) { |
| 382 | return s.length === 1 |
| 383 | }).reduce(function (l,r) { |
| 384 | l[r] = true |
| 385 | return l |
| 386 | }, {}) |
| 387 | shorthands.___singles = singles |
| 388 | debug('shorthand singles', singles) |
| 389 | } |
| 390 | |
| 391 | var chrs = arg.split("").filter(function (c) { |
| 392 | return singles[c] |
| 393 | }) |
| 394 | |
| 395 | if (chrs.join("") === arg) return chrs.map(function (c) { |
| 396 | return shorthands[c] |
| 397 | }).reduce(function (l, r) { |
| 398 | return l.concat(r) |
| 399 | }, []) |
| 400 | |
| 401 | |
| 402 | // if it's an arg abbrev, and not a literal shorthand, then prefer the arg |
| 403 | if (abbrevs[arg] && !shorthands[arg]) |
| 404 | return null |
| 405 | |
| 406 | // if it's an abbr for a shorthand, then use that |
| 407 | if (shortAbbr[arg]) |
| 408 | arg = shortAbbr[arg] |
| 409 | |
| 410 | // make it an array, if it's a list of words |
| 411 | if (shorthands[arg] && !Array.isArray(shorthands[arg])) |
| 412 | shorthands[arg] = shorthands[arg].split(/\s+/) |
| 413 | |
| 414 | return shorthands[arg] |
| 415 | } |