Skip to content
Myr

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

3 files+120 added−0 removed

modified:lib/help.js+3 added
391 unmodified lines
392 return argument.description;393 }394395 /**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 lines
406 return [helper.styleTitle(heading), ...items, ''];407 }408409 /**410 * Group items by their help group heading.411 *412 * @param {Command[] | Option[]} unsortedItems413 * @param {Command[] | Option[]} visibleItems414 * @param {Function} getGroup
333 unmodified lines
modified:typings/index.d.ts+67 added
108 unmodified lines
109 envVar?: string;110 parseArg?: <T>(value: string, previous: T) => T;111 hidden: boolean;112 argChoices?: string[];113 helpGroupHeading?: string;114115 constructor(flags: string, description?: string);116117 /**
76 unmodified lines
194 */195 attributeName(): string;196197 /**198 * Set the help group heading.199 */200 helpGroup(heading: string): this;201202 /**203 * Return whether a boolean option.204 *205 * Options are one of boolean, negated, required argument, or optional argument.206 */
112 unmodified lines
319 description: string,320 helper: Help,321 ): string;322323 /**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[];327328 /**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[]>;336337 /** Generate the built-in help text. */338 formatHelp(cmd: Command, helper: Help): string;339}340export type HelpConfiguration = Partial<Help>;
648 unmodified lines
989 */990 executableDir(): string | null;991992 /**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;10021003 /**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;10201021 /**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;10381039 /**1040 * Output help information for this command.1041 *1042 * Outputs built-in help, and custom text added using `.addHelpText()`.1043 *
70 unmodified lines
modified:typings/index.test-d.ts+50 added
431 unmodified lines
432// name433expectType<commander.Command>(program.name('my-name'));434expectType<string>(program.name());435436// helpGroup related437expectType<commander.Command>(program.helpGroup('My Group'));438expectType<string>(program.helpGroup());439440expectType<commander.Command>(program.commandsGroup('My Group'));441expectType<string>(program.commandsGroup());442443expectType<commander.Command>(program.optionsGroup('My Group'));444expectType<string>(program.optionsGroup());445446// nameFromFilename447expectType<commander.Command>(program.nameFromFilename(__filename));448449// executableDir
158 unmodified lines
608 helper.formatItem('--example', 12, 'example description', helper),609);610expectType<boolean>(helper.preformatted('a\nb c'));611612{613 const formattedItems = [614 '--example example description',615 '--example2 example2 description',616 ];617 expectType<string[]>(618 helper.formatItemList('Options', formattedItems, helper),619 );620}621622{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}631632{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}645646expectType<string>(helper.styleTitle('Usage:'));647648expectType<string>(helper.styleUsage('foo [options] <file>'));649expectType<string>(helper.styleCommandText('foo'));
31 unmodified lines
681expectType<unknown>(baseOption.presetArg);682expectType<string | undefined>(baseOption.envVar);683expectType<boolean>(baseOption.hidden);684expectType<string[] | undefined>(baseOption.argChoices);685expectType<string | undefined>(baseOption.helpGroupHeading);686687// Option methods688689// default
44 unmodified lines
734735// attributeName736expectType<string>(baseOption.attributeName());737738// helpGroup739expectType<commander.Option>(baseOption.helpGroup('My Group'));740// @ts-expect-error: Option helpGroup() can not be used as a getter.741baseOption.helpGroup();742743// isBoolean744expectType<boolean>(baseOption.isBoolean());745746// Argument properties
41 unmodified lines