index.d.ts 4.9 KB

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