Permalink
Browse files

colored output by default, .nocolors() to turn off

  • Loading branch information...
1 parent 2909cd7 commit a4ff7307672f167bcd5f3a6662218f6800cd66a1 @harthur committed Mar 17, 2013
Showing with 39 additions and 29 deletions.
  1. +1 −5 README.md
  2. +15 −8 nomnom.js
  3. +10 −10 test/commands.js
  4. +6 −5 test/expected.js
  5. +7 −1 test/usage.js
View
@@ -25,7 +25,7 @@ var opts = require("nomnom")
if (opts.debug)
// do stuff
```
-
+
You don't have to specify anything if you don't want to:
```javascript
@@ -263,10 +263,6 @@ 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
@@ -102,8 +102,13 @@ ArgParser.prototype = {
return this;
},
- colors : function() {
- this._colors = true;
+ colors: function() {
+ // deprecated - colors are on by default now
+ return this;
+ },
+
+ nocolors : function() {
+ this._nocolors = true;
return this;
},
@@ -255,7 +260,9 @@ ArgParser.prototype = {
// exit if required arg isn't present
this.specs.forEach(function(opt) {
if (opt.required && options[opt.name] === undefined) {
- var msg = (opt.name + " argument is required").red;
+ var msg = opt.name + " argument is required";
+ msg = this._nocolors ? msg : msg.red;
+
this.print("\n" + msg + "\n" + this.getUsage(), 1);
}
}, this);
@@ -283,7 +290,7 @@ ArgParser.prototype = {
// todo: use a template
var str = "\n"
- if (this._colors) {
+ if (!this._nocolors) {
str += "Usage:".bold;
}
else {
@@ -320,7 +327,7 @@ ArgParser.prototype = {
});
if (options.length) {
- if (this._colors) {
+ if (!this._nocolors) {
// must be a better way to do this
str += " [options]".blue;
}
@@ -347,7 +354,7 @@ ArgParser.prototype = {
positionals.forEach(function(pos) {
var posStr = pos.string || pos.name;
str += posStr + spaces(longest - posStr.length) + " ";
- if (this._colors) {
+ if (!this._nocolors) {
str += (pos.help || "").grey
}
else {
@@ -360,7 +367,7 @@ ArgParser.prototype = {
}
if (options.length) {
- if (this._colors) {
+ if (!this._nocolors) {
str += "Options:".blue;
}
else {
@@ -378,7 +385,7 @@ ArgParser.prototype = {
var defaults = (opt.default != null ? " [" + opt.default + "]" : "");
var help = opt.help ? opt.help + defaults : "";
- str += this._colors ? help.grey : help;
+ str += this._nocolors ? help: help.grey;
str += "\n";
}
View
@@ -1,7 +1,7 @@
var nomnom = require("../nomnom");
function strip(str) {
- return str.replace(/\s+/g, '');
+ return str.replace(/\s+/g, '');
}
exports.testCallback = function(test) {
@@ -38,7 +38,7 @@ exports.testNoCommand = function(test) {
test.expect(2);
var parser = nomnom();
-
+
parser.nocommand()
.options({
version: {
@@ -49,11 +49,11 @@ exports.testNoCommand = function(test) {
test.strictEqual(options.version, true);
})
.usage("fallback usage");
-
+
parser.command('run');
var options = parser.parse(["--version"]);
-
+
test.strictEqual(options.version, true);
test.done();
}
@@ -89,32 +89,32 @@ function createParser() {
exports.testUsage = function(test) {
test.expect(4);
-
+
var parser = createParser();
parser.printer(function(string) {
test.equal(strip(string), "testusage");
});
parser.parse(["test", "-h"]);
- parser = createParser();
+ parser = createParser().nocolors();
parser.printer(function(string) {
test.equal(strip(string), "Usage:testrun[options]Options:--debug--filefiletorunrunall");
});
parser.parse(["run", "-h"]);
- parser = createParser();
+ parser = createParser().nocolors();
parser.printer(function(string) {
test.equal(strip(string), "Usage:test[command][options]commandoneof:run,testOptions:--debug--verbosenocommand");
});
parser.parse(["-h"]);
-
- parser = createParser();
+
+ parser = createParser().nocolors();
parser.nocommand()
.usage("fallback");
parser.printer(function(string) {
test.equal(strip(string), "fallback");
});
parser.parse(["-h"]);
-
+
test.done();
}
View
@@ -4,7 +4,7 @@ var opts = {
file: {
position: 0,
required: true
- }
+ }
}
var parser = nomnom().options(opts);
@@ -17,7 +17,7 @@ exports.testFlag = function(test) {
position: 0,
}
})
- .printer(function(string) {
+ .printer(function(string) {
test.equal(0, string.indexOf("'--key1' expects a value"))
test.done();
})
@@ -32,10 +32,11 @@ exports.testRequired = function(test) {
required: true
}
})
- .printer(function(string) {
+ .printer(function(string) {
test.equal(0, string.trim().indexOf("file argument is required"))
test.done();
})
+ .nocolors()
.parse([]);
}
@@ -50,9 +51,9 @@ exports.testChoices = function(test) {
.printer(function(string) {
test.equal(0, string.indexOf("color must be one of: green, blue"))
});
-
+
parser.parse(['--color', 'red']);
-
+
var options = parser.parse(['--color', 'green']);
test.equal(options.color, 'green');
test.done();
View
@@ -28,7 +28,7 @@ var opts = {
}
}
-var parser = nomnom().options(opts).help("all the best foods").scriptName("test");
+var parser = nomnom().options(opts).help("all the best foods").scriptName("test").nocolors();
var expected = "Usage:test[egg][options]eggrobinOptions:-a,--applehowmanyapples--b-nana-cNUM,--carrots=NUM--dillPICKLEallthebestfoods"
@@ -39,6 +39,7 @@ exports.testH = function(test) {
test.equal(strip(string), expected)
test.done();
})
+ .nocolors()
.parse(["-h"]);
}
@@ -49,6 +50,7 @@ exports.testHelp = function(test) {
test.equal(strip(string), expected)
test.done();
})
+ .nocolors()
.parse(["--help"]);
}
@@ -61,6 +63,7 @@ exports.testScriptName = function(test) {
test.equal(strip(string),"Usage:test")
test.done();
})
+ .nocolors()
.parse(["-h"]);
}
@@ -73,6 +76,7 @@ exports.testUsage = function(test) {
test.equal(string, "test usage")
test.done();
})
+ .nocolors()
.parse(["--help"]);
}
@@ -89,6 +93,7 @@ exports.testHidden = function(test) {
test.equal(strip("Usage:test[options]Options:"), strip(string))
test.done();
})
+ .nocolors()
.parse(["-h"]);
}
@@ -111,5 +116,6 @@ exports.testRequiredOptional = function(test) {
test.equal(strip("Usage:test<foo>[bar]fooThefoobarThebar"), strip(string))
test.done();
})
+ .nocolors()
.parse(["-h"]);
}

0 comments on commit a4ff730

Please sign in to comment.