Skip to content

Commit

Permalink
resolves tj#560 respect custom name for version option
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Jan 19, 2018
1 parent e5d8f04 commit 89e06ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -774,7 +774,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;
};
Expand Down Expand Up @@ -857,8 +857,10 @@ Command.prototype.version = function(str, flags) {
if (0 == arguments.length) 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);
});
Expand Down
20 changes: 20 additions & 0 deletions 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);
})
20 changes: 20 additions & 0 deletions 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);
})

0 comments on commit 89e06ad

Please sign in to comment.