-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigurator.js
80 lines (63 loc) · 1.77 KB
/
configurator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const program = require("commander");
const Log = require("./logger");
class GlobalConf {
knownTyp = {
path: 'path',
bool: 'bool',
int: 'integer',
float: 'float',
string: 'string',
name: v => v,
bounds: (m, M, typ = 'integer') => `[${m}; ${M}] as ${typ}`
}
conf = {}
addParams(name, descr = "New Param", defaultValue, shortName = '', typ = name) {
this.conf[name] = { defaultValue, descr, typ, name, shortName, definedValue: defaultValue };
}
mainName = 'main';
program = undefined;
constructor(mainName, mainDescr, mainDefaultValue, mainShortName, mainTyp) {
this.mainName = mainName;
this.conf[mainName] = {
name: mainName,
defaultValue: mainDefaultValue,
descr: mainDescr,
typ: mainTyp,
shortName: mainShortName,
definedValue: mainDefaultValue
};
this.program = program;
}
run() {
const name = this.mainName;
program
.arguments(`<${this.conf[name].typ}>`);
const values = Object.values(this.conf);
const keys = Object.keys(this.conf);
const conf = {};
for (const e of values) {
if (e.name !== name)
program.option(`-${e.shortName}, --${e.name} <${e.typ}>`, '' + e.descr);
}
program.action(function (main) {
// if (!!conf) {
for (const e of keys) {
if (program[e] !== undefined) {
conf[e] = program[e];
}
}
conf[name] = main;
// console.log(conf);
// this.conf = conf;
// }
})
.parse(process.argv);
for (const e of Object.keys(conf)) {
this.conf[e].definedValue = conf[e];
}
Log.title("USED CONFIGURATION");
Log.log(Object.values(this.conf).map(e => ({ [e.name]: e.definedValue })));
Log.log("");
}
}
module.exports = GlobalConf