Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable custom help description messages for -V and -h #870

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ Usage: pizza [options]
An application for pizzas ordering

Options:
-h, --help output usage information
-V, --version output the version number
-h, --help Output usage information
-V, --version Output the version number
-p, --peppers Add peppers
-P, --pineapple Add pineapple
-b, --bbq Add bbq sauce
Expand Down Expand Up @@ -308,8 +308,8 @@ Yields the following help output when `node script-name.js -h` or `node script-n
Usage: custom-help [options]

Options:
-h, --help output usage information
-V, --version output the version number
-h, --help Output usage information
-V, --version Output the version number
-f, --foo enable some foo
-b, --bar enable some bar
-B, --baz enable some baz
Expand Down
8 changes: 4 additions & 4 deletions Readme_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ Usage: pizza [options]
An application for pizzas ordering

Options:
-h, --help output usage information
-V, --version output the version number
-h, --help Output usage information
-V, --version Output the version number
-p, --peppers Add peppers
-P, --pineapple Add pineapple
-b, --bbq Add bbq sauce
Expand Down Expand Up @@ -241,8 +241,8 @@ console.log('stuff');
Usage: custom-help [options]

Options:
-h, --help output usage information
-V, --version output the version number
-h, --help Output usage information
-V, --version Output the version number
-f, --foo enable some foo
-b, --bar enable some bar
-B, --baz enable some baz
Expand Down
24 changes: 24 additions & 0 deletions examples/help-description.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var program = require('../');

program
.option('-f, --food [item]', 'Provide your favourite food item')
.version('0.0.1', '-v, --version', 'Display version number')
.helpDescription('Display this help message')
.parse(process.argv);

/**
* Default behaviour
*/

// program
// .option('-f, --food [item]', 'Provide your favourite food item')
// .version('0.0.1')
// .parse(process.argv);

console.log('Oh, so you like ' + program.food + '!')
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -854,15 +854,16 @@ Command.prototype.variadicArgNotLast = function(name) {
*
* @param {String} str
* @param {String} [flags]
* @param {String} [description]
* @return {Command} for chaining
* @api public
*/

Command.prototype.version = function(str, flags) {
Command.prototype.version = function(str, flags, description) {
if (arguments.length === 0) return this._version;
this._version = str;
flags = flags || '-V, --version';
var versionOption = new Option(flags, 'output the version number');
var versionOption = new Option(flags, description || 'Output the version number');
this._versionOptionName = versionOption.long.substr(2) || 'version';
this.options.push(versionOption);
this.on('option:' + this._versionOptionName, function() {
Expand All @@ -872,6 +873,22 @@ Command.prototype.version = function(str, flags) {
return this;
};

/**
* Set the help message description to `description`.
*
* This methods sets the help description for the "-h, --help" flag
* If not provided, it willl default to 'Output usage information'
*
* @param {String} [description]
* @return {Command} for chaining
* @api public
*/

Command.prototype.helpDescription = function(description) {
this._helpDescription = description;
return this;
};

/**
* Set the description to `str`.
*
Expand Down Expand Up @@ -1054,7 +1071,7 @@ Command.prototype.optionHelp = function() {
return this.options.map(function(option) {
return pad(option.flags, width) + ' ' + option.description +
((option.bool && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
}).concat([pad('-h, --help', width) + ' ' + 'output usage information'])
}).concat([pad('-h, --help', width) + ' ' + (this._helpDescription || 'Output usage information')])
.join('\n');
};

Expand Down
21 changes: 21 additions & 0 deletions test/test.command.helpDescription.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var program = require('../')
, sinon = require('sinon').sandbox.create()
, should = require('should');

sinon.stub(process, 'exit');
sinon.stub(process.stdout, 'write');

program
.helpDescription('My custom help description')

program.parse(['node', 'test']);

program._helpDescription.should.equal('My custom help description');

sinon.restore();
sinon.stub(process.stdout, 'write');
program.outputHelp();

var output = process.stdout.write.args[0];

output[0].should.equal('Usage: test [options]\n\nOptions:\n -h, --help My custom help description\n');
2 changes: 1 addition & 1 deletion test/test.command.helpInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var expectedHelpInformation = [
'Usage: [options] [command]',
'',
'Options:',
' -h, --help output usage information',
' -h, --help Output usage information',
'',
'Commands:',
' somecommand',
Expand Down