index.d.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Type definitions for commander
  2. // Original definitions by: Alan Agius <https://github.com/alan-agius4>, Marcelo Dezem <https://github.com/mdezem>, vvakame <https://github.com/vvakame>, Jules Randolph <https://github.com/sveinburne>
  3. ///<reference types="node" />
  4. declare namespace commander {
  5. interface CommanderError extends Error {
  6. code: string;
  7. exitCode: number;
  8. message: string;
  9. nestedError?: string;
  10. }
  11. type CommanderErrorConstructor = { new (exitCode: number, code: string, message: string): CommanderError };
  12. interface Option {
  13. flags: string;
  14. required: boolean; // A value must be supplied when the option is specified.
  15. optional: boolean; // A value is optional when the option is specified.
  16. mandatory: boolean; // The option must have a value after parsing, which usually means it must be specified on command line.
  17. bool: boolean;
  18. short?: string;
  19. long: string;
  20. description: string;
  21. }
  22. type OptionConstructor = { new (flags: string, description?: string): Option };
  23. interface Command extends NodeJS.EventEmitter {
  24. [key: string]: any; // options as properties
  25. args: string[];
  26. /**
  27. * Set the program version to `str`.
  28. *
  29. * This method auto-registers the "-V, --version" flag
  30. * which will print the version number when passed.
  31. *
  32. * You can optionally supply the flags and description to override the defaults.
  33. */
  34. version(str: string, flags?: string, description?: string): Command;
  35. /**
  36. * Define a command, implemented using an action handler.
  37. *
  38. * @remarks
  39. * The command description is supplied using `.description`, not as a parameter to `.command`.
  40. *
  41. * @example
  42. * ```ts
  43. * program
  44. * .command('clone <source> [destination]')
  45. * .description('clone a repository into a newly created directory')
  46. * .action((source, destination) => {
  47. * console.log('clone command called');
  48. * });
  49. * ```
  50. *
  51. * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
  52. * @param opts - configuration options
  53. * @returns new command
  54. */
  55. command(nameAndArgs: string, opts?: CommandOptions): Command;
  56. /**
  57. * Define a command, implemented in a separate executable file.
  58. *
  59. * @remarks
  60. * The command description is supplied as the second parameter to `.command`.
  61. *
  62. * @example
  63. * ```ts
  64. * program
  65. * .command('start <service>', 'start named service')
  66. * .command('stop [service]', 'stop named serice, or all if no name supplied');
  67. * ```
  68. *
  69. * @param nameAndArgs - command name and arguments, args are `<required>` or `[optional]` and last may also be `variadic...`
  70. * @param description - description of executable command
  71. * @param opts - configuration options
  72. * @returns top level command for chaining more command definitions
  73. */
  74. command(nameAndArgs: string, description: string, opts?: commander.CommandOptions): Command;
  75. /**
  76. * Define argument syntax for the top-level command.
  77. *
  78. * @returns Command for chaining
  79. */
  80. arguments(desc: string): Command;
  81. /**
  82. * Parse expected `args`.
  83. *
  84. * For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
  85. *
  86. * @returns Command for chaining
  87. */
  88. parseExpectedArgs(args: string[]): Command;
  89. /**
  90. * Register callback to use as replacement for calling process.exit.
  91. */
  92. exitOverride(callback?: (err: CommanderError) => never|void): Command;
  93. /**
  94. * Register callback `fn` for the command.
  95. *
  96. * @example
  97. * program
  98. * .command('help')
  99. * .description('display verbose help')
  100. * .action(function() {
  101. * // output help here
  102. * });
  103. *
  104. * @returns Command for chaining
  105. */
  106. action(fn: (...args: any[]) => void | Promise<void>): Command;
  107. /**
  108. * Define option with `flags`, `description` and optional
  109. * coercion `fn`.
  110. *
  111. * The `flags` string should contain both the short and long flags,
  112. * separated by comma, a pipe or space. The following are all valid
  113. * all will output this way when `--help` is used.
  114. *
  115. * "-p, --pepper"
  116. * "-p|--pepper"
  117. * "-p --pepper"
  118. *
  119. * @example
  120. * // simple boolean defaulting to false
  121. * program.option('-p, --pepper', 'add pepper');
  122. *
  123. * --pepper
  124. * program.pepper
  125. * // => Boolean
  126. *
  127. * // simple boolean defaulting to true
  128. * program.option('-C, --no-cheese', 'remove cheese');
  129. *
  130. * program.cheese
  131. * // => true
  132. *
  133. * --no-cheese
  134. * program.cheese
  135. * // => false
  136. *
  137. * // required argument
  138. * program.option('-C, --chdir <path>', 'change the working directory');
  139. *
  140. * --chdir /tmp
  141. * program.chdir
  142. * // => "/tmp"
  143. *
  144. * // optional argument
  145. * program.option('-c, --cheese [type]', 'add cheese [marble]');
  146. *
  147. * @returns Command for chaining
  148. */
  149. option(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
  150. option(flags: string, description?: string, defaultValue?: any): Command;
  151. /**
  152. * Define a required option, which must have a value after parsing. This usually means
  153. * the option must be specified on the command line. (Otherwise the same as .option().)
  154. *
  155. * The `flags` string should contain both the short and long flags, separated by comma, a pipe or space.
  156. */
  157. requiredOption(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
  158. requiredOption(flags: string, description?: string, defaultValue?: any): Command;
  159. /**
  160. * Whether to store option values as properties on command object,
  161. * or store separately (specify false). In both cases the option values can be accessed using .opts().
  162. *
  163. * @return Command for chaining
  164. */
  165. storeOptionsAsProperties(value?: boolean): Command;
  166. /**
  167. * Whether to pass command to action handler,
  168. * or just the options (specify false).
  169. *
  170. * @return Command for chaining
  171. */
  172. passCommandToAction(value?: boolean): Command;
  173. /**
  174. * Allow unknown options on the command line.
  175. *
  176. * @param [arg] if `true` or omitted, no error will be thrown for unknown options.
  177. * @returns Command for chaining
  178. */
  179. allowUnknownOption(arg?: boolean): Command;
  180. /**
  181. * Parse `argv`, setting options and invoking commands when defined.
  182. *
  183. * @returns Command for chaining
  184. */
  185. parse(argv: string[]): Command;
  186. /**
  187. * Parse `argv`, setting options and invoking commands when defined.
  188. *
  189. * Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.
  190. *
  191. * @returns Promise
  192. */
  193. parseAsync(argv: string[]): Promise<any>;
  194. /**
  195. * Parse options from `argv` returning `argv` void of these options.
  196. */
  197. parseOptions(argv: string[]): commander.ParseOptionsResult;
  198. /**
  199. * Return an object containing options as key-value pairs
  200. */
  201. opts(): { [key: string]: any };
  202. /**
  203. * Set the description.
  204. *
  205. * @returns Command for chaining
  206. */
  207. description(str: string, argsDescription?: {[argName: string]: string}): Command;
  208. /**
  209. * Get the description.
  210. */
  211. description(): string;
  212. /**
  213. * Set an alias for the command.
  214. *
  215. * @returns Command for chaining
  216. */
  217. alias(alias: string): Command;
  218. /**
  219. * Get alias for the command.
  220. */
  221. alias(): string;
  222. /**
  223. * Set the command usage.
  224. *
  225. * @returns Command for chaining
  226. */
  227. usage(str: string): Command;
  228. /**
  229. * Get the command usage.
  230. */
  231. usage(): string;
  232. /**
  233. * Set the name of the command.
  234. *
  235. * @returns Command for chaining
  236. */
  237. name(str: string): Command;
  238. /**
  239. * Get the name of the command.
  240. */
  241. name(): string;
  242. /**
  243. * Output help information for this command.
  244. *
  245. * When listener(s) are available for the helpLongFlag
  246. * those callbacks are invoked.
  247. */
  248. outputHelp(cb?: (str: string) => string): void;
  249. /**
  250. * You can pass in flags and a description to override the help
  251. * flags and help description for your command.
  252. */
  253. helpOption(flags?: string, description?: string): Command;
  254. /**
  255. * Output help information and exit.
  256. */
  257. help(cb?: (str: string) => string): never;
  258. }
  259. type CommandConstructor = { new (name?: string): Command };
  260. interface CommandOptions {
  261. noHelp?: boolean;
  262. isDefault?: boolean;
  263. executableFile?: string;
  264. }
  265. interface ParseOptionsResult {
  266. args: string[];
  267. unknown: string[];
  268. }
  269. interface CommanderStatic extends Command {
  270. Command: CommandConstructor;
  271. Option: OptionConstructor;
  272. CommanderError:CommanderErrorConstructor;
  273. }
  274. }
  275. declare const commander: commander.CommanderStatic;
  276. export = commander;