UNPKG

kist

Version:

Package Pipeline Processor

120 lines (119 loc) 4.73 kB
"use strict"; // ============================================================================ // Import // ============================================================================ Object.defineProperty(exports, "__esModule", { value: true }); exports.ArgumentParser = void 0; const AbstractProcess_1 = require("../core/abstract/AbstractProcess"); const OptionsValidator_1 = require("../core/validation/OptionsValidator"); // ============================================================================ // Class // ============================================================================ /** * ArgumentParser handles parsing and validating command-line arguments * against the structure defined in OptionsInterface. * Extends AbstractProcess for consistent logging. */ class ArgumentParser extends AbstractProcess_1.AbstractProcess { // Constructor // ======================================================================== /** * Initializes the ArgumentParser with command-line arguments and an * instance of OptionsValidator for validation. * * @param args - Command-line arguments. Defaults to `process.argv.slice(2)`. */ constructor() { // args: string[] = process.argv.slice(2) super(); // Skip Node.js and script path this.args = process.argv.slice(2); // console.log(process.argv.slice(2)) // this.args = args; this.validator = new OptionsValidator_1.OptionsValidator(); this.logDebug("ArgumentParser initialized with arguments."); } // Methods // ======================================================================== /** * Retrieves the value of a specific option from the CLI arguments, with * validation. * * @param key - The name of the option (matches keys in OptionsInterface). * @param options - Additional options: * - `default`: The default value to return if the option is not found. * @returns The value of the option or the default value if not found. * @throws Error if the value is invalid based on the validation rules. */ 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) { // Create a partial object to validate the specific key-value pair const partialOption = { [key]: value, }; // Validate the key-value pair this.validator.validate(partialOption); } this.logInfo(`Retrieved option "${key}" with value: ${value}`); return value; } /** * Checks if a specific flag exists in the CLI arguments. * * @param key - The name of the flag to check (e.g., "dryRun"). * @returns `true` if the flag is present, otherwise `false`. */ hasFlag(key) { const flag = `--${key}`; const exists = this.args.includes(flag); this.logInfo(`Flag "${flag}" is ${exists ? "present" : "not present"}.`); return exists; } /** * Parses all CLI arguments into a key-value object. * Flags are treated as boolean if not followed by a value. * * Example: * --live --mode development => { live: true, mode: "development" } * * @returns A key-value object of all parsed CLI arguments. */ 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; // Skip the next argument since it's a value i++; } else { // Flag with no value is treated as boolean true flags[key] = true; } } } return flags; } /** * Retrieves a specific flag value. * * @param key - The flag name to retrieve. * @param defaultValue - The default value if the flag is not present. * @returns The value of the flag or the default value. */ getFlag(key, defaultValue = false) { var _a; const flags = this.getAllFlags(); return (_a = flags[key]) !== null && _a !== void 0 ? _a : defaultValue; } } exports.ArgumentParser = ArgumentParser;