Anonymous avatar Anonymous committed fb376e6

Added 2 column help string generator.

Comments (0)

Files changed (1)

 ***************************************************************************/
 
 var puts        = require('sys').puts
+  , path        = require('path')
   , values      = {}
   , args        = {}
   , argv        = []
 };
 
 /**
- * Print the help message and exit
+ * Print the help message
  */
 exports.help = function () {
   puts(helpString());
-  process.exit(0);
 };
 
 
-// Create the help string
+function fmt(s, len, indent) {
+	var a = s.split(/\s/);
+	var t = [];
+	var l = 0;
+
+	var tmp = '';
+	for (j=0; j<indent; ++j) tmp += ' ';
+	
+	for (i in a) {
+		t.push(a[i]);
+		l += a[i].length + 1;
+
+		if (l >= len) {
+			t.pop();
+			t.push("\n" + tmp + a[i])
+			l = a[i].length + 1;
+		}
+	}
+
+	return t.join(' ');
+}
+
+function optdraw(o, desc) {
+	var olen = 29;
+	var desclen = 50;
+	var t = ' ';
+	var r = '';
+
+	if (o.length > olen) {
+		t = "\n";
+		for (j=0; j<olen+1; ++j) t += ' ';
+	} else {
+		if (o.length < olen) for (i=0; i<olen-o.length; ++i) t += ' ';
+	}
+	r = o + t;
+
+	return r + fmt(desc, desclen, olen+1);
+}
+
+/**
+ * Create the help string.
+ */
 var helpString = function () {
-  var str = 'Usage: ' + process.argv[0] + ' ' + process.argv[1];
-  if (descriptors.opts.length) str += ' [options]';
+  var str = 'Usage: ' + path.basename(process.argv[1]);
+  if (descriptors.opts.length) str += " [options]";
   if (descriptors.args.length) {
     for (var i=0; i<descriptors.args.length; i++) {
       if (descriptors.args[i].required) {
       }
     }
   }
-  str += '\n';
   for (var i=0; i<descriptors.opts.length; i++) {
-    var opt = descriptors.opts[i];
-    if (opt.description) str += (opt.description) + '\n';
+	var opt = descriptors.opts[i];
+	
     var line = '';
     if (opt.short && !opt.long) line += '-' + opt.short;
     else if (opt.long && !opt.short) line += '--' + opt.long;
     else line += '-' + opt.short + ', --' + opt.long;
     if (opt.value) line += ' <value>';
-    if (opt.required) line += ' (required)';
-    str += '    ' + line + '\n';
+    if (opt.required) line += ' (req)';
+    str += "\n\n" + optdraw(line, (opt.description ? opt.description : ""));
   }
   return str;
 };
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.