cli.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. /**
  4. * The Cli contains the usage and the collection of programs.
  5. *
  6. * Printing help for all the programs in the following order:
  7. * usage, commands, and options. If the options are used in multiple programs,
  8. * it will list it once.
  9. */
  10. class Cli {
  11. constructor() {
  12. this.programs = {};
  13. }
  14. /**
  15. * Register a program to the command line interface.
  16. * @returns The cli for method chaining.
  17. */
  18. program(prog) {
  19. this.programs[prog.cmd] = prog;
  20. return this;
  21. }
  22. /**
  23. * Add a usage for the command line interface.
  24. * @returns The cli for method chaining.
  25. */
  26. usage(usageText) {
  27. this.usageText = usageText;
  28. return this;
  29. }
  30. /**
  31. * Prints help for the programs registered to the cli.
  32. */
  33. printHelp() {
  34. console.log('Usage: ' + this.usageText);
  35. console.log('\nCommands:');
  36. let cmdDescriptionPos = this.posCmdDescription();
  37. for (let cmd in this.programs) {
  38. let prog = this.programs[cmd];
  39. prog.printCmd(cmdDescriptionPos);
  40. }
  41. let descriptionPos = this.posDescription();
  42. let defaultPos = this.posDefault();
  43. let extOptions = {};
  44. console.log('\nOptions:');
  45. // print all options
  46. for (let cmd in this.programs) {
  47. let prog = this.programs[cmd];
  48. prog.printOptions(descriptionPos, defaultPos, extOptions);
  49. }
  50. }
  51. /**
  52. * For commands, gets the position where the description should start so they
  53. * are aligned.
  54. * @returns The position where the command description should start.
  55. */
  56. posCmdDescription() {
  57. let position = -1;
  58. for (let cmd in this.programs) {
  59. position = Math.max(position, cmd.length + 6);
  60. }
  61. return position;
  62. }
  63. /**
  64. * For options, gets the position where the description should start so they
  65. * are aligned.
  66. * @returns The position where the option description should start.
  67. */
  68. posDescription() {
  69. let position = -1;
  70. for (let cmd in this.programs) {
  71. let prog = this.programs[cmd];
  72. position = Math.max(position, prog.posDescription());
  73. }
  74. return position;
  75. }
  76. /**
  77. * For options, get the position where the default values should start so they
  78. * are aligned.
  79. * @returns The position where the option default values should start.
  80. */
  81. posDefault() {
  82. let position = -1;
  83. for (let cmd in this.programs) {
  84. let prog = this.programs[cmd];
  85. position = Math.max(position, prog.posDefault());
  86. }
  87. return position;
  88. }
  89. /**
  90. * Go through all programs and add options to the collection.
  91. * @returns The options used in the programs.
  92. */
  93. getOptions() {
  94. let allOptions = {};
  95. for (let cmd in this.programs) {
  96. let prog = this.programs[cmd];
  97. allOptions = prog.getOptions_(allOptions);
  98. }
  99. return allOptions;
  100. }
  101. /**
  102. * Get the options used by the programs and create the minimist options
  103. * to ensure that minimist parses the values properly.
  104. * @returns The options for minimist.
  105. */
  106. getMinimistOptions() {
  107. let allOptions = this.getOptions();
  108. let minimistOptions = {};
  109. let minimistBoolean = [];
  110. let minimistString = [];
  111. let minimistNumber = [];
  112. let minimistDefault = {};
  113. for (let opt in allOptions) {
  114. let option = allOptions[opt];
  115. if (option.type === 'boolean') {
  116. minimistBoolean.push(option.opt);
  117. }
  118. else if (option.type === 'string') {
  119. minimistString.push(option.opt);
  120. }
  121. else if (option.type === 'number') {
  122. minimistNumber.push(option.opt);
  123. }
  124. if (typeof option.defaultValue !== 'undefined') {
  125. minimistDefault[option.opt] = option.defaultValue;
  126. }
  127. }
  128. minimistOptions['boolean'] = minimistBoolean;
  129. minimistOptions['string'] = minimistString;
  130. minimistOptions['number'] = minimistNumber;
  131. minimistOptions['default'] = minimistDefault;
  132. return minimistOptions;
  133. }
  134. }
  135. exports.Cli = Cli;
  136. //# sourceMappingURL=cli.js.map