More Help documentation (#2282)
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 437e4e3 · Browse files · 3 files·+129 added−30 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+129 added−30 removed
- other:+129 added−30 removed
Provenance
- Commit
d355b52- Tree
86f195b- Author
John Gee <john@ruru.gen.nz>- Committer
GitHub <noreply@github.com>- Parent
437e4e3
3 files changed+129 added−30 removed
added:docs/help-in-depth.md+127 added
1+# Help in Depth2+3+The built-in help is formatted using the Help class.4+You can configure the Help behaviour by modifying data properties and methods using `.configureHelp()`, or by subclassing using `.createHelp()` if you prefer.5+6+Example file: [configure-help.js](../examples/configure-help.js)7+8+```js9+program.configureHelp({10+ sortSubcommands: true,11+ subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.12+});13+```14+15+- [Help in Depth](#help-in-depth)16+ - [Data Properties](#data-properties)17+ - [Stringify and Style](#stringify-and-style)18+ - [Layout](#layout)19+20+## Data Properties21+22+The data properties are:23+24+- `helpWidth`: specify the wrap width, useful for unit tests25+- `minWidthToWrap`: specify required width to allow wrapping (default 40)26+- `showGlobalOptions`: show a section with the global options from the parent command(s)27+- `sortSubcommands`: sort the subcommands alphabetically28+- `sortOptions`: sort the options alphabetically29+30+Example files:31+- [configure-help.js](../examples/configure-help.js)32+- [global-options-nested.js](../examples/global-options-nested.js)33+34+## Stringify and Style35+36+The `Help` object has narrowly focused methods to allow customising the displayed help.37+38+The stringify routines take an object (`Command` or `Option` or `Argument`) and produce a string. For example you could change the way subcommands are listed:39+40+```js41+program.configureHelp({42+ subcommandTerm: (cmd) => cmd.name() // just show the name instead of usage43+});44+```45+46+The style routines take just a string. For example to make the titles bold:47+48+```js49+import { styleText } from 'node:util';50+program.configureHelp({51+ styleTitle: (str) => styleText('bold', str)52+});53+```54+55+There is built-in support for detecting whether the output has colors, and respecting environment variables for `NO_COLOR`, `FORCE_COLOR`, and `CLIFORCE_COLOR`. The style routines always get called and color is stripped afterwards if needed using `Command.configureOutput().stripColor()`.56+57+This annotated help output shows where the stringify and style routines are used. The first output is for a program with subcommands, and the second output is for a subcommand with arguments. 58+59+60+```text61+Usage: color-help [options] [command]62+<-1--> <-------------2-------------->63+ <---a----> <---b---> <---c--->64+65+program description66+<--------3-------->67+68+Options:69+<--1--->70+ -h, --help display help for command71+ <---4----> <---------5------------>72+73+Commands:74+<---1--->75+ print [options] <files> print files76+ <----------6----------> <----7---->77+ <-b-> <---c---> <--d-->78+```79+80+| | stringify(object) | style(string) | default style |81+| - | - | - | - |82+| 1 | | styleTitle | |83+| 2 | commandUsage | styleUsage | a, b, c, d |84+| 3 | commandDescription | styleCommandDescription | styleDescriptionText |85+| 4 | optionTerm | styleOptionTerm | styleOptionText |86+| 5 | optionDescription | styleOptionDescription | styleDescriptionText |87+| 6 | subcommandTerm | styleSubcommandTerm | b, c, d |88+| 7 | subcommandDescription | styleSubcommandDescription | styleDescriptionText|89+| 8 | argumentTerm | styleArgumentTerm | styleArgumentText |90+| 9 | argumentDescription | styleArgumentDescription | styleDescriptionText |91+| a | | styleCommandText | |92+| b | | styleOptionText | |93+| c | | styleSubcommandText | |94+| d | | styleArgumentText | |95+96+```text97+Usage: color-help print [options] <files...>98+<-1--> <---------------2------------------->99+ <---a----> <-c-> <---b---> <---d---->100+101+Arguments:102+<---1---->103+ files files to queue for printing104+ <-8-> <------------9------------>105+...106+```107+108+Color example files:109+110+- [color-help.mjs](../examples/color-help.mjs)111+- [color-help-replacement.mjs](../examples/color-help-replacement.mjs)112+113+Stringify example files (`subcommandTerm`):114+115+- [help-subcommands-usage.js](../examples/help-subcommands-usage.js)116+- [configure-help.js](../examples/configure-help.js)117+118+## Layout119+120+Utility methods which control the layout include `padWidth`, `boxWrap`, and `formatItem`. These can be overridden to change the layout or replace with an alternative implementation.121+122+Example files:123+124+- `formatItem`: [help-centered.mjs](../examples/help-centered.mjs)125+- `formatItem`: [man-style-help.mjs](../examples/man-style-help.mjs)126+- `boxwrap`: [color-help-replacement.mjs](../examples/color-help-replacement.mjs)127+modified:examples/color-help-replacement.mjs+1 added-1 removed
7 unmodified lines89101112121314151697 unmodified lines7 unmodified lines} from 'chalk';import { Command, Help } from 'commander';
// Replace default color and wrapping support with Chalk packages as an example of// a deep replacement of format and style support.// a deep replacement of layout and style support.
// This example requires chalk and wrap-ansi and strip-ansi, and won't run// from a clone of Commander repo without installing them first.//97 unmodified linesmodified:Readme.md+1 added-29 removed
921 unmodified lines922 923 The built-in help is formatted using the Help class.924 You can configure the Help behaviour by modifying data properties and methods using `.configureHelp()`, or by subclassing using `.createHelp()` if you prefer.925 926-The data properties are:926+For more detail see (./docs/help-in-depth.md)927 928-- `helpWidth`: specify the wrap width, useful for unit tests929-- `minWidthToWrap`: specify required width to allow wrapping (default 40)930-- `sortSubcommands`: sort the subcommands alphabetically931-- `sortOptions`: sort the options alphabetically932-- `showGlobalOptions`: show a section with the global options from the parent command(s)933-934-You can override any method on the [Help](./lib/help.js) class. There 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.935-936-Example file: [configure-help.js](./examples/configure-help.js)937-938-```js939-program.configureHelp({940- sortSubcommands: true,941- subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.942-});943-```944-945-There are _style_ methods to add color to the help, like `styleTitle` and `styleOptionText`. There is built-in support for respecting946-environment variables for `NO_COLOR`, `FORCE_COLOR`, and `CLIFORCE_COLOR`.947-948-Example file: [color-help.mjs](./examples/color-help.mjs)949-950-Other help configuration examples:951-- [color-help-replacement.mjs](./examples/color-help-replacement.mjs)952-- [help-centered.mjs](./examples/help-centered.mjs)953-- [help-subcommands-usage.js](./examples/help-subcommands-usage.js)954-- [man-style-help.mjs](./examples/man-style-help.mjs)955-928 ## Custom event listeners929 930 You can execute custom actions by listening to command and option events.931 210 unmodified lines