argument.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * @api private
  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(`Allowed choices are ${this.argChoices.join(', ')}.`);
  87. }
  88. if (this.variadic) {
  89. return this._concatValue(arg, previous);
  90. }
  91. return arg;
  92. };
  93. return this;
  94. }
  95. /**
  96. * Make argument required.
  97. */
  98. argRequired() {
  99. this.required = true;
  100. return this;
  101. }
  102. /**
  103. * Make argument optional.
  104. */
  105. argOptional() {
  106. this.required = false;
  107. return this;
  108. }
  109. }
  110. /**
  111. * Takes an argument and returns its human readable equivalent for help usage.
  112. *
  113. * @param {Argument} arg
  114. * @return {string}
  115. * @api private
  116. */
  117. function humanReadableArgName(arg) {
  118. const nameOutput = arg.name() + (arg.variadic === true ? '...' : '');
  119. return arg.required
  120. ? '<' + nameOutput + '>'
  121. : '[' + nameOutput + ']';
  122. }
  123. exports.Argument = Argument;
  124. exports.humanReadableArgName = humanReadableArgName;