Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
colors() for coloring usage
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed Nov 2, 2011
1 parent 09b1bc4 commit 6c36578
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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.
Expand Down
81 changes: 63 additions & 18 deletions nomnom.js
@@ -1,4 +1,5 @@
var _ = require("underscore")._;
var _ = require("underscore")._,
colors = require("colors");


function ArgParser() {
Expand Down Expand Up @@ -101,6 +102,11 @@ ArgParser.prototype = {
return this;
},

colors : function() {
this._colors = true;
return this;
},

parseArgs : function(argv) {
// old API
return this.parse(argv);
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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";
}
};

Expand Down

0 comments on commit 6c36578

Please sign in to comment.