1 // Generated by LiveScript 1.3.1
3 var ref$, id, find, sort, min, max, map, unlines, nameToRaw, dasherize, 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;
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, typeString, that, wrap;
9 mainName = option.option, shortNames = (ref$ = option.shortNames) != null
11 : [], longNames = (ref$ = option.longNames) != null
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;
18 longNames = map(function(it){
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 typeString = (that = option['enum']) ? "One of: " + that.join(', ') : type;
31 if (maxWidth != null && !option.boolean && initialIndent + namesStringLen + typeSeparatorStringLen + typeString.length > maxWidth) {
32 wrap = wordwrap(initialIndent + namesStringLen + typeSeparatorStringLen, maxWidth);
33 return namesString + "" + typeSeparatorString + wrap(typeString).replace(/^\s+/, '');
35 return namesString + "" + (option.boolean
37 : typeSeparatorString + "" + typeString);
40 setHelpStyleDefaults = function(helpStyle){
41 helpStyle.aliasSeparator == null && (helpStyle.aliasSeparator = ', ');
42 helpStyle.typeSeparator == null && (helpStyle.typeSeparator = ' ');
43 helpStyle.descriptionSeparator == null && (helpStyle.descriptionSeparator = ' ');
44 helpStyle.initialIndent == null && (helpStyle.initialIndent = 2);
45 helpStyle.secondaryIndent == null && (helpStyle.secondaryIndent = 4);
46 helpStyle.maxPadFactor == null && (helpStyle.maxPadFactor = 1.5);
48 generateHelpForOption = function(getOption, arg$){
49 var stdout, helpStyle, ref$;
50 stdout = arg$.stdout, helpStyle = (ref$ = arg$.helpStyle) != null
53 setHelpStyleDefaults(helpStyle);
54 return function(optionName){
55 var maxWidth, wrap, option, e, pre, defaultString, restPositionalString, description, fullDescription, that, preDescription, descriptionString, exampleString, examples, seperator;
56 maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null;
57 wrap = maxWidth ? wordwrap(maxWidth) : id;
59 option = getOption(dasherize(optionName));
64 pre = getPreText(option, helpStyle);
65 defaultString = option['default'] && !option.negateName ? "\ndefault: " + option['default'] : '';
66 restPositionalString = option.restPositional ? 'Everything after this option is considered a positional argument, even if it looks like an option.' : '';
67 description = option.longDescription || option.description && sentencize(option.description);
68 fullDescription = description && restPositionalString
69 ? description + " " + restPositionalString
70 : (that = description || restPositionalString) ? that : '';
71 preDescription = 'description:';
72 descriptionString = !fullDescription
74 : maxWidth && fullDescription.length - 1 - preDescription.length > maxWidth
75 ? "\n" + preDescription + "\n" + wrap(fullDescription)
76 : "\n" + preDescription + " " + fullDescription;
77 exampleString = (that = option.example) ? (examples = [].concat(that), examples.length > 1
78 ? "\nexamples:\n" + unlines(examples)
79 : "\nexample: " + examples[0]) : '';
80 seperator = defaultString || descriptionString || exampleString ? "\n" + repeatString$('=', pre.length) : '';
81 return pre + "" + seperator + defaultString + descriptionString + exampleString;
84 generateHelp = function(arg$){
85 var options, prepend, append, helpStyle, ref$, stdout, aliasSeparator, typeSeparator, descriptionSeparator, maxPadFactor, initialIndent, secondaryIndent;
86 options = arg$.options, prepend = arg$.prepend, append = arg$.append, helpStyle = (ref$ = arg$.helpStyle) != null
88 : {}, stdout = arg$.stdout;
89 setHelpStyleDefaults(helpStyle);
90 aliasSeparator = helpStyle.aliasSeparator, typeSeparator = helpStyle.typeSeparator, descriptionSeparator = helpStyle.descriptionSeparator, maxPadFactor = helpStyle.maxPadFactor, initialIndent = helpStyle.initialIndent, secondaryIndent = helpStyle.secondaryIndent;
91 return function(arg$){
92 var ref$, showHidden, interpolate, maxWidth, output, out, data, optionCount, totalPreLen, preLens, i$, len$, item, that, pre, desc, preLen, sortedPreLens, maxPreLen, preLenMean, x, padAmount, descSepLen, fullWrapCount, partialWrapCount, descLen, totalLen, initialSpace, wrapAllFull, i, wrap;
95 : {}, showHidden = ref$.showHidden, interpolate = ref$.interpolate;
96 maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null;
99 return output.push(it != null ? it : '');
102 out(interpolate ? interp(prepend, interpolate) : prepend);
109 for (i$ = 0, len$ = (ref$ = options).length; i$ < len$; ++i$) {
111 if (showHidden || !item.hidden) {
112 if (that = item.heading) {
118 pre = getPreText(item, helpStyle, maxWidth);
119 desc = item['default'] && !item.negateName
120 ? (that = item.description) != null
121 ? that + " - default: " + item['default']
122 : "default: " + item['default']
123 : (that = item.description) != null ? that : '';
132 totalPreLen += preLen;
133 preLens.push(preLen);
137 sortedPreLens = sort(preLens);
138 maxPreLen = sortedPreLens[sortedPreLens.length - 1];
139 preLenMean = initialIndent + totalPreLen / optionCount;
140 x = optionCount > 2 ? min(preLenMean * maxPadFactor, maxPreLen) : maxPreLen;
141 for (i$ = sortedPreLens.length - 1; i$ >= 0; --i$) {
142 preLen = sortedPreLens[i$];
148 descSepLen = descriptionSeparator.length;
149 if (maxWidth != null) {
151 partialWrapCount = 0;
152 for (i$ = 0, len$ = data.length; i$ < len$; ++i$) {
154 if (item.type === 'option') {
155 pre = item.pre, desc = item.desc, descLen = item.descLen;
159 preLen = max(padAmount, pre.length) + initialIndent + descSepLen;
160 totalLen = preLen + descLen;
161 if (totalLen > maxWidth) {
162 if (descLen / 2.5 > maxWidth - preLen) {
167 item.wrap = 'partial';
176 initialSpace = repeatString$(' ', initialIndent);
177 wrapAllFull = optionCount > 1 && fullWrapCount + partialWrapCount * 0.5 > optionCount * 0.5;
178 for (i$ = 0, len$ = data.length; i$ < len$; ++i$) {
181 if (item.type === 'heading') {
185 out(item.value + ":");
187 pre = item.pre, desc = item.desc, descLen = item.descLen, wrap = item.wrap;
188 if (maxWidth != null) {
189 if (wrapAllFull || wrap === 'full') {
190 wrap = wordwrap(initialIndent + secondaryIndent, maxWidth);
191 out(initialSpace + "" + pre + "\n" + wrap(desc));
193 } else if (wrap === 'partial') {
194 wrap = wordwrap(initialIndent + descSepLen + max(padAmount, pre.length), maxWidth);
195 out(initialSpace + "" + pad(pre, padAmount) + descriptionSeparator + wrap(desc).replace(/^\s+/, ''));
200 out(initialSpace + "" + pre);
202 out(initialSpace + "" + pad(pre, padAmount) + descriptionSeparator + desc);
208 out(interpolate ? interp(append, interpolate) : append);
210 return unlines(output);
213 function pad(str, num){
216 padAmount = num - len;
217 return str + "" + repeatString$(' ', padAmount > 0 ? padAmount : 0);
219 function sentencize(str){
220 var first, rest, period;
221 first = str.charAt(0).toUpperCase();
223 period = /[\.!\?]$/.test(str) ? '' : '.';
224 return first + "" + rest + period;
226 function interp(string, object){
227 return string.replace(/{{([a-zA-Z$_][a-zA-Z$_0-9]*)}}/g, function(arg$, key){
229 return (ref$ = object[key]) != null
235 generateHelp: generateHelp,
236 generateHelpForOption: generateHelpForOption
238 function repeatString$(str, n){
239 for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;