UNPKG

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