Skip to content

Commit e1966fc

Browse files
authored
Omit the help flags from help that are masked by other options (#1247)
1 parent 56221f7 commit e1966fc

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,17 +1401,33 @@ class Command extends EventEmitter {
14011401

14021402
optionHelp() {
14031403
const width = this.padWidth();
1404-
14051404
const columns = process.stdout.columns || 80;
14061405
const descriptionWidth = columns - width - 4;
1406+
function padOptionDetails(flags, description) {
1407+
return pad(flags, width) + ' ' + optionalWrap(description, descriptionWidth, width + 2);
1408+
};
14071409

1408-
// Append the help information
1409-
return this.options.map((option) => {
1410+
// Explicit options (including version)
1411+
const help = this.options.map((option) => {
14101412
const fullDesc = option.description +
14111413
((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
1412-
return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2);
1413-
}).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2)])
1414-
.join('\n');
1414+
return padOptionDetails(option.flags, fullDesc);
1415+
});
1416+
1417+
// Implicit help
1418+
const showShortHelpFlag = this._helpShortFlag && !this._findOption(this._helpShortFlag);
1419+
const showLongHelpFlag = !this._findOption(this._helpLongFlag);
1420+
if (showShortHelpFlag || showLongHelpFlag) {
1421+
let helpFlags = this._helpFlags;
1422+
if (!showShortHelpFlag) {
1423+
helpFlags = this._helpLongFlag;
1424+
} else if (!showLongHelpFlag) {
1425+
helpFlags = this._helpShortFlag;
1426+
}
1427+
help.push(padOptionDetails(helpFlags, this._helpDescription));
1428+
}
1429+
1430+
return help.join('\n');
14151431
};
14161432

14171433
/**

tests/command.help.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,19 @@ test('when addCommand with hidden:true then not displayed in helpInformation', (
114114
const helpInformation = program.helpInformation();
115115
expect(helpInformation).not.toMatch('secret');
116116
});
117+
118+
test('when help short flag masked then not displayed in helpInformation', () => {
119+
const program = new commander.Command();
120+
program
121+
.option('-h, --host', 'select host');
122+
const helpInformation = program.helpInformation();
123+
expect(helpInformation).not.toMatch(/\W-h\W.*display help/);
124+
});
125+
126+
test('when both help flags masked then not displayed in helpInformation', () => {
127+
const program = new commander.Command();
128+
program
129+
.option('-h, --help', 'custom');
130+
const helpInformation = program.helpInformation();
131+
expect(helpInformation).not.toMatch('display help');
132+
});

0 commit comments

Comments
 (0)