Skip to content

Commit

Permalink
Omit the help flags from help that are masked by other options (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn authored Apr 21, 2020
1 parent 56221f7 commit e1966fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
28 changes: 22 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1401,17 +1401,33 @@ class Command extends EventEmitter {

optionHelp() {
const width = this.padWidth();

const columns = process.stdout.columns || 80;
const descriptionWidth = columns - width - 4;
function padOptionDetails(flags, description) {
return pad(flags, width) + ' ' + optionalWrap(description, descriptionWidth, width + 2);
};

// Append the help information
return this.options.map((option) => {
// Explicit options (including version)
const help = this.options.map((option) => {
const fullDesc = option.description +
((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
return pad(option.flags, width) + ' ' + optionalWrap(fullDesc, descriptionWidth, width + 2);
}).concat([pad(this._helpFlags, width) + ' ' + optionalWrap(this._helpDescription, descriptionWidth, width + 2)])
.join('\n');
return padOptionDetails(option.flags, fullDesc);
});

// Implicit help
const showShortHelpFlag = this._helpShortFlag && !this._findOption(this._helpShortFlag);
const showLongHelpFlag = !this._findOption(this._helpLongFlag);
if (showShortHelpFlag || showLongHelpFlag) {
let helpFlags = this._helpFlags;
if (!showShortHelpFlag) {
helpFlags = this._helpLongFlag;
} else if (!showLongHelpFlag) {
helpFlags = this._helpShortFlag;
}
help.push(padOptionDetails(helpFlags, this._helpDescription));
}

return help.join('\n');
};

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/command.help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,19 @@ test('when addCommand with hidden:true then not displayed in helpInformation', (
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('secret');
});

test('when help short flag masked then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.option('-h, --host', 'select host');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch(/\W-h\W.*display help/);
});

test('when both help flags masked then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.option('-h, --help', 'custom');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('display help');
});

0 comments on commit e1966fc

Please sign in to comment.