UNPKG

too

Version:

Combine multiple processes' Stdout/Stderr/SIGINT to keep them all foreground

50 lines (49 loc) 1.3 kB
export class Args { specs; raw = []; constructor(specs) { this.specs = specs; } parse(argv) { this.raw = argv.slice(2); const all = this.raw.reduce((ctx, v) => { if (/=/.test(v)) { return ctx.concat(v.split("=")); } return ctx.concat([v]); }, []); const rest = []; while (all.length > 0) { const e = all[0]; const spec = this.findApplicableSpec(e); if (spec) { spec.add(spec.value, e, all[0 + 1]); all.splice(0, 2); } else { rest.push(e); all.splice(0, 1); } } return rest; } findApplicableSpec(key) { return this.specs.filter((spec) => { return (spec.flags && spec.flags.indexOf(key) >= 0); })[0]; } getSpecByName(key) { return this.specs.filter((spec) => { return (spec.name === key); })[0]; } get(key) { const spec = this.getSpecByName(key); return spec ? spec.value : []; } add(key, value) { const spec = this.getSpecByName(key); if (spec) spec.add(spec.value, spec.name, value); } }