Skip to content
Browse files

colors() for coloring usage

  • Loading branch information...
1 parent 09b1bc4 commit 6c365783376d4a294ee39b7657dd30995802baf3 @harthur committed Nov 2, 2011
Showing with 67 additions and 18 deletions.
  1. +4 −0 README.md
  2. +63 −18 nomnom.js
View
4 README.md
@@ -263,6 +263,10 @@ Nomnom can't detect the alias used to run your script. You can use `script` to p
Overrides the usage printing function.
+#### colors
+
+calling `colors()` will cause usage printout to be slightly colorful.
+
#### command
Takes a command name and gives you a command object on which you can chain command options.
View
81 nomnom.js
@@ -1,4 +1,5 @@
-var _ = require("underscore")._;
+var _ = require("underscore")._,
+ colors = require("colors");
function ArgParser() {
@@ -101,6 +102,11 @@ ArgParser.prototype = {
return this;
},
+ colors : function() {
+ this._colors = true;
+ return this;
+ },
+
parseArgs : function(argv) {
// old API
return this.parse(argv);
@@ -273,7 +279,14 @@ ArgParser.prototype = {
}
// todo: use a template
- var str = "usage: " + this._script;
+ var str = "\n"
+ if (this._colors) {
+ str += "usage:".bold;
+ }
+ else {
+ str += "usage:";
+ }
+ str += " " + this._script;
var positionals = _(this.specs).select(function(opt) {
return opt.position != undefined;
@@ -298,8 +311,18 @@ ArgParser.prototype = {
str += posStr;
});
+ if (options.length) {
+ if (this._colors) {
+ // must be a better way to do this
+ str += " [options]".blue;
+ }
+ else {
+ str += " [options]";
+ }
+ }
+
if (options.length || positionals.length) {
- str += " [options]\n\n";
+ str += "\n\n";
}
function spaces(length) {
@@ -315,28 +338,50 @@ ArgParser.prototype = {
positionals.forEach(function(pos) {
var posStr = pos.string || pos.name;
- str += posStr + spaces(longest - posStr.length) + " "
- + (pos.help || "") + "\n";
- });
+ str += posStr + spaces(longest - posStr.length) + " ";
+ if (this._colors) {
+ str += (pos.help || "").grey
+ }
+ else {
+ str += (pos.help || "")
+ }
+ str += "\n";
+ }, this);
if (positionals.length && options.length) {
str += "\n";
}
if (options.length) {
- str += "options:\n";
- }
+ if (this._colors) {
+ str += "options:".blue;
+ }
+ else {
+ str += "options:";
+ }
+ str += "\n"
- var longest = options.reduce(function(max, opt) {
- return opt.string.length > max && !opt.hidden ? opt.string.length : max;
- }, 0);
+ var longest = options.reduce(function(max, opt) {
+ return opt.string.length > max && !opt.hidden ? opt.string.length : max;
+ }, 0);
- options.forEach(function(opt) {
- if (!opt.hidden) {
- str += " " + opt.string + spaces(longest - opt.string.length)
- + " " + (opt.help || "") + "\n";
- }
- });
- return str + "\n" + (this._help || "") + "\n";
+ options.forEach(function(opt) {
+ if (!opt.hidden) {
+ str += " " + opt.string + spaces(longest - opt.string.length) + " ";
+ if (this._colors) {
+ str += (opt.help || "").grey;
+ }
+ else {
+ str += (opt.help || "")
+ }
+ str += "\n";
+ }
+ }, this);
+ }
+
+ if (this._help) {
+ str += "\n" + this._help;
+ }
+ return str + "\n";
}
};

0 comments on commit 6c36578

Please sign in to comment.
Something went wrong with that request. Please try again.