Skip to content

Commit

Permalink
Merge cd018fb into 0b7c19b
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 19, 2015
2 parents 0b7c19b + cd018fb commit 7c0bab6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,18 @@ Later on, ```argv``` can be retrived with ```yargs.argv```
Add an option (e.g., `--version`) that displays the version number (given by the
`version` parameter) and exits the process. If present, the `description`
parameter customises the description of the version option in the usage string.
parameter customizes the description of the version option in the usage string.
You can provide a `function` for version, rather than a string.
This is useful if you want to use the version from your package.json:

```js
var argv = require('yargs')
.version(function() {
return require('../package').version;
})
.argv;
```
.showHelpOnFail(enable, [message])
----------------------------------
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ function Argv (processArgs, cwd) {

var versionOpt = null;
self.version = function (ver, opt, msg) {
versionOpt = opt;
versionOpt = opt || 'version';
usage.version(ver);
self.describe(opt, msg || 'Show version number');
self.describe(versionOpt, msg || 'Show version number');
return self;
};

Expand Down
3 changes: 2 additions & 1 deletion lib/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ module.exports = function (yargs) {
};

self.showVersion = function() {
console.log(version);
if (typeof version === 'function') console.log(version());
else console.log(version);
};

return self;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"repository": {
"type": "git",
"url": "http://github.com/chevex/yargs.git"
"url": "http://github.com/bcoe/yargs.git"
},
"config": {
"blanket": {
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"herp": "derp",
"z": 55,
"foo": "baz"
"foo": "baz",
"version": "1.0.2"
}
24 changes: 24 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,30 @@ describe('usage tests', function () {
r.should.have.property('exit').and.be.ok;
r.logs[0].should.eql('1.0.1');
});

it('should allow a function to be provided, rather than a number', function() {
var r = checkUsage(function () {
return yargs(['--version'])
.version(function() {
return require('./fixtures/config').version;
}, 'version')
.wrap(null)
.argv;
});
r.logs[0].should.eql('1.0.2');
});

it("should default to 'version' as version option", function() {
var r = checkUsage(function () {
return yargs(['--version'])
.version(function() {
return require('./fixtures/config').version;
})
.wrap(null)
.argv;
});
r.logs[0].should.eql('1.0.2');
});
});

describe('showHelpOnFail', function () {
Expand Down

0 comments on commit 7c0bab6

Please sign in to comment.