From 14f54c507948d936a46750a06e1ddd1f44984943 Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 25 Jun 2019 10:36:31 -0700 Subject: [PATCH] Keep help as-is, add helpOption method --- Readme.md | 4 +-- examples/custom-help-description | 2 +- index.js | 36 ++++++++++--------- test/test.command.helpInformation.custom.js | 2 +- ...test.command.helpSubCommand.customFlags.js | 4 +-- typings/index.d.ts | 12 +++++-- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/Readme.md b/Readme.md index 1a4c36fc3..f89088a75 100644 --- a/Readme.md +++ b/Readme.md @@ -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) diff --git a/examples/custom-help-description b/examples/custom-help-description index e136f50b2..9892a912a 100644 --- a/examples/custom-help-description +++ b/examples/custom-help-description @@ -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 ', 'specify template engine (jade|ejs) [jade]', 'jade'); diff --git a/index.js b/index.js index f3a0cfb26..58e6bd113 100644 --- a/index.js +++ b/index.js @@ -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` * diff --git a/test/test.command.helpInformation.custom.js b/test/test.command.helpInformation.custom.js index 878d60aff..50ad9ab91 100644 --- a/test/test.command.helpInformation.custom.js +++ b/test/test.command.helpInformation.custom.js @@ -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]'); diff --git a/test/test.command.helpSubCommand.customFlags.js b/test/test.command.helpSubCommand.customFlags.js index a617cc674..c2d7575d0 100644 --- a/test/test.command.helpSubCommand.customFlags.js +++ b/test/test.command.helpSubCommand.customFlags.js @@ -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') @@ -19,7 +19,7 @@ program program .command('family') - .help('-h, --help') + .helpOption('-h, --help') .action((cmd) => { console.log('Familysubcommand...'); }); diff --git a/typings/index.d.ts b/typings/index.d.ts index c430f2c54..8ff3a9edc 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -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; } }