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.

README.md 12KB

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