Skip to content

Commit d5ac793

Browse files
authored
Merge pull request #753 from mojavelinux/issue-560-version-option
resolves #560 respect custom name for version option
2 parents 38f41bf + 37359ee commit d5ac793

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ program
7979
.version('0.0.1', '-v, --version')
8080
```
8181

82-
Now the command will accept the `-v` option instead of the `-V` option.
82+
The version flags can be named anything, but the long option is required.
8383

8484
## Command-specific options
8585

index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ Command.prototype.opts = function() {
772772

773773
for (var i = 0; i < len; i++) {
774774
var key = this.options[i].attributeName();
775-
result[key] = key === 'version' ? this._version : this[key];
775+
result[key] = key === this._versionOptionName ? this._version : this[key];
776776
}
777777
return result;
778778
};
@@ -855,8 +855,10 @@ Command.prototype.version = function(str, flags) {
855855
if (arguments.length === 0) return this._version;
856856
this._version = str;
857857
flags = flags || '-V, --version';
858+
var longOptIndex = flags.indexOf('--')
859+
this._versionOptionName = ~longOptIndex ? flags.substr(longOptIndex + 2) : 'version'
858860
this.option(flags, 'output the version number');
859-
this.on('option:version', function() {
861+
this.on('option:' + this._versionOptionName, function() {
860862
process.stdout.write(str + '\n');
861863
process.exit(0);
862864
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var program = require('../')
2+
, should = require('should');
3+
4+
var capturedExitCode, capturedOutput, oldProcessExit, oldProcessStdoutWrite;
5+
6+
program.version('0.0.1', '-r, --revision');
7+
8+
['-r', '--revision'].forEach(function (flag) {
9+
capturedExitCode = -1;
10+
capturedOutput = '';
11+
oldProcessExit = process.exit;
12+
oldProcessStdoutWrite = process.stdout.write;
13+
process.exit = function (code) { capturedExitCode = code; }
14+
process.stdout.write = function(output) { capturedOutput += output; }
15+
program.parse(['node', 'test', flag]);
16+
process.exit = oldProcessExit;
17+
process.stdout.write = oldProcessStdoutWrite;
18+
capturedOutput.should.equal('0.0.1\n');
19+
capturedExitCode.should.equal(0);
20+
})

test/test.options.version.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var program = require('../')
2+
, should = require('should');
3+
4+
var capturedExitCode, capturedOutput, oldProcessExit, oldProcessStdoutWrite;
5+
6+
program.version('0.0.1');
7+
8+
['-V', '--version'].forEach(function (flag) {
9+
capturedExitCode = -1;
10+
capturedOutput = '';
11+
oldProcessExit = process.exit;
12+
oldProcessStdoutWrite = process.stdout.write;
13+
process.exit = function (code) { capturedExitCode = code; }
14+
process.stdout.write = function(output) { capturedOutput += output; }
15+
program.parse(['node', 'test', flag]);
16+
process.exit = oldProcessExit;
17+
process.stdout.write = oldProcessStdoutWrite;
18+
capturedOutput.should.equal('0.0.1\n');
19+
capturedExitCode.should.equal(0);
20+
})

0 commit comments

Comments
 (0)