resolves #560 respect custom name for version option
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Dan Allen · committed by Dan Allen · parent e5d8f04 · Browse files · 4 files·+45 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+45 added−3 removed
- tests:+40 added−0 removed
- other:+5 added−3 removed
Provenance
- Commit
37359ee- Tree
60f7597- Author
Dan Allen <dan.j.allen@gmail.com>- Committer
Dan Allen <dan.j.allen@gmail.com>- Parent
e5d8f04
4 files changed+45 added−3 removed
added:test/test.options.version.custom.js+20 added
1234567891011121314151617181920var 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);})added:test/test.options.version.js+20 added
1234567891011121314151617181920var 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);})modified:index.js+4 added-2 removed
772 unmodified lines773 , len = this.options.length;774 775 for (var i = 0 ; i < len; i++) {776 var key = this.options[i].attributeName();777- result[key] = key === 'version' ? this._version : this[key];777+ result[key] = key === this._versionOptionName ? this._version : this[key];778 }779 return result;780 };781 74 unmodified lines856 Command.prototype.version = function(str, flags) {857 if (0 == arguments.length) return this._version;858 this._version = str;859 flags = flags || '-V, --version';860+ var longOptIndex = flags.indexOf('--')861+ this._versionOptionName = ~longOptIndex ? flags.substr(longOptIndex + 2) : 'version'862 this.option(flags, 'output the version number');861- this.on('option:version', function() {863+ this.on('option:' + this._versionOptionName, function() {864 process.stdout.write(str + '\n');865 process.exit(0);866 });867 return this;293 unmodified linesmodified:Readme.md+1 added-1 removed
77 unmodified lines78 program79 .version('0.0.1', '-v, --version')80 ```81 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.83 84 ## Command-specific options85 86 You can attach options to a command.322 unmodified lines