UNPKG

kist

Version:

Lightweight Package Pipeline Processor with Plugin Architecture

56 lines 1.91 kB
import { AbstractProcess } from "../core/abstract/AbstractProcess.js"; import { OptionsValidator } from "../core/validation/OptionsValidator.js"; export class ArgumentParser extends AbstractProcess { args; validator; constructor() { super(); this.args = process.argv.slice(2); this.validator = new OptionsValidator(); this.logDebug("ArgumentParser initialized with arguments."); } getOption(key, options) { const flag = `--${key}`; const flagIndex = this.args.findIndex((arg) => arg === flag); const value = flagIndex !== -1 && this.args[flagIndex + 1] ? this.args[flagIndex + 1] : options?.default; if (value !== undefined) { const partialOption = { [key]: value, }; this.validator.validate(partialOption); } this.logInfo(`Retrieved option "${key}" with value: ${value}`); return value; } hasFlag(key) { const flag = `--${key}`; const exists = this.args.includes(flag); this.logInfo(`Flag "${flag}" is ${exists ? "present" : "not present"}.`); return exists; } getAllFlags() { const flags = {}; for (let i = 0; i < this.args.length; i++) { const arg = this.args[i]; if (arg.startsWith("--")) { const key = arg.slice(2); const nextArg = this.args[i + 1]; if (nextArg && !nextArg.startsWith("--")) { flags[key] = nextArg; i++; } else { flags[key] = true; } } } return flags; } getFlag(key, defaultValue = false) { const flags = this.getAllFlags(); return flags[key] ?? defaultValue; } } //# sourceMappingURL=ArgumentParser.js.map