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