9865e476ad2c0de12aefef14915e566dfb5bc74091e469db54eab04c96263be2d36b618d68626cb1a7416514fe83ce957d6d6ab9729c2994f715397d804ca0 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. # Command-Option-Argument
  2. [![build status](https://secure.travis-ci.org/veged/coa.png)](http://travis-ci.org/veged/coa)
  3. ## What is it?
  4. COA is a parser for command line options that aim to get maximum profit from formalization your program API.
  5. Once you write definition in terms of commands, options and arguments you automaticaly get:
  6. * Command line help text
  7. * Program API for use COA-based programs as modules
  8. * Shell completion
  9. ### Other features
  10. * Rich types for options and arguments, such as arrays, boolean flags and required
  11. * Commands can be async throught using promising (powered by [Q](https://github.com/kriskowal/q))
  12. * Easy submoduling some existing commands to new top-level one
  13. * Combined validation and complex parsing of values
  14. ### TODO
  15. * Localization
  16. * Shell-mode
  17. * Configs
  18. * Aliases
  19. * Defaults
  20. ## Examples
  21. ````javascript
  22. require('coa').Cmd() // main (top level) command declaration
  23. .name(process.argv[1]) // set top level command name from program name
  24. .title('My awesome command line util') // title for use in text messages
  25. .helpful() // make command "helpful", i.e. options -h --help with usage message
  26. .opt() // add some option
  27. .name('version') // name for use in API
  28. .title('Version') // title for use in text messages
  29. .short('v') // short key: -v
  30. .long('version') // long key: --version
  31. .flag() // for options without value
  32. .act(function(opts) { // add action for option
  33. // return message as result of action
  34. return JSON.parse(require('fs').readFileSync(__dirname + '/package.json'))
  35. .version;
  36. })
  37. .end() // end option chain and return to main command
  38. .cmd().name('subcommand').apply(require('./subcommand').COA).end() // load subcommand from module
  39. .cmd() // inplace subcommand declaration
  40. .name('othercommand').title('Awesome other subcommand').helpful()
  41. .opt()
  42. .name('input').title('input file, required')
  43. .short('i').long('input')
  44. .val(function(v) { // validator function, also for translate simple values
  45. return require('fs').createReadStream(v) })
  46. .req() // make option required
  47. .end() // end option chain and return to command
  48. .end() // end subcommand chain and return to parent command
  49. .run(process.argv.slice(2)); // parse and run on process.argv
  50. ````
  51. ````javascript
  52. // subcommand.js
  53. exports.COA = function() {
  54. this
  55. .title('Awesome subcommand').helpful()
  56. .opt()
  57. .name('output').title('output file')
  58. .short('o').long('output')
  59. .output() // use default preset for "output" option declaration
  60. .end()
  61. };
  62. ````
  63. ## API reference
  64. ### Cmd
  65. Command is a top level entity. Commands may have options and arguments.
  66. #### Cmd.api
  67. Returns object containing all its subcommands as methods to use from other programs.<br>
  68. **@returns** *{Object}*
  69. #### Cmd.name
  70. Set a canonical command identifier to be used anywhere in the API.<br>
  71. **@param** *String* `_name` command name<br>
  72. **@returns** *COA.Cmd* `this` instance (for chainability)
  73. #### Cmd.title
  74. Set a long description for command to be used anywhere in text messages.<br>
  75. **@param** *String* `_title` command title<br>
  76. **@returns** *COA.Cmd* `this` instance (for chainability)
  77. #### Cmd.cmd
  78. Create new or add existing subcommand for current command.<br>
  79. **@param** *COA.Cmd* `[cmd]` existing command instance<br>
  80. **@returns** *COA.Cmd* new or added subcommand instance
  81. #### Cmd.opt
  82. Create option for current command.<br>
  83. **@returns** *COA.Opt* `new` option instance
  84. #### Cmd.arg
  85. Create argument for current command.<br>
  86. **@returns** *COA.Opt* `new` argument instance
  87. #### Cmd.act
  88. Add (or set) action for current command.<br>
  89. **@param** *Function* `act` action function,
  90. invoked in the context of command instance
  91. and has the parameters:<br>
  92. - *Object* `opts` parsed options<br>
  93. - *Array* `args` parsed arguments<br>
  94. - *Object* `res` actions result accumulator<br>
  95. It can return rejected promise by Cmd.reject (in case of error)
  96. or any other value treated as result.<br>
  97. **@param** *{Boolean}* [force=false] flag for set action instead add to existings<br>
  98. **@returns** *COA.Cmd* `this` instance (for chainability)
  99. #### Cmd.apply
  100. Apply function with arguments in context of command instance.<br>
  101. **@param** *Function* `fn`<br>
  102. **@param** *Array* `args`<br>
  103. **@returns** *COA.Cmd* `this` instance (for chainability)
  104. #### Cmd.comp
  105. Set custom additional completion for current command.<br>
  106. **@param** *Function* `fn` completion generation function,
  107. invoked in the context of command instance.
  108. Accepts parameters:<br>
  109. - *Object* `opts` completion options<br>
  110. It can return promise or any other value treated as result.<br>
  111. **@returns** *COA.Cmd* `this` instance (for chainability)
  112. #### Cmd.helpful
  113. Make command "helpful", i.e. add -h --help flags for print usage.<br>
  114. **@returns** *COA.Cmd* `this` instance (for chainability)
  115. #### Cmd.completable
  116. Adds shell completion to command, adds "completion" subcommand, that makes all the magic.<br>
  117. Must be called only on root command.<br>
  118. **@returns** *COA.Cmd* `this` instance (for chainability)
  119. #### Cmd.usage
  120. Build full usage text for current command instance.<br>
  121. **@returns** *String* `usage` text
  122. #### Cmd.run
  123. Parse arguments from simple format like NodeJS process.argv
  124. and run ahead current program, i.e. call process.exit when all actions done.<br>
  125. **@param** *Array* `argv`<br>
  126. **@returns** *COA.Cmd* `this` instance (for chainability)
  127. #### Cmd.invoke
  128. Invoke specified (or current) command using provided options and arguments.<br>
  129. **@param** *String|Array* `cmds` subcommand to invoke (optional)<br>
  130. **@param** *Object* `opts` command options (optional)<br>
  131. **@param** *Object* `args` command arguments (optional)<br>
  132. **@returns** *Q.Promise*
  133. #### Cmd.reject
  134. Return reject of actions results promise.<br>
  135. Use in .act() for return with error.<br>
  136. **@param** *Object* `reason` reject reason<br>
  137. You can customize toString() method and exitCode property
  138. of reason object.<br>
  139. **@returns** *Q.promise* rejected promise
  140. #### Cmd.end
  141. Finish chain for current subcommand and return parent command instance.<br>
  142. **@returns** *COA.Cmd* `parent` command
  143. ### Opt
  144. Option is a named entity. Options may have short and long keys for use from command line.<br>
  145. **@namespace**<br>
  146. **@class** Presents option
  147. #### Opt.name
  148. Set a canonical option identifier to be used anywhere in the API.<br>
  149. **@param** *String* `_name` option name<br>
  150. **@returns** *COA.Opt* `this` instance (for chainability)
  151. #### Opt.title
  152. Set a long description for option to be used anywhere in text messages.<br>
  153. **@param** *String* `_title` option title<br>
  154. **@returns** *COA.Opt* `this` instance (for chainability)
  155. #### Opt.short
  156. Set a short key for option to be used with one hyphen from command line.<br>
  157. **@param** *String* `_short`<br>
  158. **@returns** *COA.Opt* `this` instance (for chainability)
  159. #### Opt.long
  160. Set a short key for option to be used with double hyphens from command line.<br>
  161. **@param** *String* `_long`<br>
  162. **@returns** *COA.Opt* `this` instance (for chainability)
  163. #### Opt.flag
  164. Make an option boolean, i.e. option without value.<br>
  165. **@returns** *COA.Opt* `this` instance (for chainability)
  166. #### Opt.arr
  167. Makes an option accepts multiple values.<br>
  168. Otherwise, the value will be used by the latter passed.<br>
  169. **@returns** *COA.Opt* `this` instance (for chainability)
  170. #### Opt.req
  171. Makes an option req.<br>
  172. **@returns** *COA.Opt* `this` instance (for chainability)
  173. #### Opt.only
  174. Makes an option to act as a command,
  175. i.e. program will exit just after option action.<br>
  176. **@returns** *COA.Opt* `this` instance (for chainability)
  177. #### Opt.val
  178. Set a validation (or value) function for argument.<br>
  179. Value from command line passes through before becoming available from API.<br>
  180. Using for validation and convertion simple types to any values.<br>
  181. **@param** *Function* `_val` validating function,
  182. invoked in the context of option instance
  183. and has one parameter with value from command line<br>
  184. **@returns** *COA.Opt* `this` instance (for chainability)
  185. #### Opt.def
  186. Set a default value for option.
  187. Default value passed through validation function as ordinary value.<br>
  188. **@param** *Object* `_def`<br>
  189. **@returns** *COA.Opt* `this` instance (for chainability)
  190. #### Opt.input
  191. Make option value inputting stream.
  192. It's add useful validation and shortcut for STDIN.
  193. **@returns** *{COA.Opt}* `this` instance (for chainability)
  194. #### Opt.output
  195. Make option value outputing stream.<br>
  196. It's add useful validation and shortcut for STDOUT.<br>
  197. **@returns** *COA.Opt* `this` instance (for chainability)
  198. #### Opt.act
  199. Add action for current option command.
  200. This action is performed if the current option
  201. is present in parsed options (with any value).<br>
  202. **@param** *Function* `act` action function,
  203. invoked in the context of command instance
  204. and has the parameters:<br>
  205. - *Object* `opts` parsed options<br>
  206. - *Array* `args` parsed arguments<br>
  207. - *Object* `res` actions result accumulator<br>
  208. It can return rejected promise by Cmd.reject (in case of error)
  209. or any other value treated as result.<br>
  210. **@returns** *COA.Opt* `this` instance (for chainability)
  211. #### Opt.comp
  212. Set custom additional completion for current option.<br>
  213. **@param** *Function* `fn` completion generation function,
  214. invoked in the context of command instance.
  215. Accepts parameters:<br>
  216. - *Object* `opts` completion options<br>
  217. It can return promise or any other value treated as result.<br>
  218. **@returns** *COA.Opt* `this` instance (for chainability)
  219. #### Opt.end
  220. Finish chain for current option and return parent command instance.<br>
  221. **@returns** *COA.Cmd* `parent` command
  222. ### Arg
  223. Argument is a unnamed entity.<br>
  224. From command line arguments passed as list of unnamed values.
  225. #### Arg.name
  226. Set a canonical argument identifier to be used anywhere in text messages.<br>
  227. **@param** *String* `_name` argument name<br>
  228. **@returns** *COA.Arg* `this` instance (for chainability)
  229. #### Arg.title
  230. Set a long description for argument to be used anywhere in text messages.<br>
  231. **@param** *String* `_title` argument title<br>
  232. **@returns** *COA.Arg* `this` instance (for chainability)
  233. #### Arg.arr
  234. Makes an argument accepts multiple values.<br>
  235. Otherwise, the value will be used by the latter passed.<br>
  236. **@returns** *COA.Arg* `this` instance (for chainability)
  237. #### Arg.req
  238. Makes an argument req.<br>
  239. **@returns** *COA.Arg* `this` instance (for chainability)
  240. #### Arg.val
  241. Set a validation (or value) function for argument.<br>
  242. Value from command line passes through before becoming available from API.<br>
  243. Using for validation and convertion simple types to any values.<br>
  244. **@param** *Function* `_val` validating function,
  245. invoked in the context of argument instance
  246. and has one parameter with value from command line<br>
  247. **@returns** *COA.Arg* `this` instance (for chainability)
  248. #### Arg.def
  249. Set a default value for argument.
  250. Default value passed through validation function as ordinary value.<br>
  251. **@param** *Object* `_def`<br>
  252. **@returns** *COA.Arg* `this` instance (for chainability)
  253. #### Arg.output
  254. Make argument value outputing stream.<br>
  255. It's add useful validation and shortcut for STDOUT.<br>
  256. **@returns** *COA.Arg* `this` instance (for chainability)
  257. #### Arg.comp
  258. Set custom additional completion for current argument.<br>
  259. **@param** *Function* `fn` completion generation function,
  260. invoked in the context of command instance.
  261. Accepts parameters:<br>
  262. - *Object* `opts` completion options<br>
  263. It can return promise or any other value treated as result.<br>
  264. **@returns** *COA.Arg* `this` instance (for chainability)
  265. #### Arg.end
  266. Finish chain for current option and return parent command instance.<br>
  267. **@returns** *COA.Cmd* `parent` command