Skip to content

Commit

Permalink
Keep help as-is, add helpOption method
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan committed Jun 25, 2019
1 parent f204dfd commit 14f54c5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 24 deletions.
4 changes: 2 additions & 2 deletions Readme.md
Expand Up @@ -439,13 +439,13 @@ function make_red(txt) {
}
```
## .help(flags, description)
## .helpOption(flags, description)
Override the default help flags and description.
```js
program
.help('-e, --HELP', 'read more information');
.helpOption('-e, --HELP', 'read more information');
```
## .help(cb)
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-help-description
Expand Up @@ -7,7 +7,7 @@
var program = require('../');

program
.help('-c, --HELP', 'custom help message')
.helpOption('-c, --HELP', 'custom help message')
.option('-s, --sessions', 'add session support')
.option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade');

Expand Down
36 changes: 20 additions & 16 deletions index.js
Expand Up @@ -1164,36 +1164,40 @@ Command.prototype.outputHelp = function(cb) {
};

/**
* When called with no arguments, or with a callback, this will
* output help information and exit.
*
* You can pass in flags and a description to override the help
* flags and help description for your command.
*
* @param {Function|String} [flagsOrCb]
* @param {String} [description]
* @return {Command?}
* @param {String} flags
* @param {String} description
* @return {Command}
* @api public
*/

Command.prototype.help = function(flagsOrCb, description) {
if (typeof cb === 'function' || arguments.length === 0) {
this.outputHelp(flagsOrCb);
process.exit();
}

this._helpFlags = flagsOrCb || this._helpFlags;
Command.prototype.helpOption = function(flags, description) {
this._helpFlags = flags || this._helpFlags;
this._helpDescription = description || this._helpDescription;

var flags = this._helpFlags.split(/[ ,|]+/);
var splitFlags = this._helpFlags.split(/[ ,|]+/);

if (flags.length > 1) this._helpShortFlag = flags.shift();
if (splitFlags.length > 1) this._helpShortFlag = splitFlags.shift();

this._helpLongFlag = flags.shift();
this._helpLongFlag = splitFlags.shift();

return this;
};

/**
* Output help information and exit.
*
* @param {Function} cb
* @api public
*/

Command.prototype.help = function(cb) {
this.outputHelp(cb);
process.exit();
};

/**
* Camel-case the given `flag`
*
Expand Down
2 changes: 1 addition & 1 deletion test/test.command.helpInformation.custom.js
Expand Up @@ -5,7 +5,7 @@ var program = require('../')
sinon.stub(process, 'exit');
sinon.stub(process.stdout, 'write');

program.help('-c, --HELP', 'custom help output');
program.helpOption('-c, --HELP', 'custom help output');
program.command('somecommand');
program.command('anothercommand [options]');

Expand Down
4 changes: 2 additions & 2 deletions test/test.command.helpSubCommand.customFlags.js
Expand Up @@ -8,7 +8,7 @@ sinon.stub(process.stdout, 'write');
// Test that subcommands inherit the help flags
// but can also override help flags
program
.help('-i, --ihelp', 'foo foo');
.helpOption('-i, --ihelp', 'foo foo');

program
.command('child')
Expand All @@ -19,7 +19,7 @@ program

program
.command('family')
.help('-h, --help')
.helpOption('-h, --help')
.action((cmd) => {
console.log('Familysubcommand...');
});
Expand Down
12 changes: 10 additions & 2 deletions typings/index.d.ts
Expand Up @@ -282,10 +282,18 @@ declare namespace local {
* You can pass in flags and a description to override the help
* flags and help description for your command.
*
* @param {string | (str: string) => string} [flagsOrCb]
* @param {string} [flags]
* @param {string} [description]
* @return {Command}
*/
helpOption(flags?: string, description?: string): Command;

/** Output help information and exit.
*
* @param {(str: string) => string} [cb]
* @param {string} [description]
*/
help(flagsOrCb?: string | helpCallback, description?: string): never;
help(cb: helpCallback, description?: string): never;
}

}
Expand Down

0 comments on commit 14f54c5

Please sign in to comment.