Skip to content

Commit

Permalink
Merge pull request #753 from mojavelinux/issue-560-version-option
Browse files Browse the repository at this point in the history
resolves #560 respect custom name for version option
  • Loading branch information
abetomo committed Jan 30, 2018
2 parents 38f41bf + 37359ee commit d5ac793
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Expand Up @@ -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

Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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);
});
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 d5ac793

Please sign in to comment.