Skip to content

Commit 437e4e3

Browse files
authored
Split styleItemDescription into option/subcommand/argument (#2283)
1 parent 5629947 commit 437e4e3

File tree

6 files changed

+63
-26
lines changed

6 files changed

+63
-26
lines changed

examples/color-help-replacement.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MyHelp extends Help {
4747
styleCommandDescription(str) {
4848
return this.chalk.magenta(str);
4949
}
50-
styleItemDescription(str) {
50+
styleDescriptionText(str) {
5151
return this.chalk.italic(str);
5252
}
5353
styleOptionText(str) {

examples/color-help.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ program.configureHelp({
99
styleTitle: (str) => styleText('bold', str),
1010
styleCommandText: (str) => styleText('cyan', str),
1111
styleCommandDescription: (str) => styleText('magenta', str),
12-
styleItemDescription: (str) => styleText('italic', str),
12+
styleDescriptionText: (str) => styleText('italic', str),
1313
styleOptionText: (str) => styleText('green', str),
1414
styleArgumentText: (str) => styleText('yellow', str),
1515
styleSubcommandText: (str) => styleText('blue', str),

lib/help.js

+13-12
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,7 @@ class Help {
401401
const helpWidth = helper.helpWidth ?? 80; // in case prepareContext() was not called
402402

403403
function callFormatItem(term, description) {
404-
return helper.formatItem(
405-
term,
406-
termWidth,
407-
helper.styleItemDescription(description),
408-
helper,
409-
);
404+
return helper.formatItem(term, termWidth, description, helper);
410405
}
411406

412407
// Usage
@@ -431,7 +426,7 @@ class Help {
431426
const argumentList = helper.visibleArguments(cmd).map((argument) => {
432427
return callFormatItem(
433428
helper.styleArgumentTerm(helper.argumentTerm(argument)),
434-
helper.argumentDescription(argument),
429+
helper.styleArgumentDescription(helper.argumentDescription(argument)),
435430
);
436431
});
437432
if (argumentList.length > 0) {
@@ -446,7 +441,7 @@ class Help {
446441
const optionList = helper.visibleOptions(cmd).map((option) => {
447442
return callFormatItem(
448443
helper.styleOptionTerm(helper.optionTerm(option)),
449-
helper.optionDescription(option),
444+
helper.styleOptionDescription(helper.optionDescription(option)),
450445
);
451446
});
452447
if (optionList.length > 0) {
@@ -463,7 +458,7 @@ class Help {
463458
.map((option) => {
464459
return callFormatItem(
465460
helper.styleOptionTerm(helper.optionTerm(option)),
466-
helper.optionDescription(option),
461+
helper.styleOptionDescription(helper.optionDescription(option)),
467462
);
468463
});
469464
if (globalOptionList.length > 0) {
@@ -479,7 +474,7 @@ class Help {
479474
const commandList = helper.visibleCommands(cmd).map((cmd) => {
480475
return callFormatItem(
481476
helper.styleSubcommandTerm(helper.subcommandTerm(cmd)),
482-
helper.subcommandDescription(cmd),
477+
helper.styleSubcommandDescription(helper.subcommandDescription(cmd)),
483478
);
484479
});
485480
if (commandList.length > 0) {
@@ -527,10 +522,16 @@ class Help {
527522
})
528523
.join(' ');
529524
}
530-
styleItemDescription(str) {
525+
styleCommandDescription(str) {
531526
return this.styleDescriptionText(str);
532527
}
533-
styleCommandDescription(str) {
528+
styleOptionDescription(str) {
529+
return this.styleDescriptionText(str);
530+
}
531+
styleSubcommandDescription(str) {
532+
return this.styleDescriptionText(str);
533+
}
534+
styleArgumentDescription(str) {
534535
return this.styleDescriptionText(str);
535536
}
536537
styleDescriptionText(str) {

tests/help.style.test.js

+42-10
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,68 @@ describe('override style methods and check help information', () => {
5454
);
5555
});
5656

57-
test('styleItemDescription', () => {
57+
test('styleCommandDescription', () => {
5858
const program = makeProgram();
5959
program.configureHelp({
60-
styleItemDescription: (str) => red(str),
60+
styleCommandDescription: (str) => red(str),
6161
displayWidth,
6262
});
6363
const helpText = program.helpInformation();
6464
expect(helpText).toEqual(
65-
plainHelpInformation
66-
.replace('arg description', red('arg description'))
67-
.replace('sub description', red('sub description'))
68-
.replace(/display help for command/g, red('display help for command')),
65+
plainHelpInformation.replace(
66+
'program description',
67+
red('program description'),
68+
),
6969
);
7070
});
7171

72-
test('styleCommandDescription', () => {
72+
test('styleOptionDescription', () => {
7373
const program = makeProgram();
7474
program.configureHelp({
75-
styleCommandDescription: (str) => red(str),
75+
styleOptionDescription: (str) => red(str),
7676
displayWidth,
7777
});
7878
const helpText = program.helpInformation();
7979
expect(helpText).toEqual(
8080
plainHelpInformation.replace(
81-
'program description',
82-
red('program description'),
81+
/(-h, --help *)(display help for command)/,
82+
(match, p1, p2) => p1 + red(p2),
8383
),
8484
);
8585
});
8686

87+
test('styleSubcommandDescription', () => {
88+
const program = makeProgram();
89+
program.configureHelp({
90+
styleSubcommandDescription: (str) => red(str),
91+
displayWidth,
92+
});
93+
const helpText = program.helpInformation();
94+
expect(helpText).toEqual(
95+
plainHelpInformation
96+
.replace(
97+
/(\[subarg\] *)(sub description)/,
98+
(match, p1, p2) => p1 + red(p2),
99+
)
100+
.replace(
101+
/(help \[command\] *)(display help for command)/,
102+
(match, p1, p2) => p1 + red(p2),
103+
),
104+
);
105+
});
106+
107+
test('styleArgumentDescription', () => {
108+
const program = makeProgram();
109+
program.configureHelp({
110+
styleArgumentDescription: (str) => red(str),
111+
displayWidth,
112+
});
113+
const helpText = program.helpInformation();
114+
expect(helpText).toEqual(
115+
plainHelpInformation.replace('arg description', red('arg description')),
116+
);
117+
});
118+
87119
test('styleDescriptionText', () => {
88120
const program = makeProgram();
89121
program.configureHelp({

typings/index.d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,10 @@ export class Help {
271271
/** Style for command name in usage string. */
272272
styleCommandText(str: string): string;
273273

274-
styleItemDescription(str: string): string;
275274
styleCommandDescription(str: string): string;
275+
styleOptionDescription(str: string): string;
276+
styleSubcommandDescription(str: string): string;
277+
styleArgumentDescription(str: string): string;
276278
/** Base style used by descriptions. */
277279
styleDescriptionText(str: string): string;
278280

typings/index.test-d.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,10 @@ expectType<string>(helper.styleTitle('Usage:'));
605605
expectType<string>(helper.styleUsage('foo [options] <file>'));
606606
expectType<string>(helper.styleCommandText('foo'));
607607

608-
expectType<string>(helper.styleItemDescription('description'));
609608
expectType<string>(helper.styleCommandDescription('description'));
609+
expectType<string>(helper.styleOptionDescription('description'));
610+
expectType<string>(helper.styleSubcommandDescription('description'));
611+
expectType<string>(helper.styleArgumentDescription('description'));
610612
expectType<string>(helper.styleDescriptionText('description'));
611613

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

0 commit comments

Comments
 (0)