You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

309 lines
8.2 KiB

4 years ago
  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. * @return {(Command | string)}
  213. */
  214. description(str: string): Command;
  215. description(): string;
  216. /**
  217. * Set an alias for the command.
  218. *
  219. * @param {string} alias
  220. * @return {(Command | string)}
  221. */
  222. alias(alias: string): Command;
  223. alias(): string;
  224. /**
  225. * Set or get the command usage.
  226. *
  227. * @param {string} str
  228. * @return {(Command | string)}
  229. */
  230. usage(str: string): Command;
  231. usage(): string;
  232. /**
  233. * Set the name of the command.
  234. *
  235. * @param {string} str
  236. * @return {Command}
  237. */
  238. name(str: string): Command;
  239. /**
  240. * Get the name of the command.
  241. *
  242. * @return {string}
  243. */
  244. name(): string;
  245. /**
  246. * Output help information for this command.
  247. *
  248. * @param {(str: string) => string} [cb]
  249. */
  250. outputHelp(cb?: (str: string) => string): void;
  251. /** Output help information and exit.
  252. *
  253. * @param {(str: string) => string} [cb]
  254. */
  255. help(cb?: (str: string) => string): never;
  256. }
  257. }
  258. declare namespace commander {
  259. type Command = local.Command
  260. type Option = local.Option
  261. interface CommandOptions {
  262. noHelp?: boolean;
  263. isDefault?: boolean;
  264. }
  265. interface ParseOptionsResult {
  266. args: string[];
  267. unknown: string[];
  268. }
  269. interface CommanderStatic extends Command {
  270. Command: typeof local.Command;
  271. Option: typeof local.Option;
  272. CommandOptions: CommandOptions;
  273. ParseOptionsResult: ParseOptionsResult;
  274. }
  275. }
  276. declare const commander: commander.CommanderStatic;
  277. export = commander;