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.

coaparam.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. const fs = require('fs');
  3. const CoaObject = require('./coaobject');
  4. /**
  5. * COA Parameter
  6. *
  7. * Base class for options and arguments
  8. *
  9. * --------|-----|-----|-----
  10. * | Cmd | Opt | Arg
  11. * --------|-----|-----|-----
  12. * arr | | ✓ | ✓
  13. * req | | ✓ | ✓
  14. * val | | ✓ | ✓
  15. * def | | ✓ | ✓
  16. * input | | ✓ | ✓
  17. * output | | ✓ | ✓
  18. *
  19. * @class CoaParam
  20. * @extends CoaObject
  21. */
  22. module.exports = class CoaParam extends CoaObject {
  23. constructor(cmd) {
  24. super(cmd);
  25. this._arr = false;
  26. this._req = false;
  27. this._val = undefined;
  28. this._def = undefined;
  29. }
  30. /**
  31. * Makes a param accepts multiple values.
  32. * Otherwise, the value will be used by the latter passed.
  33. *
  34. * @returns {COA.CoaParam} - this instance (for chainability)
  35. */
  36. arr() {
  37. this._arr = true;
  38. return this;
  39. }
  40. /**
  41. * Makes a param required.
  42. *
  43. * @returns {COA.CoaParam} - this instance (for chainability)
  44. */
  45. req() {
  46. this._req = true;
  47. return this;
  48. }
  49. /**
  50. * Set a validation (or value) function for param.
  51. * Value from command line passes through before becoming available from API.
  52. * Using for validation and convertion simple types to any values.
  53. *
  54. * @param {Function} val - validating function,
  55. * invoked in the context of option instance
  56. * and has one parameter with value from command line.
  57. * @returns {COA.CoaParam} - this instance (for chainability)
  58. */
  59. val(val) {
  60. this._val = val;
  61. return this;
  62. }
  63. /**
  64. * Set a default value for param.
  65. * Default value passed through validation function as ordinary value.
  66. *
  67. * @param {*} def - default value of function generator
  68. * @returns {COA.CoaParam} - this instance (for chainability)
  69. */
  70. def(def) {
  71. this._def = def;
  72. return this;
  73. }
  74. /**
  75. * Make option value inputting stream.
  76. * It's add useful validation and shortcut for STDIN.
  77. *
  78. * @returns {COA.CoaParam} - this instance (for chainability)
  79. */
  80. input() {
  81. process.stdin.pause();
  82. return this
  83. .def(process.stdin)
  84. .val(function(v) {
  85. if(typeof v !== 'string')
  86. return v;
  87. if(v === '-')
  88. return process.stdin;
  89. const s = fs.createReadStream(v, { encoding : 'utf8' });
  90. s.pause();
  91. return s;
  92. });
  93. }
  94. /**
  95. * Make option value outputing stream.
  96. * It's add useful validation and shortcut for STDOUT.
  97. *
  98. * @returns {COA.CoaParam} - this instance (for chainability)
  99. */
  100. output() {
  101. return this
  102. .def(process.stdout)
  103. .val(function(v) {
  104. if(typeof v !== 'string')
  105. return v;
  106. if(v === '-')
  107. return process.stdout;
  108. return fs.createWriteStream(v, { encoding : 'utf8' });
  109. });
  110. }
  111. };