opt.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. 'use strict';
  2. const
  3. Q = require('q'),
  4. CoaParam = require('./coaparam'),
  5. chalk = require('chalk');
  6. /**
  7. * Option
  8. *
  9. * Named entity. Options may have short and long keys for use from command line.
  10. *
  11. * @namespace
  12. * @class Opt
  13. * @extends CoaParam
  14. */
  15. module.exports = class Opt extends CoaParam {
  16. /**
  17. * @constructs
  18. * @param {COA.Cmd} cmd - parent command
  19. */
  20. constructor(cmd) {
  21. super(cmd);
  22. this._short = null;
  23. this._long = null;
  24. this._flag = false;
  25. this._only = false;
  26. this._cmd._opts.push(this);
  27. }
  28. /**
  29. * Set a short key for option to be used with one hyphen from command line.
  30. *
  31. * @param {String} short - short name
  32. * @returns {COA.Opt} - this instance (for chainability)
  33. */
  34. short(short) {
  35. this._short = short;
  36. this._cmd._optsByKey[`-${short}`] = this;
  37. return this;
  38. }
  39. /**
  40. * Set a short key for option to be used with double hyphens from command line.
  41. *
  42. * @param {String} long - long name
  43. * @returns {COA.Opt} - this instance (for chainability)
  44. */
  45. long(long) {
  46. this._long = long;
  47. this._cmd._optsByKey[`--${long}`] = this;
  48. return this;
  49. }
  50. /**
  51. * Make an option boolean, i.e. option without value.
  52. *
  53. * @returns {COA.Opt} - this instance (for chainability)
  54. */
  55. flag() {
  56. this._flag = true;
  57. return this;
  58. }
  59. /**
  60. * Makes an option to act as a command,
  61. * i.e. program will exit just after option action.
  62. *
  63. * @returns {COA.Opt} - this instance (for chainability)
  64. */
  65. only() {
  66. this._only = true;
  67. return this;
  68. }
  69. /**
  70. * Add action for current option command.
  71. * This action is performed if the current option
  72. * is present in parsed options (with any value).
  73. *
  74. * @param {Function} act - action function,
  75. * invoked in the context of command instance
  76. * and has the parameters:
  77. * - {Object} opts - parsed options
  78. * - {Array} args - parsed arguments
  79. * - {Object} res - actions result accumulator
  80. * It can return rejected promise by Cmd.reject (in case of error)
  81. * or any other value treated as result.
  82. * @returns {COA.Opt} - this instance (for chainability)
  83. */
  84. act(act) {
  85. // Need function here for arguments
  86. const opt = this;
  87. this._cmd.act(function(opts) {
  88. if(!opts.hasOwnProperty(opt._name)) return;
  89. const res = act.apply(this, arguments);
  90. if(!opt._only) return res;
  91. return Q.when(res, out => this.reject({
  92. toString : () => out.toString(),
  93. exitCode : 0
  94. }));
  95. });
  96. return this;
  97. }
  98. _saveVal(opts, val) {
  99. this._val && (val = this._val(val));
  100. const name = this._name;
  101. this._arr
  102. ? (opts[name] || (opts[name] = [])).push(val)
  103. : (opts[name] = val);
  104. return val;
  105. }
  106. _parse(argv, opts) {
  107. return this._saveVal(opts, this._flag ? true : argv.shift());
  108. }
  109. _checkParsed(opts) {
  110. return !opts.hasOwnProperty(this._name);
  111. }
  112. _usage() {
  113. const res = [],
  114. nameStr = this._name.toUpperCase();
  115. if(this._short) {
  116. res.push('-', chalk.greenBright(this._short));
  117. this._flag || res.push(' ' + nameStr);
  118. res.push(', ');
  119. }
  120. if(this._long) {
  121. res.push('--', chalk.green(this._long));
  122. this._flag || res.push('=' + nameStr);
  123. }
  124. res.push(' : ', this._title);
  125. this._req && res.push(' ', chalk.redBright('(required)'));
  126. return res.join('');
  127. }
  128. _requiredText() {
  129. return `Missing required option:\n ${this._usage()}`;
  130. }
  131. };