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

Commit

Permalink
colored output by default, .nocolors() to turn off
Browse files Browse the repository at this point in the history
  • Loading branch information
harthur committed Mar 17, 2013
1 parent 2909cd7 commit a4ff730
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
6 changes: 1 addition & 5 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 15 additions & 8 deletions nomnom.js
Expand Up @@ -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;
},

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -283,7 +290,7 @@ ArgParser.prototype = {

// todo: use a template
var str = "\n"
if (this._colors) {
if (!this._nocolors) {
str += "Usage:".bold;
}
else {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -360,7 +367,7 @@ ArgParser.prototype = {
}

if (options.length) {
if (this._colors) {
if (!this._nocolors) {
str += "Options:".blue;
}
else {
Expand All @@ -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";
}
Expand Down
20 changes: 10 additions & 10 deletions test/commands.js
@@ -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) {
Expand Down Expand Up @@ -38,7 +38,7 @@ exports.testNoCommand = function(test) {
test.expect(2);

var parser = nomnom();

parser.nocommand()
.options({
version: {
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
11 changes: 6 additions & 5 deletions test/expected.js
Expand Up @@ -4,7 +4,7 @@ var opts = {
file: {
position: 0,
required: true
}
}
}

var parser = nomnom().options(opts);
Expand All @@ -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();
})
Expand All @@ -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([]);
}

Expand All @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion test/usage.js
Expand Up @@ -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"

Expand All @@ -39,6 +39,7 @@ exports.testH = function(test) {
test.equal(strip(string), expected)
test.done();
})
.nocolors()
.parse(["-h"]);
}

Expand All @@ -49,6 +50,7 @@ exports.testHelp = function(test) {
test.equal(strip(string), expected)
test.done();
})
.nocolors()
.parse(["--help"]);
}

Expand All @@ -61,6 +63,7 @@ exports.testScriptName = function(test) {
test.equal(strip(string),"Usage:test")
test.done();
})
.nocolors()
.parse(["-h"]);
}

Expand All @@ -73,6 +76,7 @@ exports.testUsage = function(test) {
test.equal(string, "test usage")
test.done();
})
.nocolors()
.parse(["--help"]);
}

Expand All @@ -89,6 +93,7 @@ exports.testHidden = function(test) {
test.equal(strip("Usage:test[options]Options:"), strip(string))
test.done();
})
.nocolors()
.parse(["-h"]);
}

Expand All @@ -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.