UNPKG

takin

Version:

Front end engineering base toolchain and scaffold

110 lines (109 loc) 3.24 kB
import { Cli } from './index'; import Option, { OptionConfig } from './option'; interface CommandArg { required: boolean; value: string; variadic: boolean; } interface HelpSection { title?: string; body: string; } interface CommandConfig { allowUnknownOptions?: boolean; ignoreOptionDefaultValue?: boolean; } declare type HelpCallback = (sections: HelpSection[]) => void | HelpSection[]; declare type CommandExample = ((bin: string) => string) | string; /** * 子命令及参数 */ export declare type CommandOptions = { /** * 子命令名称, 如 compile */ name?: string; /** * 子命令参数, 如: compile filename 中的 filename */ args?: readonly string[]; /** * 子命令选项, 如 compile filename --verbose 中的 --verbose */ options?: Record<string, any>; }; export declare type CommandActionCallback = (cmd: CommandOptions) => any; declare class Command { rawName: string; description: string; config: CommandConfig; cli: Cli; options: Option[]; aliasNames: string[]; name: string; args: CommandArg[]; commandAction?: (...args: any[]) => any; usageText?: string; versionNumber?: string; examples: CommandExample[]; helpCallback?: HelpCallback; globalCommand?: GlobalCommand; constructor(rawName: string, description: string, config: CommandConfig, cli: Cli); usage(text: string): this; allowUnknownOptions(): this; ignoreOptionDefaultValue(): this; version(version: string, customFlags?: string, description?: string): this; example(example: CommandExample): this; /** * Add a option for this command * @param rawName Raw option name(s) * @param description Option description * @param config Option config */ option(rawName: string, description: string, config?: OptionConfig): this; alias(name: string): this; action(callback: CommandActionCallback): this; /** * Check if a command name is matched by this command * @param name Command name */ isMatched(name: string): boolean; get isDefaultCommand(): boolean; get isGlobalCommand(): boolean; /** * Check if an option is registered in this command * @param name Option name */ hasOption(name: string): boolean; /** * 获取已设置的命令行选项 * @param name - 选项名称(需要为驼峰形式, 如 outputPath) * @returns 选项实例 */ getOption(name: string): Option | undefined; /** * 为 option 设置别名 * @param name - option 名称 * @param alias - option 别名 */ aliasOption(name: string, alias: string): this; outputHelp(): void; outputVersion(): void; checkRequiredArgs(): void; /** * Check if the parsed options contain any unknown options * * Exit and output error when true */ checkUnknownOptions(): void; /** * Check if the required string-type options exist */ checkOptionValue(): void; } declare class GlobalCommand extends Command { constructor(cli: Cli); } export type { HelpCallback, CommandExample, CommandConfig }; export { GlobalCommand }; export default Command;