Skip to content

Commit

Permalink
Split styleItemDescription into option/subcommand/argument (#2283)
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn authored Nov 26, 2024
1 parent 5629947 commit 437e4e3
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/color-help-replacement.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/color-help.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
25 changes: 13 additions & 12 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
52 changes: 42 additions & 10 deletions tests/help.style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
4 changes: 3 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 3 additions & 1 deletion typings/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,10 @@ expectType<string>(helper.styleTitle('Usage:'));
expectType<string>(helper.styleUsage('foo [options] <file>'));
expectType<string>(helper.styleCommandText('foo'));

expectType<string>(helper.styleItemDescription('description'));
expectType<string>(helper.styleCommandDescription('description'));
expectType<string>(helper.styleOptionDescription('description'));
expectType<string>(helper.styleSubcommandDescription('description'));
expectType<string>(helper.styleArgumentDescription('description'));
expectType<string>(helper.styleDescriptionText('description'));

expectType<string>(helper.styleOptionTerm('-a, --all'));
Expand Down

0 comments on commit 437e4e3

Please sign in to comment.