index.d.ts 8.3 KB

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