argument.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const { InvalidArgumentError } = require('./error.js');
  2. class Argument {
  3. /**
  4. * Initialize a new command argument with the given name and description.
  5. * The default is that the argument is required, and you can explicitly
  6. * indicate this with <> around the name. Put [] around the name for an optional argument.
  7. *
  8. * @param {string} name
  9. * @param {string} [description]
  10. */
  11. constructor(name, description) {
  12. this.description = description || '';
  13. this.variadic = false;
  14. this.parseArg = undefined;
  15. this.defaultValue = undefined;
  16. this.defaultValueDescription = undefined;
  17. this.argChoices = undefined;
  18. switch (name[0]) {
  19. case '<': // e.g. <required>
  20. this.required = true;
  21. this._name = name.slice(1, -1);
  22. break;
  23. case '[': // e.g. [optional]
  24. this.required = false;
  25. this._name = name.slice(1, -1);
  26. break;
  27. default:
  28. this.required = true;
  29. this._name = name;
  30. break;
  31. }
  32. if (this._name.length > 3 && this._name.slice(-3) === '...') {
  33. this.variadic = true;
  34. this._name = this._name.slice(0, -3);
  35. }
  36. }
  37. /**
  38. * Return argument name.
  39. *
  40. * @return {string}
  41. */
  42. name() {
  43. return this._name;
  44. }
  45. /**
  46. * @package
  47. */
  48. _concatValue(value, previous) {
  49. if (previous === this.defaultValue || !Array.isArray(previous)) {
  50. return [value];
  51. }
  52. return previous.concat(value);
  53. }
  54. /**
  55. * Set the default value, and optionally supply the description to be displayed in the help.
  56. *
  57. * @param {*} value
  58. * @param {string} [description]
  59. * @return {Argument}
  60. */
  61. default(value, description) {
  62. this.defaultValue = value;
  63. this.defaultValueDescription = description;
  64. return this;
  65. }
  66. /**
  67. * Set the custom handler for processing CLI command arguments into argument values.
  68. *
  69. * @param {Function} [fn]
  70. * @return {Argument}
  71. */
  72. argParser(fn) {
  73. this.parseArg = fn;
  74. return this;
  75. }
  76. /**
  77. * Only allow argument value to be one of choices.
  78. *
  79. * @param {string[]} values
  80. * @return {Argument}
  81. */
  82. choices(values) {
  83. this.argChoices = values.slice();
  84. this.parseArg = (arg, previous) => {
  85. if (!this.argChoices.includes(arg)) {
  86. throw new InvalidArgumentError(
  87. `Allowed choices are ${this.argChoices.join(', ')}.`,
  88. );
  89. }
  90. if (this.variadic) {
  91. return this._concatValue(arg, previous);
  92. }
  93. return arg;
  94. };
  95. return this;
  96. }
  97. /**
  98. * Make argument required.
  99. *
  100. * @returns {Argument}
  101. */
  102. argRequired() {
  103. this.required = true;
  104. return this;
  105. }
  106. /**
  107. * Make argument optional.
  108. *
  109. * @returns {Argument}
  110. */
  111. argOptional() {
  112. this.required = false;
  113. return this;
  114. }
  115. }
  116. /**
  117. * Takes an argument and returns its human readable equivalent for help usage.
  118. *
  119. * @param {Argument} arg
  120. * @return {string}
  121. * @private
  122. */
  123. function humanReadableArgName(arg) {
  124. const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');
  125. return arg.required ? '<' + nameOutput + '>' : '[' + nameOutput + ']';
  126. }
  127. exports.Argument = Argument;
  128. exports.humanReadableArgName = humanReadableArgName;