@@ -2051,13 +2051,19 @@ changes:
20512051 the tokens in different ways.
20522052 ** Default:** ` false ` .
20532053 * ` help ` {string} General help text to display at the beginning of help output.
2054+ * ` addHelpOption ` {boolean} Whether to automatically add a help option to the
2055+ options if general help text is provided and no existing help option is defined.
2056+ The auto-added help option uses ` -h ` short flag and ` --help ` long flag.
2057+ ** Default:** ` true ` if ` help ` is provided, ` false ` otherwise.
2058+ * ` returnHelpText ` {boolean} Whether to return formatted help text in the result.
2059+ ** Default:** ` true ` if ` help ` is provided or ` addHelpOption ` is ` true ` , ` false ` otherwise.
20542060
20552061* Returns: {Object} The parsed command line arguments:
20562062 * ` values ` {Object} A mapping of parsed option names with their {string}
20572063 or {boolean} values.
2058- * ` help ` {string | undefined} Formatted help text for all options provided. Only included if general ` help ` text
2059- is available.
20602064 * ` positionals ` {string\[ ] } Positional arguments.
2065+ * ` helpText ` {string | undefined} Formatted help text for all options provided.
2066+ Only included if ` returnHelpText ` is ` true ` .
20612067 * ` tokens ` {Object\[ ] | undefined} See [ parseArgs tokens] ( #parseargs-tokens )
20622068 section. Only returned if ` config ` includes ` tokens: true ` .
20632069
@@ -2107,81 +2113,146 @@ console.log(values, positionals);
21072113
21082114### ` parseArgs ` help text
21092115
2110- ` parseArgs ` supports automatic formatted help text generation for command-line options. To use this feature, provide
2111- general help text using the ` help ` config property, and also
2112- a ` help ` property to each option can be optionally included.
2116+ ` parseArgs ` supports automatic formatted help text generation for command-line options. When
2117+ general help text is provided, a help option is automatically added unless disabled or already present.
2118+
2119+ #### Simple usage
2120+
2121+ By default, providing general help text automatically adds a help option (` -h, --help ` ) and
2122+ returns formatted help text in ` result.helpText ` :
21132123
21142124``` mjs
21152125import { parseArgs } from ' node:util' ;
21162126
21172127const options = {
2118- verbose: {
2119- type: ' boolean' ,
2120- short: ' v' ,
2121- },
2122- help: {
2128+ foo: {
21232129 type: ' boolean' ,
2124- short: ' h ' ,
2125- help: ' Prints usage information ' ,
2130+ short: ' f ' ,
2131+ help: ' use the foo filter ' ,
21262132 },
2127- output : {
2133+ bar : {
21282134 type: ' string' ,
2129- help: ' Output directory ' ,
2135+ help: ' use the specified bar filter ' ,
21302136 },
21312137};
21322138
2133- // Get serialized help text in result
21342139const result = parseArgs ({
2140+ help: ' utility to control filters' ,
21352141 options,
2136- help: ' My CLI Tool v1.0\n\n Process files with various options.' ,
21372142});
21382143
2139- if (result .printUsage ) {
2140- console .log (result .printUsage );
2144+ if (result .values . help ) {
2145+ console .log (result .helpText );
21412146 // Prints:
2142- // My CLI Tool v1.0
2143- //
2144- // Process files with various options.
2145- // -v, --verbose
2146- // -h, --help. Prints command line options
2147- // --output <arg> Output directory
2147+ // utility to control filters
2148+ // -f, --foo use the foo filter
2149+ // --bar <arg> use the specified bar filter
2150+ // -h, --help Show help
21482151}
21492152```
21502153
2151- ``` cjs
2152- const { parseArgs } = require (' node:util' );
2154+ #### Custom help option
2155+
2156+ You can override the auto-added help option by defining your own:
2157+
2158+ ``` mjs
2159+ import { parseArgs } from ' node:util' ;
21532160
21542161const options = {
2155- verbose : {
2162+ foo : {
21562163 type: ' boolean' ,
2157- short: ' v' ,
2164+ short: ' f' ,
2165+ help: ' use the foo filter' ,
2166+ },
2167+ bar: {
2168+ type: ' string' ,
2169+ help: ' use the specified bar filter' ,
21582170 },
21592171 help: {
21602172 type: ' boolean' ,
2161- short: ' h ' ,
2162- help: ' Prints command line options ' ,
2173+ short: ' ? ' ,
2174+ help: ' display help ' ,
21632175 },
2164- output: {
2165- type: ' string' ,
2166- help: ' Output directory' ,
2176+ };
2177+
2178+ const result = parseArgs ({
2179+ help: ' utility to control filters' ,
2180+ options,
2181+ });
2182+
2183+ if (result .values .help ) {
2184+ console .log (result .helpText );
2185+ // Prints:
2186+ // utility to control filters
2187+ // -f, --foo use the foo filter
2188+ // --bar <arg> use the specified bar filter
2189+ // -?, --help display help
2190+ }
2191+ ```
2192+
2193+ #### Advanced configuration
2194+
2195+ Use ` addHelpOption ` and ` returnHelpText ` to customize help behavior:
2196+
2197+ ``` mjs
2198+ import { parseArgs } from ' node:util' ;
2199+
2200+ const options = {
2201+ foo: {
2202+ type: ' boolean' ,
2203+ short: ' f' ,
2204+ help: ' use the foo filter' ,
2205+ },
2206+ assist: {
2207+ type: ' boolean' ,
2208+ short: ' ?' ,
2209+ help: ' display help' ,
21672210 },
21682211};
21692212
2170- // Get serialized help text in result
21712213const result = parseArgs ({
2214+ addHelpOption: false , // Suppress auto help option
2215+ returnHelpText: true , // Generate help text anyway
21722216 options,
2173- help: ' My CLI Tool v1.0\n\n Process files with various options.' ,
21742217});
21752218
2176- if (result .printUsage ) {
2177- console .log (result .printUsage );
2219+ if (result .values . assist ) {
2220+ console .log (result .helpText );
21782221 // Prints:
2179- // My CLI Tool v1.0
2180- //
2181- // Process files with various options.
2182- // -v, --verbose
2183- // -h, --help. Prints command line options
2184- // --output <arg> Output directory
2222+ // -f, --foo use the foo filter
2223+ // -?, --assist display help
2224+ }
2225+ ```
2226+
2227+ #### Deferred help generation
2228+
2229+ For performance, you can postpone help text generation until needed:
2230+
2231+ ``` mjs
2232+ import { parseArgs } from ' node:util' ;
2233+
2234+ const options = {
2235+ foo: {
2236+ type: ' boolean' ,
2237+ short: ' f' ,
2238+ help: ' use the foo filter' ,
2239+ },
2240+ };
2241+
2242+ // First parse without generating help text
2243+ const result = parseArgs ({
2244+ addHelpOption: true ,
2245+ returnHelpText: false ,
2246+ options,
2247+ });
2248+
2249+ if (result .values .help ) {
2250+ // Generate help text only when needed
2251+ const { helpText } = parseArgs ({
2252+ help: ' utility to control filters' ,
2253+ options,
2254+ });
2255+ console .log (helpText);
21852256}
21862257```
21872258
0 commit comments