UNPKG

@intuitionrobotics/ts-common

Version:
56 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CliParamsModule = void 0; const module_1 = require("../core/module"); const exceptions_1 = require("../core/exceptions"); const __1 = require(".."); class CliParamsModule_Class extends module_1.Module { constructor() { super("CliParamsModule"); this.paramsValue = {}; this.printHowTo = (params) => { const missingParams = params.filter((param) => !this.paramsValue[param.keyName] && !param.optional); const foundParams = params.filter((param) => this.paramsValue[param.keyName]); this.printFoundArgs("Found Args", foundParams, this.paramsValue); if (missingParams.length === 0) return; this.printFoundArgs("Missing Args", missingParams, this.paramsValue); throw new exceptions_1.ImplementationMissingException("Missing cli params"); }; this.getParams = () => { return this.paramsValue; }; } init() { this.config.params.forEach((param) => this.paramsValue[param.keyName] = this.getParam(param)); this.printHowTo(this.config.params); return this.paramsValue; } getParam(param, args = process.argv.slice(2, process.argv.length)) { if (!this.config.params.find(_param => _param.keyName === param.keyName)) throw new exceptions_1.BadImplementationException("Requested not existing param"); let value = this.extractParam(param, args); if (!value) value = param.defaultValue; if (!value) return value; return (param.process ? param.process(value) : value); } extractParam(param, argv) { if (param.isArray) return param.keys.reduce((values, key) => { values.push(...(0, __1.filterInstances)(argv.map(arg => { var _a; return (_a = arg.match(new RegExp(`${key}=(.*)`))) === null || _a === void 0 ? void 0 : _a[1]; }))); return values; }, []); const find = param.keys.map(key => argv.map(arg => { var _a; return (_a = arg.match(new RegExp(`${key}=(.*)`))) === null || _a === void 0 ? void 0 : _a[1]; })); return (0, __1.flatArray)(find).find(k => k); } printFoundArgs(title, params, foundArgs) { if (params.length) return; this.logInfoBold(` ${title}:`); params.forEach((param) => this.logInfo(` ${param.keys[0]}=${foundArgs[param.keyName] || `<${param.name}>`}`)); } } exports.CliParamsModule = new CliParamsModule_Class(); //# sourceMappingURL=CliParamsModule.js.map