|
| 1 | +# Help in Depth |
| 2 | + |
| 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 | +```js |
| 9 | +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 Properties |
| 21 | + |
| 22 | +The data properties are: |
| 23 | + |
| 24 | +- `helpWidth`: specify the wrap width, useful for unit tests |
| 25 | +- `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 alphabetically |
| 28 | +- `sortOptions`: sort the options alphabetically |
| 29 | + |
| 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 Style |
| 35 | + |
| 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 | +```js |
| 41 | +program.configureHelp({ |
| 42 | + subcommandTerm: (cmd) => cmd.name() // just show the name instead of usage |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +The style routines take just a string. For example to make the titles bold: |
| 47 | + |
| 48 | +```js |
| 49 | +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 | +```text |
| 61 | +Usage: color-help [options] [command] |
| 62 | +<-1--> <-------------2--------------> |
| 63 | + <---a----> <---b---> <---c---> |
| 64 | +
|
| 65 | +program description |
| 66 | +<--------3--------> |
| 67 | +
|
| 68 | +Options: |
| 69 | +<--1---> |
| 70 | + -h, --help display help for command |
| 71 | + <---4----> <---------5------------> |
| 72 | +
|
| 73 | +Commands: |
| 74 | +<---1---> |
| 75 | + print [options] <files> print files |
| 76 | + <----------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 | +```text |
| 97 | +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 printing |
| 104 | + <-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 | +## Layout |
| 119 | + |
| 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 | + |
0 commit comments