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

Exit with status code 1 on incorrect usage #15

Merged
merged 1 commit into from
Jan 15, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions nomnom.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ ArgParser.prototype = {
}, },


parse : function(argv) { parse : function(argv) {
this.print = this.print || function(str) { this.print = this.print || function(str, code) {
console.log(str); console.log(str + '\n');
process.exit(0); process.exit(code || 0);
}; };
this._help = this._help || ""; this._help = this._help || "";
this._script = this._script || process.argv[0] + " " this._script = this._script || process.argv[0] + " "
Expand All @@ -142,7 +142,7 @@ ArgParser.prototype = {
this.command = command; this.command = command;
} }
else if (arg) { else if (arg) {
return this.print(this._script + ": no such command '" + arg + "'"); return this.print(this._script + ": no such command '" + arg + "'", 1);
} }
else { else {
// no command but command expected e.g. 'git -v' // no command but command expected e.g. 'git -v'
Expand Down Expand Up @@ -203,7 +203,7 @@ ArgParser.prototype = {
} }
else { else {
that.print("'-" + (that.opt(last).name || last) + "'" that.print("'-" + (that.opt(last).name || last) + "'"
+ " expects a value\n\n" + that.getUsage()); + " expects a value\n\n" + that.getUsage(), 1);
} }
} }
else { else {
Expand All @@ -225,7 +225,7 @@ ArgParser.prototype = {
} }
else { else {
that.print("'--" + (that.opt(arg.full).name || arg.full) + "'" that.print("'--" + (that.opt(arg.full).name || arg.full) + "'"
+ " expects a value\n\n" + that.getUsage()); + " expects a value\n\n" + that.getUsage(), 1);
} }
} }
else { else {
Expand Down Expand Up @@ -253,7 +253,7 @@ ArgParser.prototype = {
// exit if required arg isn't present // exit if required arg isn't present
this.specs.forEach(function(opt) { this.specs.forEach(function(opt) {
if (opt.required && options[opt.name] === undefined) { if (opt.required && options[opt.name] === undefined) {
this.print(opt.name + " argument is required\n\n" + this.getUsage()); this.print(opt.name + " argument is required\n\n" + this.getUsage(), 1);
} }
}, this); }, this);


Expand Down Expand Up @@ -402,7 +402,7 @@ ArgParser.prototype.setOption = function(options, arg, value) {
var message = option.callback(value); var message = option.callback(value);


if (typeof message == "string") { if (typeof message == "string") {
this.print(message); this.print(message, 1);
} }
} }


Expand All @@ -416,7 +416,7 @@ ArgParser.prototype.setOption = function(options, arg, value) {


var name = option.name || arg; var name = option.name || arg;
if (option.choices && option.choices.indexOf(value) == -1) { if (option.choices && option.choices.indexOf(value) == -1) {
this.print(name + " must be one of: " + option.choices.join(", ")); this.print(name + " must be one of: " + option.choices.join(", "), 1);
} }


if (option.list) { if (option.list) {
Expand Down