diff --git a/examples/color-help-replacement.mjs b/examples/color-help-replacement.mjs index 9a1c5f709..c53bd2e1f 100644 --- a/examples/color-help-replacement.mjs +++ b/examples/color-help-replacement.mjs @@ -47,7 +47,7 @@ class MyHelp extends Help { styleCommandDescription(str) { return this.chalk.magenta(str); } - styleItemDescription(str) { + styleDescriptionText(str) { return this.chalk.italic(str); } styleOptionText(str) { diff --git a/examples/color-help.mjs b/examples/color-help.mjs index fac6c2265..94bc3e215 100644 --- a/examples/color-help.mjs +++ b/examples/color-help.mjs @@ -9,7 +9,7 @@ program.configureHelp({ styleTitle: (str) => styleText('bold', str), styleCommandText: (str) => styleText('cyan', str), styleCommandDescription: (str) => styleText('magenta', str), - styleItemDescription: (str) => styleText('italic', str), + styleDescriptionText: (str) => styleText('italic', str), styleOptionText: (str) => styleText('green', str), styleArgumentText: (str) => styleText('yellow', str), styleSubcommandText: (str) => styleText('blue', str), diff --git a/lib/help.js b/lib/help.js index 8813e9ee0..e44801541 100644 --- a/lib/help.js +++ b/lib/help.js @@ -401,12 +401,7 @@ class Help { const helpWidth = helper.helpWidth ?? 80; // in case prepareContext() was not called function callFormatItem(term, description) { - return helper.formatItem( - term, - termWidth, - helper.styleItemDescription(description), - helper, - ); + return helper.formatItem(term, termWidth, description, helper); } // Usage @@ -431,7 +426,7 @@ class Help { const argumentList = helper.visibleArguments(cmd).map((argument) => { return callFormatItem( helper.styleArgumentTerm(helper.argumentTerm(argument)), - helper.argumentDescription(argument), + helper.styleArgumentDescription(helper.argumentDescription(argument)), ); }); if (argumentList.length > 0) { @@ -446,7 +441,7 @@ class Help { const optionList = helper.visibleOptions(cmd).map((option) => { return callFormatItem( helper.styleOptionTerm(helper.optionTerm(option)), - helper.optionDescription(option), + helper.styleOptionDescription(helper.optionDescription(option)), ); }); if (optionList.length > 0) { @@ -463,7 +458,7 @@ class Help { .map((option) => { return callFormatItem( helper.styleOptionTerm(helper.optionTerm(option)), - helper.optionDescription(option), + helper.styleOptionDescription(helper.optionDescription(option)), ); }); if (globalOptionList.length > 0) { @@ -479,7 +474,7 @@ class Help { const commandList = helper.visibleCommands(cmd).map((cmd) => { return callFormatItem( helper.styleSubcommandTerm(helper.subcommandTerm(cmd)), - helper.subcommandDescription(cmd), + helper.styleSubcommandDescription(helper.subcommandDescription(cmd)), ); }); if (commandList.length > 0) { @@ -527,10 +522,16 @@ class Help { }) .join(' '); } - styleItemDescription(str) { + styleCommandDescription(str) { return this.styleDescriptionText(str); } - styleCommandDescription(str) { + styleOptionDescription(str) { + return this.styleDescriptionText(str); + } + styleSubcommandDescription(str) { + return this.styleDescriptionText(str); + } + styleArgumentDescription(str) { return this.styleDescriptionText(str); } styleDescriptionText(str) { diff --git a/tests/help.style.test.js b/tests/help.style.test.js index 08e3c7f3d..c3640879c 100644 --- a/tests/help.style.test.js +++ b/tests/help.style.test.js @@ -54,36 +54,68 @@ describe('override style methods and check help information', () => { ); }); - test('styleItemDescription', () => { + test('styleCommandDescription', () => { const program = makeProgram(); program.configureHelp({ - styleItemDescription: (str) => red(str), + styleCommandDescription: (str) => red(str), displayWidth, }); const helpText = program.helpInformation(); expect(helpText).toEqual( - plainHelpInformation - .replace('arg description', red('arg description')) - .replace('sub description', red('sub description')) - .replace(/display help for command/g, red('display help for command')), + plainHelpInformation.replace( + 'program description', + red('program description'), + ), ); }); - test('styleCommandDescription', () => { + test('styleOptionDescription', () => { const program = makeProgram(); program.configureHelp({ - styleCommandDescription: (str) => red(str), + styleOptionDescription: (str) => red(str), displayWidth, }); const helpText = program.helpInformation(); expect(helpText).toEqual( plainHelpInformation.replace( - 'program description', - red('program description'), + /(-h, --help *)(display help for command)/, + (match, p1, p2) => p1 + red(p2), ), ); }); + test('styleSubcommandDescription', () => { + const program = makeProgram(); + program.configureHelp({ + styleSubcommandDescription: (str) => red(str), + displayWidth, + }); + const helpText = program.helpInformation(); + expect(helpText).toEqual( + plainHelpInformation + .replace( + /(\[subarg\] *)(sub description)/, + (match, p1, p2) => p1 + red(p2), + ) + .replace( + /(help \[command\] *)(display help for command)/, + (match, p1, p2) => p1 + red(p2), + ), + ); + }); + + test('styleArgumentDescription', () => { + const program = makeProgram(); + program.configureHelp({ + styleArgumentDescription: (str) => red(str), + displayWidth, + }); + const helpText = program.helpInformation(); + expect(helpText).toEqual( + plainHelpInformation.replace('arg description', red('arg description')), + ); + }); + test('styleDescriptionText', () => { const program = makeProgram(); program.configureHelp({ diff --git a/typings/index.d.ts b/typings/index.d.ts index 1c0c36b77..acbbbc4b0 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -271,8 +271,10 @@ export class Help { /** Style for command name in usage string. */ styleCommandText(str: string): string; - styleItemDescription(str: string): string; styleCommandDescription(str: string): string; + styleOptionDescription(str: string): string; + styleSubcommandDescription(str: string): string; + styleArgumentDescription(str: string): string; /** Base style used by descriptions. */ styleDescriptionText(str: string): string; diff --git a/typings/index.test-d.ts b/typings/index.test-d.ts index 6b67a5361..7d401c0e5 100644 --- a/typings/index.test-d.ts +++ b/typings/index.test-d.ts @@ -605,8 +605,10 @@ expectType(helper.styleTitle('Usage:')); expectType(helper.styleUsage('foo [options] ')); expectType(helper.styleCommandText('foo')); -expectType(helper.styleItemDescription('description')); expectType(helper.styleCommandDescription('description')); +expectType(helper.styleOptionDescription('description')); +expectType(helper.styleSubcommandDescription('description')); +expectType(helper.styleArgumentDescription('description')); expectType(helper.styleDescriptionText('description')); expectType(helper.styleOptionTerm('-a, --all'));