pparams
Version:
Program Parameters - lib to manage command line parameters
111 lines (110 loc) • 3.62 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class PParams {
constructor(tabargs) {
this._hash = {};
this._tab = [];
this._nbVals = 0;
let cur;
let val;
if (typeof tabargs === 'string')
tabargs = tabargs.split(' ');
let tab = [];
if (tabargs)
for (cur of tabargs) {
val = cur.trim();
if (val && val !== '-')
tab.push(val);
}
for (let i = 0; i < tab.length; i++) {
cur = tab[i];
if (cur.startsWith('-')) {
cur = cur.substr(1);
if ((i + 1) < tab.length && !tab[i + 1].startsWith('-')) {
i++;
val = tab[i];
}
else
val = 'true';
this._hash[cur] = val;
this._tab.push({ name: cur, value: val });
}
else {
this._hash[this._nbVals] = cur;
this._nbVals++;
this._tab.push(cur);
}
}
}
size() { return (this._tab.length); }
type(pos) {
let val = this._tab[pos];
if (val === undefined)
return (undefined);
else if (typeof val === 'string')
return ('value');
else
return ('field');
}
get(pos) {
return (this._tab[pos]);
}
nbValue() { return (this._nbVals); }
intVal(name, def_val, dec) {
let val = this.val(name, undefined, dec);
if (val === undefined)
return (def_val);
return (Number(val));
}
val(name, def_val = undefined, dec = 0) {
if (dec == 0) {
let val = this._hash[name];
return (val !== undefined ? val : def_val);
}
else {
if (typeof name === 'string') {
for (let i = 0; i < this._tab.length; i++) {
let cur = this._tab[i];
if (typeof cur !== 'string') {
if (cur.name == name) {
let searched;
while (dec > 0) {
i++;
dec--;
searched = this._tab[i];
if (typeof searched !== 'string')
return (def_val);
}
return (searched);
}
}
}
return (def_val);
}
else {
let pos = 0;
for (let i = 0; i < this._tab.length; i++) {
let cur = this._tab[i];
if (typeof cur === 'string') {
if (name == pos) {
let searched;
while (dec > 0) {
i++;
dec--;
searched = this._tab[i];
if (typeof searched !== 'string')
return (def_val);
}
return (searched);
}
else
pos++;
}
}
return (def_val);
}
}
}
exist(name) { return (this._hash[name] !== undefined); }
}
exports.default = PParams;