UNPKG

6.27 kBTypeScriptView Raw
1
2// Type definitions for events 3.0
3// Project: https://github.com/Gozala/events
4// Definitions by: Yasunori Ohoka <https://github.com/yasupeke>
5// Shenwei Wang <https://github.com/weareoutman>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8type Listener = (...args: any[]) => void;
9
10declare class EventEmitter {
11 static listenerCount(emitter: EventEmitter, type: string | number): number;
12 static defaultMaxListeners: number;
13
14 eventNames(): Array<string | number>;
15 setMaxListeners(n: number): this;
16 getMaxListeners(): number;
17 emit(type: string | number, ...args: any[]): boolean;
18 addListener(type: string | number, listener: Listener): this;
19 on(type: string | number, listener: Listener): this;
20 once(type: string | number, listener: Listener): this;
21 prependListener(type: string | number, listener: Listener): this;
22 prependOnceListener(type: string | number, listener: Listener): this;
23 removeListener(type: string | number, listener: Listener): this;
24 off(type: string | number, listener: Listener): this;
25 removeAllListeners(type?: string | number): this;
26 listeners(type: string | number): Listener[];
27 listenerCount(type: string | number): number;
28 rawListeners(type: string | number): Listener[];
29}
30
31interface OptionConfig {
32 default?: any;
33 type?: any[];
34}
35declare class Option {
36 rawName: string;
37 description: string;
38 /** Option name */
39 name: string;
40 /** Option name and aliases */
41 names: string[];
42 isBoolean?: boolean;
43 required?: boolean;
44 config: OptionConfig;
45 negated: boolean;
46 constructor(rawName: string, description: string, config?: OptionConfig);
47}
48
49interface CommandArg {
50 required: boolean;
51 value: string;
52 variadic: boolean;
53}
54interface HelpSection {
55 title?: string;
56 body: string;
57}
58interface CommandConfig {
59 allowUnknownOptions?: boolean;
60 ignoreOptionDefaultValue?: boolean;
61}
62declare type HelpCallback = (sections: HelpSection[]) => void;
63declare type CommandExample = ((bin: string) => string) | string;
64declare class Command {
65 rawName: string;
66 description: string;
67 config: CommandConfig;
68 cli: CAC;
69 options: Option[];
70 aliasNames: string[];
71 name: string;
72 args: CommandArg[];
73 commandAction?: (...args: any[]) => any;
74 usageText?: string;
75 versionNumber?: string;
76 examples: CommandExample[];
77 helpCallback?: HelpCallback;
78 globalCommand?: GlobalCommand;
79 constructor(rawName: string, description: string, config: CommandConfig, cli: CAC);
80 usage(text: string): this;
81 allowUnknownOptions(): this;
82 ignoreOptionDefaultValue(): this;
83 version(version: string, customFlags?: string): this;
84 example(example: CommandExample): this;
85 /**
86 * Add a option for this command
87 * @param rawName Raw option name(s)
88 * @param description Option description
89 * @param config Option config
90 */
91 option(rawName: string, description: string, config?: OptionConfig): this;
92 alias(name: string): this;
93 action(callback: (...args: any[]) => any): this;
94 /**
95 * Check if a command name is matched by this command
96 * @param name Command name
97 */
98 isMatched(name: string): boolean;
99 get isDefaultCommand(): boolean;
100 get isGlobalCommand(): boolean;
101 /**
102 * Check if an option is registered in this command
103 * @param name Option name
104 */
105 hasOption(name: string): Option | undefined;
106 outputHelp(): void;
107 outputVersion(): void;
108 checkRequiredArgs(): void;
109 /**
110 * Check if the parsed options contain any unknown options
111 *
112 * Exit and output error when true
113 */
114 checkUnknownOptions(): void;
115 /**
116 * Check if the required string-type options exist
117 */
118 checkOptionValue(): void;
119}
120declare class GlobalCommand extends Command {
121 constructor(cli: CAC);
122}
123
124interface ParsedArgv {
125 args: ReadonlyArray<string>;
126 options: {
127 [k: string]: any;
128 };
129}
130declare class CAC extends EventEmitter {
131 /** The program name to display in help and version message */
132 name: string;
133 commands: Command[];
134 globalCommand: GlobalCommand;
135 matchedCommand?: Command;
136 matchedCommandName?: string;
137 /**
138 * Raw CLI arguments
139 */
140 rawArgs: string[];
141 /**
142 * Parsed CLI arguments
143 */
144 args: ParsedArgv['args'];
145 /**
146 * Parsed CLI options, camelCased
147 */
148 options: ParsedArgv['options'];
149 private showHelpOnExit;
150 private showVersionOnExit;
151 /**
152 * @param name The program name to display in help and version message
153 */
154 constructor(name?: string);
155 /**
156 * Add a global usage text.
157 *
158 * This is not used by sub-commands.
159 */
160 usage(text: string): this;
161 /**
162 * Add a sub-command
163 */
164 command(rawName: string, description?: string, config?: CommandConfig): Command;
165 /**
166 * Add a global CLI option.
167 *
168 * Which is also applied to sub-commands.
169 */
170 option(rawName: string, description: string, config?: OptionConfig): this;
171 /**
172 * Show help message when `-h, --help` flags appear.
173 *
174 */
175 help(callback?: HelpCallback): this;
176 /**
177 * Show version number when `-v, --version` flags appear.
178 *
179 */
180 version(version: string, customFlags?: string): this;
181 /**
182 * Add a global example.
183 *
184 * This example added here will not be used by sub-commands.
185 */
186 example(example: CommandExample): this;
187 /**
188 * Output the corresponding help message
189 * When a sub-command is matched, output the help message for the command
190 * Otherwise output the global one.
191 *
192 */
193 outputHelp(): void;
194 /**
195 * Output the version number.
196 *
197 */
198 outputVersion(): void;
199 private setParsedInfo;
200 unsetMatchedCommand(): void;
201 /**
202 * Parse argv
203 */
204 parse(argv?: string[], {
205 /** Whether to run the action for matched command */
206 run }?: {
207 run?: boolean | undefined;
208 }): ParsedArgv;
209 private mri;
210 runMatchedCommand(): any;
211}
212
213/**
214 * @param name The program name to display in help and version message
215 */
216declare const cac: (name?: string) => CAC;
217
218export default cac;
219export { CAC, Command, cac };