Skip to content
Closed
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ program
.option('-s --size <size>', 'Pizza size', /^(large|medium|small)$/i, 'medium')
.option('-d --drink [drink]', 'Drink', /^(coke|pepsi|izze)$/i)
.parse(process.argv);

console.log(' size: %j', program.size);
console.log(' drink: %j', program.drink);
```
Expand Down Expand Up @@ -298,6 +298,10 @@ function make_red(txt) {
Output help information and exit immediately.
Optional callback cb allows post-processing of help text before it is displayed.

## .exitCode(code)

Set the exit code for option parsing errors. Defaults to `64`.

## Examples

```js
Expand Down Expand Up @@ -348,4 +352,3 @@ More Demos can be found in the [examples](https://github.com/tj/commander.js/tre
## License

MIT

32 changes: 23 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function Command(name) {
this._allowUnknownOption = false;
this._args = [];
this._name = name || '';
this._code = 64;
}

/**
Expand Down Expand Up @@ -266,7 +267,7 @@ Command.prototype.action = function(fn) {
var parsed = self.parseOptions(unknown);

// Output help if necessary
outputHelpIfNecessary(self, parsed.unknown);
outputHelpIfNecessary(self, parsed.unknown, this._code);

// If there are still any unknown options, then we simply
// die, unless someone asked for help, in which case we give it
Expand Down Expand Up @@ -617,7 +618,7 @@ Command.prototype.parseArgs = function(args, unknown) {
this.emit('*', args);
}
} else {
outputHelpIfNecessary(this, unknown);
outputHelpIfNecessary(this, unknown, this._code);

// If there were no args and we have unknown options,
// then they are extraneous and we need to error.
Expand Down Expand Up @@ -752,7 +753,7 @@ Command.prototype.missingArgument = function(name) {
console.error();
console.error(" error: missing required argument `%s'", name);
console.error();
process.exit(1);
process.exit(this._code);
};

/**
Expand All @@ -771,7 +772,7 @@ Command.prototype.optionMissingArgument = function(option, flag) {
console.error(" error: option `%s' argument missing", option.flags);
}
console.error();
process.exit(1);
process.exit(this._code);
};

/**
Expand All @@ -786,7 +787,7 @@ Command.prototype.unknownOption = function(flag) {
console.error();
console.error(" error: unknown option `%s'", flag);
console.error();
process.exit(1);
process.exit(this._code);
};

/**
Expand Down Expand Up @@ -827,6 +828,20 @@ Command.prototype.version = function(str, flags) {
return this;
};

/**
* Set the exit code used for option parsing problems.
*
* @param {Integer} code
* @return {Command} for chaining
* @api public
*/

Command.prototype.exitCode = function(code) {
if (0 == arguments.length) return this._code;
this._code = code;
return this;
};

/**
* Set the description to `str`.
*
Expand Down Expand Up @@ -1031,7 +1046,7 @@ Command.prototype.outputHelp = function(cb) {

Command.prototype.help = function(cb) {
this.outputHelp(cb);
process.exit();
process.exit(this._code);
};

/**
Expand Down Expand Up @@ -1070,12 +1085,12 @@ function pad(str, width) {
* @api private
*/

function outputHelpIfNecessary(cmd, options) {
function outputHelpIfNecessary(cmd, options, code) {
options = options || [];
for (var i = 0; i < options.length; i++) {
if (options[i] == '--help' || options[i] == '-h') {
cmd.outputHelp();
process.exit(0);
process.exit(code);
}
}
}
Expand Down Expand Up @@ -1106,4 +1121,3 @@ function exists(file) {
return false;
}
}

18 changes: 18 additions & 0 deletions test/test.codes.help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Module dependencies.
*/

var util = require('util')
, program = require('../')
, should = require('should');

process.on('exit', function (code) {
code.should.equal(66);
process.exit(0)
});

program
.exitCode(66)
.option('-c, --cheese <type>', 'optionally specify the type of cheese');

program.parse(['node', 'test', '-h']);
18 changes: 18 additions & 0 deletions test/test.codes.unknown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Module dependencies.
*/

var util = require('util')
, program = require('../')
, should = require('should');

process.on('exit', function (code) {
code.should.equal(65);
process.exit(0)
});

program
.exitCode(65)
.option('-c, --cheese <type>', 'optionally specify the type of cheese');

program.parse(['node', 'test', '-g']);
18 changes: 18 additions & 0 deletions test/test.codes.variadic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Module dependencies.
*/

var util = require('util')
, program = require('../')
, should = require('should');

process.on('exit', function (code) {
code.should.equal(1);
process.exit(0)
});

program
.exitCode(65)
.arguments('<cmd...> [env]')
.action(function(cmd, env){});
program.parse(['node', 'test', 'foo']);
5 changes: 5 additions & 0 deletions test/test.command.nohelp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var program = require('../')
, sinon = require('sinon').sandbox.create()
, should = require('should');

process.on('exit', function (code) {
code.should.equal(64);
process.exit(0)
});

program.command('mycommand [options]', 'this is my command');

program
Expand Down
2 changes: 1 addition & 1 deletion test/test.options.args.required.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ console.error = function () {
};

process.on('exit', function (code) {
code.should.equal(1);
code.should.equal(64);
info.length.should.equal(3);
info[1].should.equal(" error: option `-c, --cheese <type>' argument missing");
process.exit(0)
Expand Down