kist
Version:
Lightweight Package Pipeline Processor with Plugin Architecture
55 lines • 1.99 kB
JavaScript
import { AbstractProcess } from "../core/abstract/AbstractProcess.js";
import { OptionsValidator } from "../core/validation/OptionsValidator.js";
export class ArgumentParser extends AbstractProcess {
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 === null || options === void 0 ? void 0 : 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) {
var _a;
const flags = this.getAllFlags();
return (_a = flags[key]) !== null && _a !== void 0 ? _a : defaultValue;
}
}
//# sourceMappingURL=ArgumentParser.js.map