Add typings for helpGroup support (#2367)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by John Gee · committed by GitHub · parent c324ea3 · Browse files · 3 files·+120 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+120 added−0 removed
- other:+120 added−0 removed
Provenance
- Commit
dd7edf4- Tree
39272e1- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
c324ea3
3 files changed+120 added−0 removed
modified:lib/help.js+3 added
391 unmodified lines392 return argument.description;393 }394 395 /**396+ * Format a list of items, given a heading and an array of formatted items.397+ *398 * @param {string} heading399 * @param {string[]} items400 * @param {Help} helper401 * @returns string[]4 unmodified lines406 return [helper.styleTitle(heading), ...items, ''];407 }408 409 /**410+ * Group items by their help group heading.411 *412 * @param {Command[] | Option[]} unsortedItems413 * @param {Command[] | Option[]} visibleItems414 * @param {Function} getGroup333 unmodified linesmodified:typings/index.d.ts+67 added
108 unmodified lines109 envVar?: string;110 parseArg?: <T>(value: string, previous: T) => T;111 hidden: boolean;112 argChoices?: string[];113+ helpGroupHeading?: string;114 115 constructor(flags: string, description?: string);116 117 /**76 unmodified lines194 */195 attributeName(): string;196 197 /**198+ * Set the help group heading.199+ */200+ helpGroup(heading: string): this;201+202+ /**203 * Return whether a boolean option.204 *205 * Options are one of boolean, negated, required argument, or optional argument.206 */112 unmodified lines319 description: string,320 helper: Help,321 ): string;322 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+337 /** Generate the built-in help text. */338 formatHelp(cmd: Command, helper: Help): string;339 }340 export type HelpConfiguration = Partial<Help>;648 unmodified lines989 */990 executableDir(): string | null;991 992 /**993+ * Set the help group heading for this subcommand in parent command's help.994+ *995+ * @returns `this` command for chaining996+ */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+ * @example1008+ * program.commandsGroup('Development Commands:);1009+ * program.command('watch')...1010+ * program.command('lint')...1011+ * ...1012+ *1013+ * @returns `this` command for chaining1014+ */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+ * @example1026+ * program1027+ * .optionsGroup('Development Options:')1028+ * .option('-d, --debug', 'output extra debugging')1029+ * .option('-p, --profile', 'output profiling information')1030+ *1031+ * @returns `this` command for chaining1032+ */1033+ optionsGroup(heading: string): this;1034+ /**1035+ * Get the default help group heading for options added to this command.1036+ */1037+ optionsGroup(): string;1038+1039+ /**1040 * Output help information for this command.1041 *1042 * Outputs built-in help, and custom text added using `.addHelpText()`.1043 *70 unmodified linesmodified:typings/index.test-d.ts+50 added
431 unmodified lines432 // name433 expectType<commander.Command>(program.name('my-name'));434 expectType<string>(program.name());435 436+// helpGroup related437+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+446 // nameFromFilename447 expectType<commander.Command>(program.nameFromFilename(__filename));448 449 // executableDir158 unmodified lines608 helper.formatItem('--example', 12, 'example description', helper),609 );610 expectType<boolean>(helper.preformatted('a\nb c'));611 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+646 expectType<string>(helper.styleTitle('Usage:'));647 648 expectType<string>(helper.styleUsage('foo [options] <file>'));649 expectType<string>(helper.styleCommandText('foo'));31 unmodified lines681 expectType<unknown>(baseOption.presetArg);682 expectType<string | undefined>(baseOption.envVar);683 expectType<boolean>(baseOption.hidden);684 expectType<string[] | undefined>(baseOption.argChoices);685+expectType<string | undefined>(baseOption.helpGroupHeading);686 687 // Option methods688 689 // default44 unmodified lines734 735 // attributeName736 expectType<string>(baseOption.attributeName());737 738+// helpGroup739+expectType<commander.Option>(baseOption.helpGroup('My Group'));740+// @ts-expect-error: Option helpGroup() can not be used as a getter.741+baseOption.helpGroup();742+743 // isBoolean744 expectType<boolean>(baseOption.isBoolean());745 746 // Argument properties41 unmodified lines