opt.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Generated by CoffeeScript 1.6.3
  2. var Cmd, Color, Opt, Q, fs;
  3. fs = require('fs');
  4. Q = require('q');
  5. Color = require('./color').Color;
  6. Cmd = require('./cmd').Cmd;
  7. /**
  8. Option
  9. Named entity. Options may have short and long keys for use from command line.
  10. @namespace
  11. @class Presents option
  12. */
  13. exports.Opt = Opt = (function() {
  14. /**
  15. @constructs
  16. @param {COA.Cmd} cmd parent command
  17. */
  18. function Opt(_cmd) {
  19. this._cmd = _cmd;
  20. this._cmd._opts.push(this);
  21. }
  22. /**
  23. Set a canonical option identifier to be used anywhere in the API.
  24. @param {String} _name option name
  25. @returns {COA.Opt} this instance (for chainability)
  26. */
  27. Opt.prototype.name = function(_name) {
  28. this._name = _name;
  29. return this;
  30. };
  31. /**
  32. Set a long description for option to be used anywhere in text messages.
  33. @param {String} _title option title
  34. @returns {COA.Opt} this instance (for chainability)
  35. */
  36. Opt.prototype.title = Cmd.prototype.title;
  37. /**
  38. Set a short key for option to be used with one hyphen from command line.
  39. @param {String} _short
  40. @returns {COA.Opt} this instance (for chainability)
  41. */
  42. Opt.prototype.short = function(_short) {
  43. this._short = _short;
  44. return this._cmd._optsByKey['-' + _short] = this;
  45. };
  46. /**
  47. Set a short key for option to be used with double hyphens from command line.
  48. @param {String} _long
  49. @returns {COA.Opt} this instance (for chainability)
  50. */
  51. Opt.prototype.long = function(_long) {
  52. this._long = _long;
  53. return this._cmd._optsByKey['--' + _long] = this;
  54. };
  55. /**
  56. Make an option boolean, i.e. option without value.
  57. @returns {COA.Opt} this instance (for chainability)
  58. */
  59. Opt.prototype.flag = function() {
  60. this._flag = true;
  61. return this;
  62. };
  63. /**
  64. Makes an option accepts multiple values.
  65. Otherwise, the value will be used by the latter passed.
  66. @returns {COA.Opt} this instance (for chainability)
  67. */
  68. Opt.prototype.arr = function() {
  69. this._arr = true;
  70. return this;
  71. };
  72. /**
  73. Makes an option required.
  74. @returns {COA.Opt} this instance (for chainability)
  75. */
  76. Opt.prototype.req = function() {
  77. this._req = true;
  78. return this;
  79. };
  80. /**
  81. Makes an option to act as a command,
  82. i.e. program will exit just after option action.
  83. @returns {COA.Opt} this instance (for chainability)
  84. */
  85. Opt.prototype.only = function() {
  86. this._only = true;
  87. return this;
  88. };
  89. /**
  90. Set a validation (or value) function for option.
  91. Value from command line passes through before becoming available from API.
  92. Using for validation and convertion simple types to any values.
  93. @param {Function} _val validating function,
  94. invoked in the context of option instance
  95. and has one parameter with value from command line
  96. @returns {COA.Opt} this instance (for chainability)
  97. */
  98. Opt.prototype.val = function(_val) {
  99. this._val = _val;
  100. return this;
  101. };
  102. /**
  103. Set a default value for option.
  104. Default value passed through validation function as ordinary value.
  105. @param {Object} _def
  106. @returns {COA.Opt} this instance (for chainability)
  107. */
  108. Opt.prototype.def = function(_def) {
  109. this._def = _def;
  110. return this;
  111. };
  112. /**
  113. Make option value inputting stream.
  114. It's add useful validation and shortcut for STDIN.
  115. @returns {COA.Opt} this instance (for chainability)
  116. */
  117. Opt.prototype.input = function() {
  118. process.stdin.pause();
  119. return this.def(process.stdin).val(function(v) {
  120. var s;
  121. if (typeof v === 'string') {
  122. if (v === '-') {
  123. return process.stdin;
  124. } else {
  125. s = fs.createReadStream(v, {
  126. encoding: 'utf8'
  127. });
  128. s.pause();
  129. return s;
  130. }
  131. } else {
  132. return v;
  133. }
  134. });
  135. };
  136. /**
  137. Make option value outputing stream.
  138. It's add useful validation and shortcut for STDOUT.
  139. @returns {COA.Opt} this instance (for chainability)
  140. */
  141. Opt.prototype.output = function() {
  142. return this.def(process.stdout).val(function(v) {
  143. if (typeof v === 'string') {
  144. if (v === '-') {
  145. return process.stdout;
  146. } else {
  147. return fs.createWriteStream(v, {
  148. encoding: 'utf8'
  149. });
  150. }
  151. } else {
  152. return v;
  153. }
  154. });
  155. };
  156. /**
  157. Add action for current option command.
  158. This action is performed if the current option
  159. is present in parsed options (with any value).
  160. @param {Function} act action function,
  161. invoked in the context of command instance
  162. and has the parameters:
  163. - {Object} opts parsed options
  164. - {Array} args parsed arguments
  165. - {Object} res actions result accumulator
  166. It can return rejected promise by Cmd.reject (in case of error)
  167. or any other value treated as result.
  168. @returns {COA.Opt} this instance (for chainability)
  169. */
  170. Opt.prototype.act = function(act) {
  171. var name, opt;
  172. opt = this;
  173. name = this._name;
  174. this._cmd.act(function(opts) {
  175. var res,
  176. _this = this;
  177. if (name in opts) {
  178. res = act.apply(this, arguments);
  179. if (opt._only) {
  180. return Q.when(res, function(res) {
  181. return _this.reject({
  182. toString: function() {
  183. return res.toString();
  184. },
  185. exitCode: 0
  186. });
  187. });
  188. } else {
  189. return res;
  190. }
  191. }
  192. });
  193. return this;
  194. };
  195. /**
  196. Set custom additional completion for current option.
  197. @param {Function} completion generation function,
  198. invoked in the context of option instance.
  199. Accepts parameters:
  200. - {Object} opts completion options
  201. It can return promise or any other value treated as result.
  202. @returns {COA.Opt} this instance (for chainability)
  203. */
  204. Opt.prototype.comp = Cmd.prototype.comp;
  205. Opt.prototype._saveVal = function(opts, val) {
  206. var _name;
  207. if (this._val) {
  208. val = this._val(val);
  209. }
  210. if (this._arr) {
  211. (opts[_name = this._name] || (opts[_name] = [])).push(val);
  212. } else {
  213. opts[this._name] = val;
  214. }
  215. return val;
  216. };
  217. Opt.prototype._parse = function(argv, opts) {
  218. return this._saveVal(opts, this._flag ? true : argv.shift());
  219. };
  220. Opt.prototype._checkParsed = function(opts, args) {
  221. return !opts.hasOwnProperty(this._name);
  222. };
  223. Opt.prototype._usage = function() {
  224. var nameStr, res;
  225. res = [];
  226. nameStr = this._name.toUpperCase();
  227. if (this._short) {
  228. res.push('-', Color('lgreen', this._short));
  229. if (!this._flag) {
  230. res.push(' ' + nameStr);
  231. }
  232. res.push(', ');
  233. }
  234. if (this._long) {
  235. res.push('--', Color('green', this._long));
  236. if (!this._flag) {
  237. res.push('=' + nameStr);
  238. }
  239. }
  240. res.push(' : ', this._title);
  241. if (this._req) {
  242. res.push(' ', Color('lred', '(required)'));
  243. }
  244. return res.join('');
  245. };
  246. Opt.prototype._requiredText = function() {
  247. return 'Missing required option:\n ' + this._usage();
  248. };
  249. /**
  250. Return rejected promise with error code.
  251. Use in .val() for return with error.
  252. @param {Object} reject reason
  253. You can customize toString() method and exitCode property
  254. of reason object.
  255. @returns {Q.promise} rejected promise
  256. */
  257. Opt.prototype.reject = Cmd.prototype.reject;
  258. /**
  259. Finish chain for current option and return parent command instance.
  260. @returns {COA.Cmd} parent command
  261. */
  262. Opt.prototype.end = Cmd.prototype.end;
  263. /**
  264. Apply function with arguments in context of option instance.
  265. @param {Function} fn
  266. @param {Array} args
  267. @returns {COA.Opt} this instance (for chainability)
  268. */
  269. Opt.prototype.apply = Cmd.prototype.apply;
  270. return Opt;
  271. })();