Skip to content
Myr

Support showing global options in help (#1828)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add help configuration to show the global options.

authored by John Gee · committed by GitHub · parent 198c6e4 · Browse files · 11 files+274 added−82 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

11 files+274 added−82 removed

  • tests:+120 added−0 removed
  • other:+154 added−82 removed

11 files+274 added−82 removed

added:examples/global-options-added.js+51 added
added:examples/global-options-nested.js+32 added
deleted:examples/global-options.js-51 removed
deleted:examples/optsWithGlobals.js-24 removed
modified:lib/help.js+62 added-7 removed
14 unmodified lines
15 constructor() {16 this.helpWidth = undefined;17 this.sortSubcommands = false;18 this.sortOptions = false;19 this.showGlobalOptions = false;20 }2122 /**23 * Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one.
22 unmodified lines
46 return visibleCommands;47 }4849 /**50 * Compare options for sort.51 *52 * @param {Option} a53 * @param {Option} b54 * @returns number55 */56 compareOptions(a, b) {57 const getSortKey = (option) => {58 // WYSIWYG for order displayed in help. Short used for comparison if present. No special handling for negated.59 return option.short ? option.short.replace(/^-/, '') : option.long.replace(/^--/, '');60 };61 return getSortKey(a).localeCompare(getSortKey(b));62 }6364 /**65 * Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one.66 *67 * @param {Command} cmd68 * @returns {Option[]}
15 unmodified lines
84 }85 visibleOptions.push(helpOption);86 }87 if (this.sortOptions) {72 const getSortKey = (option) => {73 // WYSIWYG for order displayed in help with short before long, no special handling for negated.74 return option.short ? option.short.replace(/^-/, '') : option.long.replace(/^--/, '');75 };76 visibleOptions.sort((a, b) => {77 return getSortKey(a).localeCompare(getSortKey(b));78 });88 visibleOptions.sort(this.compareOptions);89 }90 return visibleOptions;91 }9293 /**94 * Get an array of the visible global options. (Not including help.)95 *96 * @param {Command} cmd97 * @returns {Option[]}98 */99100 visibleGlobalOptions(cmd) {101 if (!this.showGlobalOptions) return [];102103 const globalOptions = [];104 for (let parentCmd = cmd.parent; parentCmd; parentCmd = parentCmd.parent) {105 const visibleOptions = parentCmd.options.filter((option) => !option.hidden);106 globalOptions.push(...visibleOptions);107 }108 if (this.sortOptions) {109 globalOptions.sort(this.compareOptions);110 }111 return globalOptions;112 }113114 /**115 * Get an array of the arguments if any have a description.116 *117 * @param {Command} cmd118 * @returns {Argument[]}
80 unmodified lines
199 }, 0);200 }201202 /**203 * Get the longest global option term length.204 *205 * @param {Command} cmd206 * @param {Help} helper207 * @returns {number}208 */209210 longestGlobalOptionTermLength(cmd, helper) {211 return helper.visibleGlobalOptions(cmd).reduce((max, option) => {212 return Math.max(max, helper.optionTerm(option).length);213 }, 0);214 }215216 /**217 * Get the longest argument term length.218 *219 * @param {Command} cmd220 * @param {Help} helper
164 unmodified lines
385 if (optionList.length > 0) {386 output = output.concat(['Options:', formatList(optionList), '']);387 }388389 if (this.showGlobalOptions) {390 const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {391 return formatItem(helper.optionTerm(option), helper.optionDescription(option));392 });393 if (globalOptionList.length > 0) {394 output = output.concat(['Global Options:', formatList(globalOptionList), '']);395 }396 }397398 // Commands399 const commandList = helper.visibleCommands(cmd).map((cmd) => {400 return formatItem(helper.subcommandTerm(cmd), helper.subcommandDescription(cmd));401 });
14 unmodified lines
416417 padWidth(cmd, helper) {418 return Math.max(419 helper.longestOptionTermLength(cmd, helper),420 helper.longestGlobalOptionTermLength(cmd, helper),421 helper.longestSubcommandTermLength(cmd, helper),422 helper.longestArgumentTermLength(cmd, helper)423 );424 }
37 unmodified lines
modified:tests/help.padWidth.test.js+13 added
added:tests/help.showGlobalOptions.test.js+62 added
added:tests/help.visibleGlobalOptions.test.js+45 added
modified:typings/index.d.ts+5 added
197 unmodified lines
198 /** output helpWidth, long lines are wrapped to fit */199 helpWidth?: number;200 sortSubcommands: boolean;201 sortOptions: boolean;202 showGlobalOptions: boolean;203204 constructor();205206 /** Get the command term to show in the list of subcommands. */
17 unmodified lines
224 /** Get an array of the visible subcommands. Includes a placeholder for the implicit help command, if there is one. */225 visibleCommands(cmd: Command): Command[];226 /** Get an array of the visible options. Includes a placeholder for the implicit help option, if there is one. */227 visibleOptions(cmd: Command): Option[];228 /** Get an array of the visible global options. (Not including help.) */229 visibleGlobalOptions(cmd: Command): Option[];230 /** Get an array of the arguments which have descriptions. */231 visibleArguments(cmd: Command): Argument[];232233 /** Get the longest command term length. */234 longestSubcommandTermLength(cmd: Command, helper: Help): number;235 /** Get the longest option term length. */236 longestOptionTermLength(cmd: Command, helper: Help): number;237 /** Get the longest global option term length. */238 longestGlobalOptionTermLength(cmd: Command, helper: Help): number;239 /** Get the longest argument term length. */240 longestArgumentTermLength(cmd: Command, helper: Help): number;241 /** Calculate the pad width from the maximum term length. */242 padWidth(cmd: Command, helper: Help): number;
644 unmodified lines
modified:typings/index.test-d.ts+3 added
364 unmodified lines
365366expectType<number | undefined>(helper.helpWidth);367expectType<boolean>(helper.sortSubcommands);368expectType<boolean>(helper.sortOptions);369expectType<boolean>(helper.showGlobalOptions);370371expectType<string>(helper.subcommandTerm(helperCommand));372expectType<string>(helper.commandUsage(helperCommand));373expectType<string>(helper.commandDescription(helperCommand));
4 unmodified lines
378expectType<string>(helper.argumentDescription(helperArgument));379380expectType<commander.Command[]>(helper.visibleCommands(helperCommand));381expectType<commander.Option[]>(helper.visibleOptions(helperCommand));382expectType<commander.Option[]>(helper.visibleGlobalOptions(helperCommand));383expectType<commander.Argument[]>(helper.visibleArguments(helperCommand));384385expectType<number>(helper.longestSubcommandTermLength(helperCommand, helper));386expectType<number>(helper.longestOptionTermLength(helperCommand, helper));387expectType<number>(helper.longestGlobalOptionTermLength(helperCommand, helper));388expectType<number>(helper.longestArgumentTermLength(helperCommand, helper));389expectType<number>(helper.padWidth(helperCommand, helper));390391expectType<string>(helper.wrap('a b c', 50, 3));
86 unmodified lines
modified:Readme.md+1 added
913 unmodified lines
914915- `helpWidth`: specify the wrap width, useful for unit tests916- `sortSubcommands`: sort the subcommands alphabetically917- `sortOptions`: sort the options alphabetically918- `showGlobalOptions`: show a section with the global options from the parent command(s)919920There are methods getting the visible lists of arguments, options, and subcommands. There are methods for formatting the items in the lists, with each item having a _term_ and _description_. Take a look at `.formatHelp()` to see how they are used.921922Example file: [configure-help.js](./examples/configure-help.js)
205 unmodified lines