import commander from 'commander'; import { WithRequired } from './Types'; /** * The general texts that a CLI app uses. * This include the description texts that are used as the description of the * different parts of the CLI. * The `name` and the `versionNumber` are expected to be the app name (No translation * is used, as the name should be the same through all the app), and the `versionNumber` * should be the version in the major.minor.patch format. */ export interface CLIGeneralTexts { /** The application name */ name: string; /** The application version number in semVer */ versionNumber: string; /** A text displayed when showing the application's help */ help: string; /** The language used by the application */ language?: string; /** The error message displayed when using a wrong language */ languageError?: string; /** Text used by the tool */ tool: string; /** The text displayed when showing the version of the application */ version: string; } /** * The general flags that a CLI app accepts, when configured to used them. * Note that currently the default flags cannot be changed. */ export interface CLIGeneralFlags { /** The help flags, both short and long */ help: { short: string; long: string; }; /** The language set flags, both short and long */ language: { short: string; long: string; }; /** The version information flags, both short and long */ version: { short: string; long: string; }; /** The input file flags, both short and long */ in: { short: string; long: string; }; /** The output file flags, both short and long */ out: { short: string; long: string; }; } /** * A set of options for initially configure a CLI application. * If a translation is given */ export interface CLIAppOptions { /** * The description texts that is used as the description of the different parts of the CLI. * The `name` and the `versionNumber` are expected to be the app name (No translation * is used, as the name should be the same through all the app), and the `versionNumber` * should be the version in the major.minor.patch format. */ texts: CLIGeneralTexts; /** * The flag names to use in this application, if the flags differ in any way from * the default ones. * * The default flags include: * * help: -h, --help * * version: -v, --version * * language selection: -l, --language * * input file for a command: -i, --in * * output file for a command: -o, --out */ flags?: CLIGeneralFlags; } /** * A builder for a CLI command. May be the main command of the app ({@link CLIApp}) * extends this class) or a sub-command. */ export declare class CLICommandBuilder { private static SHORT_HELP_FLAG; private static SHORT_VERSION_FLAG; private static SHORT_LANG_FLAG; private static SHORT_INPUT_FLAG; private static SHORT_OUTPUT_FLAG; private static LONG_HELP_FLAG; private static LONG_VERSION_FLAG; private static LONG_LANG_FLAG; private static LONG_INPUT_FLAG; private static LONG_OUTPUT_FLAG; protected program: commander.Command; protected hasAction: boolean; protected currentArgs: unknown[]; protected currentOptions: unknown; protected onReadErrorMsg: string; protected options: WithRequired; protected isSubcommand: boolean; constructor(cmdrProgram: commander.Command, options: CLIAppOptions, isSubcommand?: boolean); /** * Make this command to be able to read input from a file. * * @param description - The input flag description or the translation key if a translator is used. * @param onReadErrorMsg - The error message or translation key if a translator is used. */ input(description: string, onReadErrorMsg: string): this; /** * Make this command to be able to write the output to a file. * * @param description - The output flag description or translation key if a translator is used. */ output(description: string): this; /** * Add a new option to the command. * * @param flags - The flags to trigger this option * @param description - The description or translation key if a translator is used. * @param defaultValue - A default value. */ option(flags: string, description?: string, defaultValue?: string | boolean): this; /** * Set the action for this command. The action callback receives both the current * command, and the arguments (Note this is one or more arguments, depending * on the command definition. Mandatory or optional positional arguments are * passed first, while the last element consists of the flags passed to the command) * * @param f - The callback to run when this command is called. */ action(f: (cliapp: this, ...args: unknown[]) => void): this; /** * Read the input to this command. The input may be the first arguments passed * to a command (without the flags, separated by space) if a mandatory argument * or optional argument was given, or the contents of the * input file if an input was configured. */ read(): string; /** * Write the given data to the standard output, or, to the expected file, * if an output was configured. Note that when outputting to a file, if the * file does not exists, it gets created. If it already exists, then the * output is appended to the previously defined contents of that file. * * @param data - The data to output */ write(data: string): void; /** * Read the contents of a file. Throws error if * the file does not exist. * * @param fileName - The file to read. */ readFileInput(fileName: string): string; /** * Write a set of contents to a given file. * * @param fileName - The file to write to. * @param contents - The contents to write. */ writeToFile(fileName: string, contents: string): void; /** * Write a set of contents to the standard output. * * @param contents - The contents to write. */ writeToConsole(contents: string): void; /** * Ensure a condition is met, and if not, show the given error message, * and exit the application with 1. * * @param condition - The condition that needs to satisfy * @param error - */ ensureOrFailAndExit(condition: boolean, error: string): void; /** * Returns true if the command received no arguments nor flags */ hasNoArgs(): boolean; /** * Output the command help. */ outputHelp(): void; /** * Output the command's help if no arguments where given, * then exit the application with 0. */ outputHelpOnNoArgs(): void; /** * Exit the application with the given value. * * @param value - The value to exit with */ exit(value: number): void; } /** * The CLIApp class is the class to extend in order to define your CLI based * application. */ export declare class CLIApp extends CLICommandBuilder { /** The arguments passed to the application */ private processArgs; constructor(options: CLIAppOptions); /** * Run the CLI app. * Call when your CLI app has been completely configured over the main app. */ run(): void; /** * Define a new sub-command. * * @param name - The new sub-command name * @param description - The sub-command description, or translation key if a translator is used. * @param f - A callback to construct the newly defined sub-command. */ command(name: string, description: string, f: (cmd: CLICommandBuilder) => void): this; } /** * The Type of a CLI application */ export type cli = CLIApp; /** * Create a new CLI application. * @param options - The application options. * * @group API: Main */ export declare const cli: (options: CLIAppOptions) => CLIApp; /** * Retrieves an object with the data from a JSON file * after reading the same from the command line. * This function is useful here as most times you will * want to retrieve data from the package.json file. * * @param fileLocation - The location of the file to read. */ export declare const readJSON: (fileLocation: string) => unknown; //# sourceMappingURL=cli.d.ts.map