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.

155 lines
3.8 KiB

4 years ago
  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. };