1 // Generated by LiveScript 1.2.0
3 var ref$, id, find, sort, min, max, map, partition, 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, partition = ref$.partition, 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, aliases, ref$, type, description, aliasSeparator, typeSeparator, initialIndent, shortNames, longNames, names, namesString, namesStringLen, typeSeparatorString, typeSeparatorStringLen, typeString, that, wrap;
9 mainName = option.option, aliases = (ref$ = option.aliases) != null
11 : [], type = option.type, description = option.description;
12 aliasSeparator = arg$.aliasSeparator, typeSeparator = arg$.typeSeparator, initialIndent = arg$.initialIndent;
13 ref$ = partition(function(it){
14 return it.length === 1;
15 }, aliases), shortNames = ref$[0], longNames = ref$[1];
16 names = mainName.length === 1
17 ? [mainName].concat(shortNames, longNames)
18 : shortNames.concat([mainName], longNames);
19 namesString = map(nameToRaw, names).join(aliasSeparator);
20 namesStringLen = namesString.length;
21 typeSeparatorString = mainName === 'NUM' ? '::' : typeSeparator;
22 typeSeparatorStringLen = typeSeparatorString.length;
23 typeString = (that = option['enum']) ? "One of: " + that.join(', ') : type;
24 if (maxWidth != null && !option.boolean && initialIndent + namesStringLen + typeSeparatorStringLen + typeString.length > maxWidth) {
25 wrap = wordwrap(initialIndent + namesStringLen + typeSeparatorStringLen, maxWidth);
26 return namesString + "" + typeSeparatorString + wrap(typeString).replace(/^\s+/, '');
28 return namesString + "" + (option.boolean
30 : typeSeparatorString + "" + typeString);
33 setHelpStyleDefaults = function(helpStyle){
34 helpStyle.aliasSeparator == null && (helpStyle.aliasSeparator = ', ');
35 helpStyle.typeSeparator == null && (helpStyle.typeSeparator = ' ');
36 helpStyle.descriptionSeparator == null && (helpStyle.descriptionSeparator = ' ');
37 helpStyle.initialIndent == null && (helpStyle.initialIndent = 2);
38 helpStyle.secondaryIndent == null && (helpStyle.secondaryIndent = 4);
39 helpStyle.maxPadFactor == null && (helpStyle.maxPadFactor = 1.5);
41 generateHelpForOption = function(getOption, arg$){
42 var stdout, helpStyle, ref$;
43 stdout = arg$.stdout, helpStyle = (ref$ = arg$.helpStyle) != null
46 setHelpStyleDefaults(helpStyle);
47 return function(optionName){
48 var maxWidth, wrap, option, e, pre, defaultString, restPositionalString, description, fullDescription, that, preDescription, descriptionString, exampleString, examples, seperator;
49 maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null;
50 wrap = maxWidth ? wordwrap(maxWidth) : id;
52 option = getOption(dasherize(optionName));
57 pre = getPreText(option, helpStyle);
58 defaultString = option['default'] ? "\ndefault: " + option['default'] : '';
59 restPositionalString = option.restPositional ? 'Everything after this option is considered a positional argument, even if it looks like an option.' : '';
60 description = option.longDescription || option.description && sentencize(option.description);
61 fullDescription = description && restPositionalString
62 ? description + " " + restPositionalString
63 : (that = description || restPositionalString) ? that : '';
64 preDescription = 'description:';
65 descriptionString = !fullDescription
67 : maxWidth && fullDescription.length - 1 - preDescription.length > maxWidth
68 ? "\n" + preDescription + "\n" + wrap(fullDescription)
69 : "\n" + preDescription + " " + fullDescription;
70 exampleString = (that = option.example) ? (examples = [].concat(that), examples.length > 1
71 ? "\nexamples:\n" + unlines(examples)
72 : "\nexample: " + examples[0]) : '';
73 seperator = defaultString || descriptionString || exampleString ? "\n" + repeatString$('=', pre.length) : '';
74 return pre + "" + seperator + defaultString + descriptionString + exampleString;
77 generateHelp = function(arg$){
78 var options, prepend, append, helpStyle, ref$, stdout, aliasSeparator, typeSeparator, descriptionSeparator, maxPadFactor, initialIndent, secondaryIndent;
79 options = arg$.options, prepend = arg$.prepend, append = arg$.append, helpStyle = (ref$ = arg$.helpStyle) != null
81 : {}, stdout = arg$.stdout;
82 setHelpStyleDefaults(helpStyle);
83 aliasSeparator = helpStyle.aliasSeparator, typeSeparator = helpStyle.typeSeparator, descriptionSeparator = helpStyle.descriptionSeparator, maxPadFactor = helpStyle.maxPadFactor, initialIndent = helpStyle.initialIndent, secondaryIndent = helpStyle.secondaryIndent;
84 return function(arg$){
85 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;
88 : {}, showHidden = ref$.showHidden, interpolate = ref$.interpolate;
89 maxWidth = stdout != null && stdout.isTTY ? stdout.columns - 1 : null;
92 return output.push(it != null ? it : '');
95 out(interpolate ? interp(prepend, interpolate) : prepend);
102 for (i$ = 0, len$ = (ref$ = options).length; i$ < len$; ++i$) {
104 if (showHidden || !item.hidden) {
105 if (that = item.heading) {
111 pre = getPreText(item, helpStyle, maxWidth);
112 desc = item['default']
113 ? (that = item.description) != null
114 ? that + " - default: " + item['default']
115 : "default: " + item['default']
116 : (that = item.description) != null ? that : '';
125 totalPreLen += preLen;
126 preLens.push(preLen);
130 sortedPreLens = sort(preLens);
131 maxPreLen = sortedPreLens[sortedPreLens.length - 1];
132 preLenMean = initialIndent + totalPreLen / optionCount;
133 x = optionCount > 2 ? min(preLenMean * maxPadFactor, maxPreLen) : maxPreLen;
134 for (i$ = sortedPreLens.length - 1; i$ >= 0; --i$) {
135 preLen = sortedPreLens[i$];
141 descSepLen = descriptionSeparator.length;
142 if (maxWidth != null) {
144 partialWrapCount = 0;
145 for (i$ = 0, len$ = data.length; i$ < len$; ++i$) {
147 if (item.type === 'option') {
148 pre = item.pre, desc = item.desc, descLen = item.descLen;
152 preLen = max(padAmount, pre.length) + initialIndent + descSepLen;
153 totalLen = preLen + descLen;
154 if (totalLen > maxWidth) {
155 if (descLen / 2.5 > maxWidth - preLen) {
160 item.wrap = 'partial';
169 initialSpace = repeatString$(' ', initialIndent);
170 wrapAllFull = optionCount > 1 && fullWrapCount + partialWrapCount * 0.5 > optionCount * 0.5;
171 for (i$ = 0, len$ = data.length; i$ < len$; ++i$) {
174 if (item.type === 'heading') {
178 out(item.value + ":");
180 pre = item.pre, desc = item.desc, descLen = item.descLen, wrap = item.wrap;
181 if (maxWidth != null) {
182 if (wrapAllFull || wrap === 'full') {
183 wrap = wordwrap(initialIndent + secondaryIndent, maxWidth);
184 out(initialSpace + "" + pre + "\n" + wrap(desc));
186 } else if (wrap === 'partial') {
187 wrap = wordwrap(initialIndent + descSepLen + max(padAmount, pre.length), maxWidth);
188 out(initialSpace + "" + pad(pre, padAmount) + descriptionSeparator + wrap(desc).replace(/^\s+/, ''));
193 out(initialSpace + "" + pre);
195 out(initialSpace + "" + pad(pre, padAmount) + descriptionSeparator + desc);
201 out(interpolate ? interp(append, interpolate) : append);
203 return unlines(output);
206 function pad(str, num){
209 padAmount = num - len;
210 return str + "" + repeatString$(' ', padAmount > 0 ? padAmount : 0);
212 function sentencize(str){
213 var first, rest, period;
214 first = str.charAt(0).toUpperCase();
216 period = /[\.!\?]$/.test(str) ? '' : '.';
217 return first + "" + rest + period;
219 function interp(string, object){
220 return string.replace(/{{([a-zA-Z$_][a-zA-Z$_0-9]*)}}/g, function(arg$, key){
222 return (ref$ = object[key]) != null
228 generateHelp: generateHelp,
229 generateHelpForOption: generateHelpForOption
231 function repeatString$(str, n){
232 for (var r = ''; n > 0; (n >>= 1) && (str += str)) if (n & 1) r += str;