help.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Generated by LiveScript 1.5.0
  2. (function(){
  3. var ref$, id, find, sort, min, max, map, unlines, nameToRaw, dasherize, naturalJoin, wordwrap, getPreText, setHelpStyleDefaults, generateHelpForOption, generateHelp;
  4. ref$ = require('prelude-ls'), id = ref$.id, find = ref$.find, sort = ref$.sort, min = ref$.min, max = ref$.max, map = ref$.map, unlines = ref$.unlines;
  5. ref$ = require('./util'), nameToRaw = ref$.nameToRaw, dasherize = ref$.dasherize, naturalJoin = ref$.naturalJoin;
  6. wordwrap = require('wordwrap');
  7. getPreText = function(option, arg$, maxWidth){
  8. var mainName, shortNames, ref$, longNames, type, description, aliasSeparator, typeSeparator, initialIndent, names, namesString, namesStringLen, typeSeparatorString, typeSeparatorStringLen, wrap;
  9. mainName = option.option, shortNames = (ref$ = option.shortNames) != null
  10. ? ref$
  11. : [], longNames = (ref$ = option.longNames) != null
  12. ? ref$
  13. : [], type = option.type, description = option.description;
  14. aliasSeparator = arg$.aliasSeparator, typeSeparator = arg$.typeSeparator, initialIndent = arg$.initialIndent;
  15. if (option.negateName) {
  16. mainName = "no-" + mainName;
  17. if (longNames) {
  18. longNames = map(function(it){
  19. return "no-" + it;
  20. }, longNames);
  21. }
  22. }
  23. names = mainName.length === 1
  24. ? [mainName].concat(shortNames, longNames)
  25. : shortNames.concat([mainName], longNames);
  26. namesString = map(nameToRaw, names).join(aliasSeparator);
  27. namesStringLen = namesString.length;
  28. typeSeparatorString = mainName === 'NUM' ? '::' : typeSeparator;
  29. typeSeparatorStringLen = typeSeparatorString.length;
  30. if (maxWidth != null && !option.boolean && initialIndent + namesStringLen + typeSeparatorStringLen + type.length > maxWidth) {
  31. wrap = wordwrap(initialIndent + namesStringLen + typeSeparatorStringLen, maxWidth);
  32. return namesString + "" + typeSeparatorString + wrap(type).replace(/^\s+/, '');
  33. } else {
  34. return namesString + "" + (option.boolean
  35. ? ''
  36. : typeSeparatorString + "" + type);
  37. }
  38. };
  39. setHelpStyleDefaults = function(helpStyle){
  40. helpStyle.aliasSeparator == null && (helpStyle.aliasSeparator = ', ');
  41. helpStyle.typeSeparator == null && (helpStyle.typeSeparator = ' ');
  42. helpStyle.descriptionSeparator == null && (helpStyle.descriptionSeparator = ' ');
  43. helpStyle.initialIndent == null && (helpStyle.initialIndent = 2);
  44. helpStyle.secondaryIndent == null && (helpStyle.secondaryIndent = 4);
  45. helpStyle.maxPadFactor == null && (helpStyle.maxPadFactor = 1.5);
  46. };
  47. generateHelpForOption = function(getOption, arg$){
  48. var stdout, helpStyle, ref$;
  49. stdout = arg$.stdout, helpStyle = (ref$ = arg$.helpStyle) != null
  50. ? ref$
  51. : {};
  52. setHelpStyleDefaults(helpStyle);
  53. return function(optionName){
  54. var maxWidth, wrap, option, e, pre, defaultString, restPositionalString, description, fullDescription, that, preDescription, descriptionString, exampleString, examples, seperator;
  55. maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null;
  56. wrap = maxWidth ? wordwrap(maxWidth) : id;
  57. try {
  58. option = getOption(dasherize(optionName));
  59. } catch (e$) {
  60. e = e$;
  61. return e.message;
  62. }
  63. pre = getPreText(option, helpStyle);
  64. defaultString = option['default'] && !option.negateName ? "\ndefault: " + option['default'] : '';
  65. restPositionalString = option.restPositional ? 'Everything after this option is considered a positional argument, even if it looks like an option.' : '';
  66. description = option.longDescription || option.description && sentencize(option.description);
  67. fullDescription = description && restPositionalString
  68. ? description + " " + restPositionalString
  69. : (that = description || restPositionalString) ? that : '';
  70. preDescription = 'description:';
  71. descriptionString = !fullDescription
  72. ? ''
  73. : maxWidth && fullDescription.length - 1 - preDescription.length > maxWidth
  74. ? "\n" + preDescription + "\n" + wrap(fullDescription)
  75. : "\n" + preDescription + " " + fullDescription;
  76. exampleString = (that = option.example) ? (examples = [].concat(that), examples.length > 1
  77. ? "\nexamples:\n" + unlines(examples)
  78. : "\nexample: " + examples[0]) : '';
  79. seperator = defaultString || descriptionString || exampleString ? "\n" + repeatString$('=', pre.length) : '';
  80. return pre + "" + seperator + defaultString + descriptionString + exampleString;
  81. };
  82. };
  83. generateHelp = function(arg$){
  84. var options, prepend, append, helpStyle, ref$, stdout, aliasSeparator, typeSeparator, descriptionSeparator, maxPadFactor, initialIndent, secondaryIndent;
  85. options = arg$.options, prepend = arg$.prepend, append = arg$.append, helpStyle = (ref$ = arg$.helpStyle) != null
  86. ? ref$
  87. : {}, stdout = arg$.stdout;
  88. setHelpStyleDefaults(helpStyle);
  89. aliasSeparator = helpStyle.aliasSeparator, typeSeparator = helpStyle.typeSeparator, descriptionSeparator = helpStyle.descriptionSeparator, maxPadFactor = helpStyle.maxPadFactor, initialIndent = helpStyle.initialIndent, secondaryIndent = helpStyle.secondaryIndent;
  90. return function(arg$){
  91. var ref$, showHidden, interpolate, maxWidth, output, out, data, optionCount, totalPreLen, preLens, i$, len$, item, that, pre, descParts, desc, preLen, sortedPreLens, maxPreLen, preLenMean, x, padAmount, descSepLen, fullWrapCount, partialWrapCount, descLen, totalLen, initialSpace, wrapAllFull, i, wrap;
  92. ref$ = arg$ != null
  93. ? arg$
  94. : {}, showHidden = ref$.showHidden, interpolate = ref$.interpolate;
  95. maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null;
  96. output = [];
  97. out = function(it){
  98. return output.push(it != null ? it : '');
  99. };
  100. if (prepend) {
  101. out(interpolate ? interp(prepend, interpolate) : prepend);
  102. out();
  103. }
  104. data = [];
  105. optionCount = 0;
  106. totalPreLen = 0;
  107. preLens = [];
  108. for (i$ = 0, len$ = (ref$ = options).length; i$ < len$; ++i$) {
  109. item = ref$[i$];
  110. if (showHidden || !item.hidden) {
  111. if (that = item.heading) {
  112. data.push({
  113. type: 'heading',
  114. value: that
  115. });
  116. } else {
  117. pre = getPreText(item, helpStyle, maxWidth);
  118. descParts = [];
  119. if ((that = item.description) != null) {
  120. descParts.push(that);
  121. }
  122. if (that = item['enum']) {
  123. descParts.push("either: " + naturalJoin(that));
  124. }
  125. if (item['default'] && !item.negateName) {
  126. descParts.push("default: " + item['default']);
  127. }
  128. desc = descParts.join(' - ');
  129. data.push({
  130. type: 'option',
  131. pre: pre,
  132. desc: desc,
  133. descLen: desc.length
  134. });
  135. preLen = pre.length;
  136. optionCount++;
  137. totalPreLen += preLen;
  138. preLens.push(preLen);
  139. }
  140. }
  141. }
  142. sortedPreLens = sort(preLens);
  143. maxPreLen = sortedPreLens[sortedPreLens.length - 1];
  144. preLenMean = initialIndent + totalPreLen / optionCount;
  145. x = optionCount > 2 ? min(preLenMean * maxPadFactor, maxPreLen) : maxPreLen;
  146. for (i$ = sortedPreLens.length - 1; i$ >= 0; --i$) {
  147. preLen = sortedPreLens[i$];
  148. if (preLen <= x) {
  149. padAmount = preLen;
  150. break;
  151. }
  152. }
  153. descSepLen = descriptionSeparator.length;
  154. if (maxWidth != null) {
  155. fullWrapCount = 0;
  156. partialWrapCount = 0;
  157. for (i$ = 0, len$ = data.length; i$ < len$; ++i$) {
  158. item = data[i$];
  159. if (item.type === 'option') {
  160. pre = item.pre, desc = item.desc, descLen = item.descLen;
  161. if (descLen === 0) {
  162. item.wrap = 'none';
  163. } else {
  164. preLen = max(padAmount, pre.length) + initialIndent + descSepLen;
  165. totalLen = preLen + descLen;
  166. if (totalLen > maxWidth) {
  167. if (descLen / 2.5 > maxWidth - preLen) {
  168. fullWrapCount++;
  169. item.wrap = 'full';
  170. } else {
  171. partialWrapCount++;
  172. item.wrap = 'partial';
  173. }
  174. } else {
  175. item.wrap = 'none';
  176. }
  177. }
  178. }
  179. }
  180. }
  181. initialSpace = repeatString$(' ', initialIndent);
  182. wrapAllFull = optionCount > 1 && fullWrapCount + partialWrapCount * 0.5 > optionCount * 0.5;
  183. for (i$ = 0, len$ = data.length; i$ < len$; ++i$) {
  184. i = i$;
  185. item = data[i$];
  186. if (item.type === 'heading') {
  187. if (i !== 0) {
  188. out();
  189. }
  190. out(item.value + ":");
  191. } else {
  192. pre = item.pre, desc = item.desc, descLen = item.descLen, wrap = item.wrap;
  193. if (maxWidth != null) {
  194. if (wrapAllFull || wrap === 'full') {
  195. wrap = wordwrap(initialIndent + secondaryIndent, maxWidth);
  196. out(initialSpace + "" + pre + "\n" + wrap(desc));
  197. continue;
  198. } else if (wrap === 'partial') {
  199. wrap = wordwrap(initialIndent + descSepLen + max(padAmount, pre.length), maxWidth);
  200. out(initialSpace + "" + pad(pre, padAmount) + descriptionSeparator + wrap(desc).replace(/^\s+/, ''));
  201. continue;
  202. }
  203. }
  204. if (descLen === 0) {
  205. out(initialSpace + "" + pre);
  206. } else {
  207. out(initialSpace + "" + pad(pre, padAmount) + descriptionSeparator + desc);
  208. }
  209. }
  210. }
  211. if (append) {
  212. out();
  213. out(interpolate ? interp(append, interpolate) : append);
  214. }
  215. return unlines(output);
  216. };
  217. };
  218. function pad(str, num){
  219. var len, padAmount;
  220. len = str.length;
  221. padAmount = num - len;
  222. return str + "" + repeatString$(' ', padAmount > 0 ? padAmount : 0);
  223. }
  224. function sentencize(str){
  225. var first, rest, period;
  226. first = str.charAt(0).toUpperCase();
  227. rest = str.slice(1);
  228. period = /[\.!\?]$/.test(str) ? '' : '.';
  229. return first + "" + rest + period;
  230. }
  231. function interp(string, object){
  232. return string.replace(/{{([a-zA-Z$_][a-zA-Z$_0-9]*)}}/g, function(arg$, key){
  233. var ref$;
  234. return (ref$ = object[key]) != null
  235. ? ref$
  236. : "{{" + key + "}}";
  237. });
  238. }
  239. module.exports = {
  240. generateHelp: generateHelp,
  241. generateHelpForOption: generateHelpForOption
  242. };
  243. function repeatString$(str, n){
  244. for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;
  245. return r;
  246. }
  247. }).call(this);