UNPKG

3.2 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.OptionParser = void 0;
4const debug = require("debug");
5const ymir_1 = require("@fimbul/ymir");
6const log = debug('wotan:optparse');
7var OptionParser;
8(function (OptionParser) {
9 function parse(options, specs, config) {
10 const result = {};
11 let name;
12 for (name of Object.keys(specs))
13 result[name] = specs[name](options && options[name], reportMismatch);
14 if (config.exhaustive && options)
15 for (const key of Object.keys(options))
16 if (specs[key] === undefined)
17 report(`Unexpected option '${key}'.`);
18 return result;
19 function reportMismatch(type) {
20 report(`Expected a value of type '${type}' for option '${name}'.`);
21 }
22 function report(message) {
23 message = config.context + ': ' + message;
24 if (config.validate)
25 throw new ymir_1.ConfigurationError(message);
26 log(message);
27 }
28 }
29 OptionParser.parse = parse;
30 let Transform;
31 (function (Transform) {
32 function withDefault(parseFn, defaultValue) {
33 return (value, report) => {
34 const result = parseFn(value, report);
35 return result === undefined ? defaultValue : result;
36 };
37 }
38 Transform.withDefault = withDefault;
39 function noDefault(parseFn) {
40 return (value, report) => {
41 return value === undefined ? undefined : parseFn(value, report);
42 };
43 }
44 Transform.noDefault = noDefault;
45 function map(parseFn, cb) {
46 return (value, report) => { var _a; return (_a = parseFn(value, report)) === null || _a === void 0 ? void 0 : _a.map(cb); };
47 }
48 Transform.map = map;
49 function transform(parseFn, cb) {
50 return (value, report) => cb(parseFn(value, report));
51 }
52 Transform.transform = transform;
53 })(Transform = OptionParser.Transform || (OptionParser.Transform = {}));
54 let Factory;
55 (function (Factory) {
56 function parsePrimitive(...types) {
57 return (value, report) => {
58 for (const type of types)
59 if (typeof value === type)
60 return value;
61 if (value !== undefined)
62 report(types.join(' | '));
63 return;
64 };
65 }
66 Factory.parsePrimitive = parsePrimitive;
67 function parsePrimitiveOrArray(type) {
68 return (value, report) => {
69 if (Array.isArray(value) && value.every((v) => typeof v === type))
70 return value;
71 if (typeof value === 'string')
72 return [value];
73 if (value !== undefined)
74 report('string | string[]');
75 return;
76 };
77 }
78 Factory.parsePrimitiveOrArray = parsePrimitiveOrArray;
79 })(Factory = OptionParser.Factory || (OptionParser.Factory = {}));
80})(OptionParser = exports.OptionParser || (exports.OptionParser = {}));
81//# sourceMappingURL=optparse.js.map
\No newline at end of file