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

Commit

Permalink
Merge pull request #19 from calmh/slightly-tidy-usage-output
Browse files Browse the repository at this point in the history
Capitalize and highlight optional args.
  • Loading branch information
harthur committed Jun 23, 2012
2 parents 7a01c17 + d8079c5 commit f7346ad
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
17 changes: 11 additions & 6 deletions nomnom.js
Expand Up @@ -253,7 +253,7 @@ ArgParser.prototype = {
// exit if required arg isn't present
this.specs.forEach(function(opt) {
if (opt.required && options[opt.name] === undefined) {
this.print(opt.name + " argument is required\n\n" + this.getUsage(), 1);
this.print("\n" + opt.name + " argument is required\n" + this.getUsage(), 1);
}
}, this);

Expand Down Expand Up @@ -281,10 +281,10 @@ ArgParser.prototype = {
// todo: use a template
var str = "\n"
if (this._colors) {
str += "usage:".bold;
str += "Usage:".bold;
}
else {
str += "usage:";
str += "Usage:";
}
str += " " + this._script;

Expand All @@ -303,7 +303,12 @@ ArgParser.prototype = {
str += " ";
var posStr = pos.string;
if (!posStr) {
posStr = "<" + (pos.name || "arg" + pos.position) + ">";
posStr = pos.name || "arg" + pos.position;
if (pos.required) {
posStr = "<" + posStr + ">";
} else {
posStr = "[" + posStr + "]";
}
if (pos.list) {
posStr += "...";
}
Expand Down Expand Up @@ -353,10 +358,10 @@ ArgParser.prototype = {

if (options.length) {
if (this._colors) {
str += "options:".blue;
str += "Options:".blue;
}
else {
str += "options:";
str += "Options:";
}
str += "\n"

Expand Down
6 changes: 3 additions & 3 deletions test/commands.js
Expand Up @@ -98,13 +98,13 @@ exports.testUsage = function(test) {

parser = createParser();
parser.printer(function(string) {
test.equal(strip(string), "usage:testrun[options]options:--debug--filefiletorunrunall");
test.equal(strip(string), "Usage:testrun[options]Options:--debug--filefiletorunrunall");
});
parser.parse(["run", "-h"]);

parser = createParser();
parser.printer(function(string) {
test.equal(strip(string), "usage:test<command>[options]commandoneof:run,testoptions:--debug--verbosenocommand");
test.equal(strip(string), "Usage:test[command][options]commandoneof:run,testOptions:--debug--verbosenocommand");
});
parser.parse(["-h"]);

Expand All @@ -117,4 +117,4 @@ exports.testUsage = function(test) {
parser.parse(["-h"]);

test.done();
}
}
2 changes: 1 addition & 1 deletion test/expected.js
Expand Up @@ -33,7 +33,7 @@ exports.testRequired = function(test) {
}
})
.printer(function(string) {
test.equal(0, string.indexOf("file argument is required"))
test.equal(0, string.trim().indexOf("file argument is required"))
test.done();
})
.parse([]);
Expand Down
28 changes: 25 additions & 3 deletions test/usage.js
Expand Up @@ -30,7 +30,7 @@ var opts = {

var parser = nomnom().options(opts).help("all the best foods").scriptName("test");

var expected = "usage:test<egg>[options]eggrobinoptions:-a,--applehowmanyapples--b-nana-cNUM,--carrots=NUM--dillPICKLEallthebestfoods"
var expected = "Usage:test[egg][options]eggrobinOptions:-a,--applehowmanyapples--b-nana-cNUM,--carrots=NUM--dillPICKLEallthebestfoods"

exports.testH = function(test) {
test.expect(1);
Expand Down Expand Up @@ -58,7 +58,7 @@ exports.testScriptName = function(test) {
nomnom()
.script("test")
.printer(function(string) {
test.equal(strip(string),"usage:test")
test.equal(strip(string),"Usage:test")
test.done();
})
.parse(["-h"]);
Expand Down Expand Up @@ -86,7 +86,29 @@ exports.testHidden = function(test) {
})
.scriptName("test")
.printer(function(string) {
test.equal(strip("usage:test[options]options:"), strip(string))
test.equal(strip("Usage:test[options]Options:"), strip(string))
test.done();
})
.parse(["-h"]);
}

exports.testRequiredOptional = function(test) {
test.expect(1);

nomnom().options({
foo: {
position: 0,
required: true,
help: 'The foo'
},
bar: {
position: 1,
help: 'The bar'
}
})
.scriptName("test")
.printer(function(string) {
test.equal(strip("Usage:test<foo>[bar]fooThefoobarThebar"), strip(string))
test.done();
})
.parse(["-h"]);
Expand Down

0 comments on commit f7346ad

Please sign in to comment.