diff --git a/nomnom.js b/nomnom.js index bcd6460..6d0bfd9 100644 --- a/nomnom.js +++ b/nomnom.js @@ -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); @@ -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; @@ -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 += "..."; } @@ -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" diff --git a/test/commands.js b/test/commands.js index 59e52f8..cc80f7a 100644 --- a/test/commands.js +++ b/test/commands.js @@ -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[options]commandoneof:run,testoptions:--debug--verbosenocommand"); + test.equal(strip(string), "Usage:test[command][options]commandoneof:run,testOptions:--debug--verbosenocommand"); }); parser.parse(["-h"]); @@ -117,4 +117,4 @@ exports.testUsage = function(test) { parser.parse(["-h"]); test.done(); -} \ No newline at end of file +} diff --git a/test/expected.js b/test/expected.js index ac1955c..e964aa5 100644 --- a/test/expected.js +++ b/test/expected.js @@ -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([]); diff --git a/test/usage.js b/test/usage.js index 4a0687e..1749e65 100644 --- a/test/usage.js +++ b/test/usage.js @@ -30,7 +30,7 @@ var opts = { var parser = nomnom().options(opts).help("all the best foods").scriptName("test"); -var expected = "usage:test[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); @@ -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"]); @@ -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[bar]fooThefoobarThebar"), strip(string)) test.done(); }) .parse(["-h"]);