UNPKG

@megaorm/cli

Version:

This package allows you to communicate with MegaORM via commands directly from the command line interface (CLI).

131 lines (130 loc) 4.51 kB
/** * Represents a parsed parameter with a key (name) and its type (required, optional, or option). * * @property `key` The name of the parameter (e.g., 'name', 'male'). * @property `type` The type of the parameter (e.g., '!', '?', '-'). */ type Parameters = Array<{ key: string; type: string; }>; /** * Represents a parsed argument with a key (name) and its associated value (string). * * @property `key` The name of the argument (e.g., 'name', 'male'). * @property `value` The value of the argument (e.g., 'John', 'true'). */ type Arguments = Array<{ key: string; value: string; }>; /** * Represents a parsed option with a key (name) and its associated value (boolean). * * @property `key` The name of the option (e.g., 'verbose'). * @property `value` The value of the option (true or false). */ type Options = Array<{ key: string; value: string; }>; /** * The result of validating command arguments, consisting of parsed arguments and options. * * @property `arguments` An array of parsed arguments. * @property `options` An array of parsed options. */ type Result = { arguments: Arguments; options: Options; }; /** * Custom error class used in MegaCommand for handling command-related errors. */ export declare class MegaCommandError extends Error { } /** * Abstract class representing a command with a specific syntax, arguments, and options. * The class validates and parses the provided command-line arguments according to the defined syntax. * * @property `syntax` The command syntax that defines the required/optional parameters and options. * * @throws `MegaCommandError` Throws an error if the provided syntax is invalid. */ export declare abstract class MegaCommand { /** * The command syntax that defines parameters and options. */ protected static syntax: string; /** * Parsed arguments & options provided during command execution. */ protected static result: Result; /** * Parses the syntax string to extract command parameters. * It identifies required arguments (`<!`), optional arguments (`<?`), and options (`<-`). * * @param syntax The command syntax string to be parsed. * @returns An array of parsed parameters with their type and key. */ protected static parse(syntax: string): Parameters; /** * Validates command arguments based on the provided parameters. * Ensures that required arguments are present, optional arguments are handled properly, * and options are validated and parsed correctly. * * @param params An array of parameter definitions (each with a name and type). * @param args An array of command-line arguments provided by the user. * @returns The parsed result containing arguments and options. * @throws `MegaCommandError` if there are too many arguments, missing required arguments, or unexpected options. */ protected static validate(params: Parameters, args: Array<string>): Result; /** * Retrieves the value of a specific argument by its name. * * @param name The name of the argument to retrieve. * @returns The value of the argument. * @throws `MegaCommandError` if the argument name is invalid or not found. */ protected static argument(name: string): string | void; /** * Retrieves the value of a specific option by its name. * * @param name The name of the option to retrieve. * @returns The value of the option. * @throws `MegaCommandError` if the option name is invalid or not found. */ protected static option(name: string): boolean; /** * Logs an informational message in blue color. * * @param message The message to log. */ static info(message: string): void; /** * Logs an error message in red color. * * @param message The error message to log. */ static error(message: string): void; /** * Logs a warning message in yellow color. * * @param message The warning message to log. */ static warning(message: string): void; /** * Logs a success message in green color. * * @param message The success message to log. */ static success(message: string): void; /** * Executes the command logic. * This is an abstract method and should be implemented in a subclass. * * @returns The result of the command execution. */ static exec(): unknown; } export {};