From 1f754352125caeae9c481177902356dc172e492d Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Tue, 15 Sep 2015 15:59:05 -0600 Subject: [PATCH 1/2] Add support for exit codes. Fixes #435. Fixes #188. --- Readme.md | 7 +++++-- index.js | 32 +++++++++++++++++++++--------- test/test.codes.help.js | 18 +++++++++++++++++ test/test.codes.unknown.js | 18 +++++++++++++++++ test/test.codes.variadic.js | 18 +++++++++++++++++ test/test.options.args.required.js | 2 +- 6 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 test/test.codes.help.js create mode 100644 test/test.codes.unknown.js create mode 100644 test/test.codes.variadic.js diff --git a/Readme.md b/Readme.md index 08b9e4cb9..c647596c1 100644 --- a/Readme.md +++ b/Readme.md @@ -95,7 +95,7 @@ program .option('-s --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); ``` @@ -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 @@ -348,4 +352,3 @@ More Demos can be found in the [examples](https://github.com/tj/commander.js/tre ## License MIT - diff --git a/index.js b/index.js index 69ebe878c..6e64d7ee5 100644 --- a/index.js +++ b/index.js @@ -87,6 +87,7 @@ function Command(name) { this._allowUnknownOption = false; this._args = []; this._name = name || ''; + this._code = 64; } /** @@ -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 @@ -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. @@ -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); }; /** @@ -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); }; /** @@ -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); }; /** @@ -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`. * @@ -1031,7 +1046,7 @@ Command.prototype.outputHelp = function(cb) { Command.prototype.help = function(cb) { this.outputHelp(cb); - process.exit(); + process.exit(this._code); }; /** @@ -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); } } } @@ -1106,4 +1121,3 @@ function exists(file) { return false; } } - diff --git a/test/test.codes.help.js b/test/test.codes.help.js new file mode 100644 index 000000000..958d6981f --- /dev/null +++ b/test/test.codes.help.js @@ -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 ', 'optionally specify the type of cheese'); + +program.parse(['node', 'test', '-h']); diff --git a/test/test.codes.unknown.js b/test/test.codes.unknown.js new file mode 100644 index 000000000..260506840 --- /dev/null +++ b/test/test.codes.unknown.js @@ -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 ', 'optionally specify the type of cheese'); + +program.parse(['node', 'test', '-g']); diff --git a/test/test.codes.variadic.js b/test/test.codes.variadic.js new file mode 100644 index 000000000..e4b48dfd5 --- /dev/null +++ b/test/test.codes.variadic.js @@ -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(' [env]') + .action(function(cmd, env){}); +program.parse(['node', 'test', 'foo']); diff --git a/test/test.options.args.required.js b/test/test.options.args.required.js index f809f3f67..fced57a3b 100644 --- a/test/test.options.args.required.js +++ b/test/test.options.args.required.js @@ -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 ' argument missing"); process.exit(0) From 3b5ba5813f078b9c5f9b164416acb051730d6d2b Mon Sep 17 00:00:00 2001 From: Joe Hildebrand Date: Sat, 19 Sep 2015 12:20:29 -0600 Subject: [PATCH 2/2] Fix test --- test/test.command.nohelp.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/test.command.nohelp.js b/test/test.command.nohelp.js index f7a7f26f3..850cb1f64 100644 --- a/test/test.command.nohelp.js +++ b/test/test.command.nohelp.js @@ -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