Skip to content
Myr

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

3 files+129 added−30 removed

added:docs/help-in-depth.md+127 added
1# Help in Depth23The built-in help is formatted using the Help class.4You can configure the Help behaviour by modifying data properties and methods using `.configureHelp()`, or by subclassing using `.createHelp()` if you prefer.56Example file: [configure-help.js](../examples/configure-help.js)78```js9program.configureHelp({10 sortSubcommands: true,11 subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.12});13```1415- [Help in Depth](#help-in-depth)16 - [Data Properties](#data-properties)17 - [Stringify and Style](#stringify-and-style)18 - [Layout](#layout)1920## Data Properties2122The data properties are:2324- `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 alphabetically2930Example files:31- [configure-help.js](../examples/configure-help.js)32- [global-options-nested.js](../examples/global-options-nested.js)3334## Stringify and Style3536The `Help` object has narrowly focused methods to allow customising the displayed help.3738The stringify routines take an object (`Command` or `Option` or `Argument`) and produce a string. For example you could change the way subcommands are listed:3940```js41program.configureHelp({42 subcommandTerm: (cmd) => cmd.name() // just show the name instead of usage43});44```4546The style routines take just a string. For example to make the titles bold:4748```js49import { styleText } from 'node:util';50program.configureHelp({51 styleTitle: (str) => styleText('bold', str)52});53```5455There 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()`.5657This 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. 585960```text61Usage: color-help [options] [command]62<-1--> <-------------2-------------->63 <---a----> <---b---> <---c--->6465program description66<--------3-------->6768Options:69<--1--->70 -h, --help display help for command71 <---4----> <---------5------------>7273Commands:74<---1--->75 print [options] <files> print files76 <----------6----------> <----7---->77 <-b-> <---c---> <--d-->78```7980| | 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 | |9596```text97Usage: color-help print [options] <files...>98<-1--> <---------------2------------------->99 <---a----> <-c-> <---b---> <---d---->100101Arguments:102<---1---->103 files files to queue for printing104 <-8-> <------------9------------>105...106```107108Color example files:109110- [color-help.mjs](../examples/color-help.mjs)111- [color-help-replacement.mjs](../examples/color-help-replacement.mjs)112113Stringify example files (`subcommandTerm`):114115- [help-subcommands-usage.js](../examples/help-subcommands-usage.js)116- [configure-help.js](../examples/configure-help.js)117118## Layout119120Utility methods which control the layout include `padWidth`, `boxWrap`, and `formatItem`. These can be overridden to change the layout or replace with an alternative implementation.121122Example files:123124- `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
modified:Readme.md+1 added-29 removed
921 unmodified lines
922923The built-in help is formatted using the Help class.924You can configure the Help behaviour by modifying data properties and methods using `.configureHelp()`, or by subclassing using `.createHelp()` if you prefer.925926The data properties are:926For more detail see (./docs/help-in-depth.md)927928- `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)933934You 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.935936Example file: [configure-help.js](./examples/configure-help.js)937938```js939program.configureHelp({940 sortSubcommands: true,941 subcommandTerm: (cmd) => cmd.name() // Just show the name, instead of short usage.942});943```944945There are _style_ methods to add color to the help, like `styleTitle` and `styleOptionText`. There is built-in support for respecting946environment variables for `NO_COLOR`, `FORCE_COLOR`, and `CLIFORCE_COLOR`.947948Example file: [color-help.mjs](./examples/color-help.mjs)949950Other 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)955928## Custom event listeners929930You can execute custom actions by listening to command and option events.931
210 unmodified lines