From 123d56401f16ceff80cad7293fbcc241e3d85925 Mon Sep 17 00:00:00 2001 From: Thomas Dressler Date: Mon, 10 Jul 2017 12:56:03 -0600 Subject: [PATCH] do not display short flag -h for help option if there is a user-defined option with short flag -h. --- index.js | 29 +++++++++++++++++++++------- package-lock.json | 2 +- test/test.command.helpInformation.js | 24 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 test/test.command.helpInformation.js diff --git a/index.js b/index.js index aacce61d5..7e7a49bc8 100644 --- a/index.js +++ b/index.js @@ -931,6 +931,22 @@ Command.prototype.largestOptionLength = function() { }, 0); }; +/** + * Return `Option` object for help option. + * + * @return {Option} + * @api private + */ +Command.prototype.optionHelp = function() { + // check if there is an overriding user-defined `-h` short flag + var shortHOptions = this.options.filter(function(option) { + return option.short === '-h'; + }); + var helpFlags = shortHOptions.length ? '--help' : '-h, --help'; + + return new Option(helpFlags, 'output usage information'); +}; + /** * Return help for options. * @@ -938,14 +954,13 @@ Command.prototype.largestOptionLength = function() { * @api private */ -Command.prototype.optionHelp = function() { +Command.prototype.optionHelpText = function() { var width = this.largestOptionLength(); - // Append the help information - return this.options.map(function(option) { - return pad(option.flags, width) + ' ' + option.description; - }).concat([pad('-h, --help', width) + ' ' + 'output usage information']) - .join('\n'); + return [this.optionHelp()].concat(this.options).map(function(option) { + return pad(option.flags, width) + ' ' + option.description; + }) + .join('\n'); }; /** @@ -1024,7 +1039,7 @@ Command.prototype.helpInformation = function() { '' , ' Options:' , '' - , '' + this.optionHelp().replace(/^/gm, ' ') + , '' + this.optionHelpText().replace(/^/gm, ' ') , '' ]; diff --git a/package-lock.json b/package-lock.json index 5924df1a3..29c4e6d8b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "commander", - "version": "2.10.0", + "version": "2.11.0", "lockfileVersion": 1, "dependencies": { "diff": { diff --git a/test/test.command.helpInformation.js b/test/test.command.helpInformation.js new file mode 100644 index 000000000..fbebc5a01 --- /dev/null +++ b/test/test.command.helpInformation.js @@ -0,0 +1,24 @@ +var program = require('../') + , sinon = require('sinon').sandbox.create() + , should = require('should'); + +program.option('-x, --x-option', 'option x'); + +var helpText = program.helpInformation(); +helpText.should.containEql('-h, --help'); +helpText.should.containEql('-x, --x-option'); +/** + * User defined -h should override help option short flag. + * In the help information text, -h short flag should + * only appear for the user defined option. + */ +program + .option('-h, --h-option', 'User defined -h.'); + +helpText = program.helpInformation(); +helpText.should.containEql('-h, --h-option'); +helpText.should.not.containEql('-h, --help'); +helpText.should.containEql('--help'); + +program.parse(['node', 'test', '-h']); +program.should.have.property('hOption');