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