diff --git a/Readme.md b/Readme.md index 25c09af95..6a21b9009 100644 --- a/Readme.md +++ b/Readme.md @@ -79,7 +79,7 @@ program .version('0.0.1', '-v, --version') ``` -Now the command will accept the `-v` option instead of the `-V` option. +The version flags can be named anything, but the long option is required. ## Command-specific options diff --git a/index.js b/index.js index c4339a7b3..955f92907 100644 --- a/index.js +++ b/index.js @@ -772,7 +772,7 @@ Command.prototype.opts = function() { for (var i = 0; i < len; i++) { var key = this.options[i].attributeName(); - result[key] = key === 'version' ? this._version : this[key]; + result[key] = key === this._versionOptionName ? this._version : this[key]; } return result; }; @@ -855,8 +855,10 @@ Command.prototype.version = function(str, flags) { if (arguments.length === 0) return this._version; this._version = str; flags = flags || '-V, --version'; + var longOptIndex = flags.indexOf('--') + this._versionOptionName = ~longOptIndex ? flags.substr(longOptIndex + 2) : 'version' this.option(flags, 'output the version number'); - this.on('option:version', function() { + this.on('option:' + this._versionOptionName, function() { process.stdout.write(str + '\n'); process.exit(0); }); diff --git a/test/test.options.version.custom.js b/test/test.options.version.custom.js new file mode 100644 index 000000000..9b7d256f8 --- /dev/null +++ b/test/test.options.version.custom.js @@ -0,0 +1,20 @@ +var program = require('../') + , should = require('should'); + +var capturedExitCode, capturedOutput, oldProcessExit, oldProcessStdoutWrite; + +program.version('0.0.1', '-r, --revision'); + +['-r', '--revision'].forEach(function (flag) { + capturedExitCode = -1; + capturedOutput = ''; + oldProcessExit = process.exit; + oldProcessStdoutWrite = process.stdout.write; + process.exit = function (code) { capturedExitCode = code; } + process.stdout.write = function(output) { capturedOutput += output; } + program.parse(['node', 'test', flag]); + process.exit = oldProcessExit; + process.stdout.write = oldProcessStdoutWrite; + capturedOutput.should.equal('0.0.1\n'); + capturedExitCode.should.equal(0); +}) diff --git a/test/test.options.version.js b/test/test.options.version.js new file mode 100644 index 000000000..ae25e84fe --- /dev/null +++ b/test/test.options.version.js @@ -0,0 +1,20 @@ +var program = require('../') + , should = require('should'); + +var capturedExitCode, capturedOutput, oldProcessExit, oldProcessStdoutWrite; + +program.version('0.0.1'); + +['-V', '--version'].forEach(function (flag) { + capturedExitCode = -1; + capturedOutput = ''; + oldProcessExit = process.exit; + oldProcessStdoutWrite = process.stdout.write; + process.exit = function (code) { capturedExitCode = code; } + process.stdout.write = function(output) { capturedOutput += output; } + program.parse(['node', 'test', flag]); + process.exit = oldProcessExit; + process.stdout.write = oldProcessStdoutWrite; + capturedOutput.should.equal('0.0.1\n'); + capturedExitCode.should.equal(0); +})