UNPKG

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