Skip to content

Commit

Permalink
Added .help() and .version() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisn committed Jun 25, 2014
1 parent ed1216f commit d991b9b
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,10 +496,14 @@ Format usage output to wrap at `columns` many columns.
Any command-line argument given that is not demanded, or does not have a
corresponding description, will be reported as an error.

.help()
-------
.help([option, [description]])
------------------------------

Add an option (e.g., `--help`) that displays the usage string and exits the
process. If present, the `description` parameter customises the description of
the help option in the usage string.

Return the generated usage string.
If invoked without parameters, `.help` returns the generated usage string.

Example:

Expand All @@ -511,6 +515,13 @@ console.log(yargs.help());

Later on, ```argv``` can be retrived with ```yargs.argv```

.version(version, option, [description])
----------------------------------------

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.

.showHelp(fn=console.error)
---------------------------

Expand Down
27 changes: 27 additions & 0 deletions example/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var yargs = require('../index');

var argv = yargs
.usage('This is my awesome program\n\nUsage: $0 [options]')
.help('help').alias('help', 'h')
.version('1.0.1', 'version').alias('version', 'V')
.options({
input: {
alias: 'i',
description: "<filename> Input file name",
requiresArg: true,
required: true
},
output: {
alias: 'o',
description: "<filename> output file name",
requiresArg: true,
required: true
}
})
.argv;

console.log('Inspecting options');
console.dir(argv);

console.log("input:", argv.input);
console.log("output:", argv.output);
39 changes: 35 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ function Argv (processArgs, cwd) {
if (opt.count || opt.type === 'count') {
self.count(key);
}

var desc = opt.describe || opt.description || opt.desc;
if (desc) {
self.describe(key, desc);
Expand All @@ -236,10 +236,10 @@ function Argv (processArgs, cwd) {
self.requiresArg(key);
}
}

return self;
};

var wrap = null;
self.wrap = function (cols) {
wrap = cols;
Expand All @@ -257,8 +257,28 @@ function Argv (processArgs, cwd) {
fn(self.help());
return self;
};


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

var helpOpt = null;
self.addHelpOpt = function (opt, msg) {
helpOpt = opt;
self.describe(opt, msg || 'Show help');
return self;
};

self.help = function () {
if (arguments.length > 0) {
return self.addHelpOpt.apply(self, arguments);
}

var keys = Object.keys(
Object.keys(descriptions)
.concat(Object.keys(demanded))
Expand Down Expand Up @@ -397,6 +417,17 @@ function Argv (processArgs, cwd) {

argv.$0 = self.$0;

Object.keys(argv).forEach(function(key) {
if (key === helpOpt) {
self.showHelp(console.log);
process.exit(0);
}
else if (key === versionOpt) {
console.log(version);
process.exit(0);
}
});

if (demanded._ && argv._.length < demanded._.count) {
if (demanded._.msg) {
fail(demanded._.msg);
Expand Down
40 changes: 40 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,46 @@ describe('usage', function () {
]);
});

describe('help option', function () {
it('should display usage', function () {
var r = checkUsage(function () {
return yargs(['--help'])
.demand(['y'])
.help('help')
.argv;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors');
r.should.have.property('logs').with.length(1);
r.should.have.property('exit').and.be.ok;
r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' --help Show help',
' -y [required]',
''
]);
});
});

describe('version option', function () {
it('should display version', function () {
var r = checkUsage(function () {
return yargs(['--version'])
.version('1.0.1', 'version', 'Show version number')
.argv;
});
r.should.have.property('result');
r.result.should.have.property('_').with.length(0);
r.should.have.property('errors');
r.should.have.property('logs').with.length(1);
r.should.have.property('exit').and.be.ok;
r.logs.join('\n').split(/\n+/).should.deep.equal([
'1.0.1'
]);
});
});

it('should succeed when rebase', function () {
yargs.rebase('/home/chevex', '/home/chevex/foo/bar/baz').should.equal('./foo/bar/baz');
yargs.rebase('/home/chevex/foo/bar/baz', '/home/chevex').should.equal('../../..');
Expand Down

0 comments on commit d991b9b

Please sign in to comment.