UNPKG

takin

Version:

Front end engineering base toolchain and scaffold

115 lines (114 loc) 3.23 kB
/// <reference types="node" /> import { EventEmitter } from 'events'; import type { Runner } from '../runner'; import Command, { CommandActionCallback, CommandConfig, CommandExample, CommandOptions, GlobalCommand, HelpCallback } from './command'; import { OptionConfig } from './option'; interface ParsedArgv { args: ReadonlyArray<string>; options: { [k: string]: any; }; } export { Command, CommandOptions, CommandActionCallback }; declare type CommandName = string; export declare type PluginProvidedCommand = { pluginName: string; command: Command; }; export declare class Cli extends EventEmitter { /** The program name to display in help and version message */ name: string; pluginName: string; readonly runner: Runner; commands: Map<CommandName, PluginProvidedCommand>; globalCommand: GlobalCommand; matchedCommand?: Command; matchedCommandName?: string; /** * Raw CLI arguments */ rawArgs: string[]; /** * Parsed CLI arguments */ args: ParsedArgv['args']; /** * Parsed CLI options, camelCased */ options: ParsedArgv['options']; showHelpOnExit?: boolean; showVersionOnExit?: boolean; /** * @param name The program name to display in help and version message */ constructor(name: string, runner: Runner); /** * Add a global usage text. * * This is not used by sub-commands. */ usage(text: string): this; /** * 添加子命令 */ command(rawName: string, description?: string, config?: CommandConfig): Command; /** * Add a global CLI option. * * Which is also applied to sub-commands. */ option(rawName: string, description: string, config?: OptionConfig): this; /** * Show help message when `-h, --help` flags appear. * */ help(callback?: HelpCallback): this; /** * Show version number when `-v, --version` flags appear. * */ version(version: string, customFlags?: string): this; /** * Add a global example. * * This example added here will not be used by sub-commands. */ example(example: CommandExample): this; /** * Output the corresponding help message * When a sub-command is matched, output the help message for the command * Otherwise output the global one. * */ outputHelp(): void; /** * Output the version number. * */ outputVersion(): void; private setParsedInfo; /** * 自定义 setParsedInfo * @param param0 - ParsedArgv 已解析的 argv * @param matchedCommand - 匹配的 命令实例 * @param matchedCommandName - 匹配的命令名称 */ private setParsedInfo2; unsetMatchedCommand(): void; /** * Parse argv */ parse(argv?: string[], { /** Whether to run the action for matched command */ run }?: { run?: boolean; }): ParsedArgv; parseByCommand(command: CommandOptions): ParsedArgv; /** * 准备匹配的命令选项 * @returns 匹配的命令选项 */ prepareMatchedCommandAndArgs(): void | CommandOptions; private mri; runMatchedCommand(): any; }