help-in-depth.md
This file
Path
docs/help-in-depth.md
127 lines · 4.2 KiB · Markdown
Last changed by
More Help documentation (#2282)John Geebefore adoptiond355b52
History of this fileTake it
More Help documentation (#2282)John Geebefore adoptiond355b52
lines 1–127why, one step back1# 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