index.d.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // Project: https://github.com/visionmedia/commander.js
  2. // Definitions by: Alan Agius <https://github.com/alan-agius4>, Marcelo Dezem <https://github.com/mdezem>, vvakame <https://github.com/vvakame>
  3. declare class Option {
  4. flags: string;
  5. required: boolean;
  6. optional: boolean;
  7. bool: boolean;
  8. short?: string;
  9. long: string;
  10. description: string;
  11. /**
  12. * Initialize a new `Option` with the given `flags` and `description`.
  13. *
  14. * @param {string} flags
  15. * @param {string} [description]
  16. */
  17. constructor(flags: string, description?: string);
  18. }
  19. declare class Command extends NodeJS.EventEmitter {
  20. [key: string]: any;
  21. args: string[];
  22. /**
  23. * Initialize a new `Command`.
  24. *
  25. * @param {string} [name]
  26. */
  27. constructor(name?: string);
  28. /**
  29. * Set the program version to `str`.
  30. *
  31. * This method auto-registers the "-V, --version" flag
  32. * which will print the version number when passed.
  33. *
  34. * @param {string} str
  35. * @param {string} [flags]
  36. * @returns {Command} for chaining
  37. */
  38. version(str: string, flags?: string): Command;
  39. /**
  40. * Add command `name`.
  41. *
  42. * The `.action()` callback is invoked when the
  43. * command `name` is specified via __ARGV__,
  44. * and the remaining arguments are applied to the
  45. * function for access.
  46. *
  47. * When the `name` is "*" an un-matched command
  48. * will be passed as the first arg, followed by
  49. * the rest of __ARGV__ remaining.
  50. *
  51. * @example
  52. * program
  53. * .version('0.0.1')
  54. * .option('-C, --chdir <path>', 'change the working directory')
  55. * .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
  56. * .option('-T, --no-tests', 'ignore test hook')
  57. *
  58. * program
  59. * .command('setup')
  60. * .description('run remote setup commands')
  61. * .action(function() {
  62. * console.log('setup');
  63. * });
  64. *
  65. * program
  66. * .command('exec <cmd>')
  67. * .description('run the given remote command')
  68. * .action(function(cmd) {
  69. * console.log('exec "%s"', cmd);
  70. * });
  71. *
  72. * program
  73. * .command('teardown <dir> [otherDirs...]')
  74. * .description('run teardown commands')
  75. * .action(function(dir, otherDirs) {
  76. * console.log('dir "%s"', dir);
  77. * if (otherDirs) {
  78. * otherDirs.forEach(function (oDir) {
  79. * console.log('dir "%s"', oDir);
  80. * });
  81. * }
  82. * });
  83. *
  84. * program
  85. * .command('*')
  86. * .description('deploy the given env')
  87. * .action(function(env) {
  88. * console.log('deploying "%s"', env);
  89. * });
  90. *
  91. * program.parse(process.argv);
  92. *
  93. * @param {string} name
  94. * @param {string} [desc] for git-style sub-commands
  95. * @param {CommandOptions} [opts] command options
  96. * @returns {Command} the new command
  97. */
  98. command(name: string, desc?: string, opts?: commander.CommandOptions): Command;
  99. /**
  100. * Define argument syntax for the top-level command.
  101. *
  102. * @param {string} desc
  103. * @returns {Command} for chaining
  104. */
  105. arguments(desc: string): Command;
  106. /**
  107. * Parse expected `args`.
  108. *
  109. * For example `["[type]"]` becomes `[{ required: false, name: 'type' }]`.
  110. *
  111. * @param {string[]} args
  112. * @returns {Command} for chaining
  113. */
  114. parseExpectedArgs(args: string[]): Command;
  115. /**
  116. * Register callback `fn` for the command.
  117. *
  118. * @example
  119. * program
  120. * .command('help')
  121. * .description('display verbose help')
  122. * .action(function() {
  123. * // output help here
  124. * });
  125. *
  126. * @param {(...args: any[]) => void} fn
  127. * @returns {Command} for chaining
  128. */
  129. action(fn: (...args: any[]) => void): Command;
  130. /**
  131. * Define option with `flags`, `description` and optional
  132. * coercion `fn`.
  133. *
  134. * The `flags` string should contain both the short and long flags,
  135. * separated by comma, a pipe or space. The following are all valid
  136. * all will output this way when `--help` is used.
  137. *
  138. * "-p, --pepper"
  139. * "-p|--pepper"
  140. * "-p --pepper"
  141. *
  142. * @example
  143. * // simple boolean defaulting to false
  144. * program.option('-p, --pepper', 'add pepper');
  145. *
  146. * --pepper
  147. * program.pepper
  148. * // => Boolean
  149. *
  150. * // simple boolean defaulting to true
  151. * program.option('-C, --no-cheese', 'remove cheese');
  152. *
  153. * program.cheese
  154. * // => true
  155. *
  156. * --no-cheese
  157. * program.cheese
  158. * // => false
  159. *
  160. * // required argument
  161. * program.option('-C, --chdir <path>', 'change the working directory');
  162. *
  163. * --chdir /tmp
  164. * program.chdir
  165. * // => "/tmp"
  166. *
  167. * // optional argument
  168. * program.option('-c, --cheese [type]', 'add cheese [marble]');
  169. *
  170. * @param {string} flags
  171. * @param {string} [description]
  172. * @param {((arg1: any, arg2: any) => void) | RegExp} [fn] function or default
  173. * @param {*} [defaultValue]
  174. * @returns {Command} for chaining
  175. */
  176. option(flags: string, description?: string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command;
  177. option(flags: string, description?: string, defaultValue?: any): Command;
  178. /**
  179. * Allow unknown options on the command line.
  180. *
  181. * @param {boolean} [arg] if `true` or omitted, no error will be thrown for unknown options.
  182. * @returns {Command} for chaining
  183. */
  184. allowUnknownOption(arg?: boolean): Command;
  185. /**
  186. * Parse `argv`, settings options and invoking commands when defined.
  187. *
  188. * @param {string[]} argv
  189. * @returns {Command} for chaining
  190. */
  191. parse(argv: string[]): Command;
  192. /**
  193. * Parse options from `argv` returning `argv` void of these options.
  194. *
  195. * @param {string[]} argv
  196. * @returns {ParseOptionsResult}
  197. */
  198. parseOptions(argv: string[]): commander.ParseOptionsResult;
  199. /**
  200. * Return an object containing options as key-value pairs
  201. *
  202. * @returns {{[key: string]: string}}
  203. */
  204. opts(): { [key: string]: string };
  205. /**
  206. * Set the description to `str`.
  207. *
  208. * @param {string} str
  209. * @return {(Command | string)}
  210. */
  211. description(str: string): Command;
  212. description(): string;
  213. /**
  214. * Set an alias for the command.
  215. *
  216. * @param {string} alias
  217. * @return {(Command | string)}
  218. */
  219. alias(alias: string): Command;
  220. alias(): string;
  221. /**
  222. * Set or get the command usage.
  223. *
  224. * @param {string} str
  225. * @return {(Command | string)}
  226. */
  227. usage(str: string): Command;
  228. usage(): string;
  229. /**
  230. * Set the name of the command.
  231. *
  232. * @param {string} str
  233. * @return {Command}
  234. */
  235. name(str: string): Command;
  236. /**
  237. * Get the name of the command.
  238. *
  239. * @return {string}
  240. */
  241. name(): string;
  242. /**
  243. * Output help information for this command.
  244. *
  245. * @param {(str: string) => string} [cb]
  246. */
  247. outputHelp(cb?: (str: string) => string): void;
  248. /** Output help information and exit. */
  249. help(): void;
  250. }
  251. declare namespace commander {
  252. interface CommandOptions {
  253. noHelp?: boolean;
  254. isDefault?: boolean;
  255. }
  256. interface ParseOptionsResult {
  257. args: string[];
  258. unknown: string[];
  259. }
  260. interface CommanderStatic extends Command {
  261. Command: typeof Command;
  262. Option: typeof Option;
  263. CommandOptions: CommandOptions;
  264. ParseOptionsResult: ParseOptionsResult;
  265. }
  266. }
  267. declare const commander: commander.CommanderStatic;
  268. export = commander;