argument.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.endsWith('...')) {
  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. _collectValue(value, previous) {
  49. if (previous === this.defaultValue || !Array.isArray(previous)) {
  50. return [value];
  51. }
  52. previous.push(value);
  53. return previous;
  54. }
  55. /**
  56. * Set the default value, and optionally supply the description to be displayed in the help.
  57. *
  58. * @param {*} value
  59. * @param {string} [description]
  60. * @return {Argument}
  61. */
  62. default(value, description) {
  63. this.defaultValue = value;
  64. this.defaultValueDescription = description;
  65. return this;
  66. }
  67. /**
  68. * Set the custom handler for processing CLI command arguments into argument values.
  69. *
  70. * @param {Function} [fn]
  71. * @return {Argument}
  72. */
  73. argParser(fn) {
  74. this.parseArg = fn;
  75. return this;
  76. }
  77. /**
  78. * Only allow argument value to be one of choices.
  79. *
  80. * @param {string[]} values
  81. * @return {Argument}
  82. */
  83. choices(values) {
  84. this.argChoices = values.slice();
  85. this.parseArg = (arg, previous) => {
  86. if (!this.argChoices.includes(arg)) {
  87. throw new InvalidArgumentError(
  88. `Allowed choices are ${this.argChoices.join(', ')}.`,
  89. );
  90. }
  91. if (this.variadic) {
  92. return this._collectValue(arg, previous);
  93. }
  94. return arg;
  95. };
  96. return this;
  97. }
  98. /**
  99. * Make argument required.
  100. *
  101. * @returns {Argument}
  102. */
  103. argRequired() {
  104. this.required = true;
  105. return this;
  106. }
  107. /**
  108. * Make argument optional.
  109. *
  110. * @returns {Argument}
  111. */
  112. argOptional() {
  113. this.required = false;
  114. return this;
  115. }
  116. }
  117. /**
  118. * Takes an argument and returns its human readable equivalent for help usage.
  119. *
  120. * @param {Argument} arg
  121. * @return {string}
  122. * @private
  123. */
  124. function humanReadableArgName(arg) {
  125. const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');
  126. return arg.required ? '<' + nameOutput + '>' : '[' + nameOutput + ']';
  127. }
  128. exports.Argument = Argument;
  129. exports.humanReadableArgName = humanReadableArgName;