Skip to content

Commit dd7edf4

Browse files
authored
Add typings for helpGroup support (#2367)
1 parent c324ea3 commit dd7edf4

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

lib/help.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,8 @@ class Help {
393393
}
394394

395395
/**
396+
* Format a list of items, given a heading and an array of formatted items.
397+
*
396398
* @param {string} heading
397399
* @param {string[]} items
398400
* @param {Help} helper
@@ -405,6 +407,7 @@ class Help {
405407
}
406408

407409
/**
410+
* Group items by their help group heading.
408411
*
409412
* @param {Command[] | Option[]} unsortedItems
410413
* @param {Command[] | Option[]} visibleItems

typings/index.d.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export class Option {
110110
parseArg?: <T>(value: string, previous: T) => T;
111111
hidden: boolean;
112112
argChoices?: string[];
113+
helpGroupHeading?: string;
113114

114115
constructor(flags: string, description?: string);
115116

@@ -193,6 +194,11 @@ export class Option {
193194
*/
194195
attributeName(): string;
195196

197+
/**
198+
* Set the help group heading.
199+
*/
200+
helpGroup(heading: string): this;
201+
196202
/**
197203
* Return whether a boolean option.
198204
*
@@ -314,6 +320,20 @@ export class Help {
314320
helper: Help,
315321
): string;
316322

323+
/**
324+
* Format a list of items, given a heading and an array of formatted items.
325+
*/
326+
formatItemList(heading: string, items: string[], helper: Help): string[];
327+
328+
/**
329+
* Group items by their help group heading.
330+
*/
331+
groupItems<T extends Command | Option>(
332+
unsortedItems: T[],
333+
visibleItems: T[],
334+
getGroup: (item: T) => string,
335+
): Map<string, T[]>;
336+
317337
/** Generate the built-in help text. */
318338
formatHelp(cmd: Command, helper: Help): string;
319339
}
@@ -969,6 +989,53 @@ export class Command {
969989
*/
970990
executableDir(): string | null;
971991

992+
/**
993+
* Set the help group heading for this subcommand in parent command's help.
994+
*
995+
* @returns `this` command for chaining
996+
*/
997+
helpGroup(heading: string): this;
998+
/**
999+
* Get the help group heading for this subcommand in parent command's help.
1000+
*/
1001+
helpGroup(): string;
1002+
1003+
/**
1004+
* Set the default help group heading for subcommands added to this command.
1005+
* (This does not override a group set directly on the subcommand using .helpGroup().)
1006+
*
1007+
* @example
1008+
* program.commandsGroup('Development Commands:);
1009+
* program.command('watch')...
1010+
* program.command('lint')...
1011+
* ...
1012+
*
1013+
* @returns `this` command for chaining
1014+
*/
1015+
commandsGroup(heading: string): this;
1016+
/**
1017+
* Get the default help group heading for subcommands added to this command.
1018+
*/
1019+
commandsGroup(): string;
1020+
1021+
/**
1022+
* Set the default help group heading for options added to this command.
1023+
* (This does not override a group set directly on the option using .helpGroup().)
1024+
*
1025+
* @example
1026+
* program
1027+
* .optionsGroup('Development Options:')
1028+
* .option('-d, --debug', 'output extra debugging')
1029+
* .option('-p, --profile', 'output profiling information')
1030+
*
1031+
* @returns `this` command for chaining
1032+
*/
1033+
optionsGroup(heading: string): this;
1034+
/**
1035+
* Get the default help group heading for options added to this command.
1036+
*/
1037+
optionsGroup(): string;
1038+
9721039
/**
9731040
* Output help information for this command.
9741041
*

typings/index.test-d.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,16 @@ expectType<string>(program.usage());
433433
expectType<commander.Command>(program.name('my-name'));
434434
expectType<string>(program.name());
435435

436+
// helpGroup related
437+
expectType<commander.Command>(program.helpGroup('My Group'));
438+
expectType<string>(program.helpGroup());
439+
440+
expectType<commander.Command>(program.commandsGroup('My Group'));
441+
expectType<string>(program.commandsGroup());
442+
443+
expectType<commander.Command>(program.optionsGroup('My Group'));
444+
expectType<string>(program.optionsGroup());
445+
436446
// nameFromFilename
437447
expectType<commander.Command>(program.nameFromFilename(__filename));
438448

@@ -599,6 +609,40 @@ expectType<string>(
599609
);
600610
expectType<boolean>(helper.preformatted('a\nb c'));
601611

612+
{
613+
const formattedItems = [
614+
'--example example description',
615+
'--example2 example2 description',
616+
];
617+
expectType<string[]>(
618+
helper.formatItemList('Options', formattedItems, helper),
619+
);
620+
}
621+
622+
{
623+
const unsortedOptions = [new commander.Option('-a, --all')];
624+
const sortedOptions = unsortedOptions;
625+
const groupsItemsOptionHeading = (option: commander.Option) =>
626+
option.helpGroupHeading ?? 'Options:';
627+
expectType<Map<string, commander.Option[]>>(
628+
helper.groupItems(unsortedOptions, sortedOptions, groupsItemsOptionHeading),
629+
);
630+
}
631+
632+
{
633+
const unsortedCommands = [new commander.Command('foo')];
634+
const sortedCommands = unsortedCommands;
635+
const groupsItemsCommandHeading = (cmd: commander.Command) =>
636+
cmd.helpGroup() ?? 'Commands:';
637+
expectType<Map<string, commander.Command[]>>(
638+
helper.groupItems(
639+
unsortedCommands,
640+
sortedCommands,
641+
groupsItemsCommandHeading,
642+
),
643+
);
644+
}
645+
602646
expectType<string>(helper.styleTitle('Usage:'));
603647

604648
expectType<string>(helper.styleUsage('foo [options] <file>'));
@@ -638,6 +682,7 @@ expectType<unknown>(baseOption.presetArg);
638682
expectType<string | undefined>(baseOption.envVar);
639683
expectType<boolean>(baseOption.hidden);
640684
expectType<string[] | undefined>(baseOption.argChoices);
685+
expectType<string | undefined>(baseOption.helpGroupHeading);
641686

642687
// Option methods
643688

@@ -690,6 +735,11 @@ expectType<string>(baseOption.name());
690735
// attributeName
691736
expectType<string>(baseOption.attributeName());
692737

738+
// helpGroup
739+
expectType<commander.Option>(baseOption.helpGroup('My Group'));
740+
// @ts-expect-error: Option helpGroup() can not be used as a getter.
741+
baseOption.helpGroup();
742+
693743
// isBoolean
694744
expectType<boolean>(baseOption.isBoolean());
695745

0 commit comments

Comments
 (0)