Skip to content

Commit f6b6199

Browse files
lib: removing returnHelpText and addHelpOption
1 parent caf2840 commit f6b6199

File tree

3 files changed

+18
-211
lines changed

3 files changed

+18
-211
lines changed

doc/api/util.md

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,19 +2051,12 @@ 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.
20602054

20612055
* Returns: {Object} The parsed command line arguments:
20622056
* `values` {Object} A mapping of parsed option names with their {string}
20632057
or {boolean} values.
20642058
* `positionals` {string\[]} Positional arguments.
20652059
* `helpText` {string | undefined} Formatted help text for all options provided.
2066-
Only included if `returnHelpText` is `true`.
20672060
* `tokens` {Object\[] | undefined} See [parseArgs tokens](#parseargs-tokens)
20682061
section. Only returned if `config` includes `tokens: true`.
20692062

@@ -2114,7 +2107,7 @@ console.log(values, positionals);
21142107
### `parseArgs` help text
21152108

21162109
`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.
2110+
general help text is provided, a help option is automatically added unless already present.
21182111

21192112
#### Simple usage
21202113

@@ -2190,72 +2183,6 @@ if (result.values.help) {
21902183
}
21912184
```
21922185

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',
2210-
},
2211-
};
2212-
2213-
const result = parseArgs({
2214-
addHelpOption: false, // Suppress auto help option
2215-
returnHelpText: true, // Generate help text anyway
2216-
options,
2217-
});
2218-
2219-
if (result.values.assist) {
2220-
console.log(result.helpText);
2221-
// Prints:
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);
2256-
}
2257-
```
2258-
22592186
### `parseArgs` `tokens`
22602187

22612188
Detailed parse information is available for adding custom behaviors by

lib/internal/util/parse_args/parse_args.js

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,7 @@ const parseArgs = (config = kEmptyObject) => {
352352
const help = objectGetOwn(config, 'help') ?? '';
353353

354354
const hasGenerateHelp = help.length > 0;
355-
const addHelpOption = objectGetOwn(config, 'addHelpOption') ?? hasGenerateHelp;
356-
const returnHelpText = objectGetOwn(config, 'returnHelpText') ?? (hasGenerateHelp || addHelpOption);
357-
358-
// Auto-inject help option if requested and not already present
359-
if (addHelpOption && !ObjectHasOwn(options, 'help')) {
355+
if (hasGenerateHelp && !ObjectHasOwn(options, 'help')) {
360356
options = {
361357
...options,
362358
__proto__: null,
@@ -378,8 +374,6 @@ const parseArgs = (config = kEmptyObject) => {
378374
validateBoolean(allowNegative, 'allowNegative');
379375
validateObject(options, 'options');
380376
validateString(help, 'help');
381-
validateBoolean(addHelpOption, 'addHelpOption');
382-
validateBoolean(returnHelpText, 'returnHelpText');
383377
ArrayPrototypeForEach(
384378
ObjectEntries(options),
385379
({ 0: longOption, 1: optionConfig }) => {
@@ -466,24 +460,22 @@ const parseArgs = (config = kEmptyObject) => {
466460
}
467461
});
468462

469-
// Phase 4: generate print usage for each option if requested
470-
if (returnHelpText) {
471-
let printUsage = '';
472-
if (help) {
473-
printUsage += help;
474-
}
475-
ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption, 1: optionConfig }) => {
476-
const helpTextForPrint = formatHelpTextForPrint(longOption, optionConfig);
477-
478-
if (printUsage.length > 0) {
479-
printUsage += '\n';
480-
}
481-
printUsage += helpTextForPrint;
482-
});
463+
// Phase 4: generate print usage for each option
464+
let printUsage = '';
465+
if (help) {
466+
printUsage += help;
467+
}
468+
ArrayPrototypeForEach(ObjectEntries(options), ({ 0: longOption, 1: optionConfig }) => {
469+
const helpTextForPrint = formatHelpTextForPrint(longOption, optionConfig);
483470

484471
if (printUsage.length > 0) {
485-
result.helpText = printUsage;
472+
printUsage += '\n';
486473
}
474+
printUsage += helpTextForPrint;
475+
});
476+
477+
if (help && printUsage.length > 0) {
478+
result.helpText = printUsage;
487479
}
488480

489481
return result;

test/parallel/test-parse-args.mjs

Lines changed: 3 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,127 +1203,15 @@ test('auto-detect --no-foo as negated when strict:false and allowNegative', () =
12031203
assert.strictEqual(result.helpText, printUsage);
12041204
});
12051205

1206-
// Test addHelpOption behavior
1207-
test('addHelpOption validation must be boolean', () => {
1208-
const args = ['-f', 'bar'];
1209-
const options = { foo: { type: 'string', short: 'f', help: 'help text' } };
1210-
1211-
assert.throws(() => {
1212-
parseArgs({ args, options, addHelpOption: 'true' });
1213-
}, /The "addHelpOption" argument must be of type boolean/
1214-
);
1215-
});
1216-
1217-
test('addHelpOption is true auto-injects help option when no existing help option', () => {
1206+
test('auto-injects help option when no existing help option', () => {
12181207
const args = ['--foo', 'bar'];
12191208
const options = { foo: { type: 'string', help: 'use the foo filter' } };
12201209
const help = 'utility to control filters';
12211210

1222-
const result = parseArgs({ args, options, help, addHelpOption: true });
1211+
const result = parseArgs({ args, options, help });
12231212

12241213
assert.ok(result.helpText.includes('-h, --help Show help'));
1225-
const resultWithHelp = parseArgs({ args: ['--help'], options, help, addHelpOption: true });
1214+
const resultWithHelp = parseArgs({ args: ['--help'], options, help });
12261215
assert.strictEqual(resultWithHelp.values.help, true);
12271216
});
1228-
1229-
test('addHelpOption is false prevents auto-injection of help option', () => {
1230-
const args = ['--foo', 'bar'];
1231-
const options = { foo: { type: 'string', help: 'use the foo filter' } };
1232-
const help = 'utility to control filters';
1233-
1234-
const result = parseArgs({ args, options, help, addHelpOption: false });
1235-
1236-
assert.ok(!result.helpText.includes('-h, --help'));
1237-
assert.throws(() => {
1238-
parseArgs({ args: ['--help'], options, help, addHelpOption: false });
1239-
}, { code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' });
1240-
});
1241-
1242-
test('addHelpOption is false but existing help option should still work', () => {
1243-
const args = ['--help'];
1244-
const options = {
1245-
foo: { type: 'string', help: 'use the foo filter' },
1246-
help: { type: 'boolean', short: '?', help: 'display help' }
1247-
};
1248-
const help = 'utility to control filters';
1249-
1250-
const result = parseArgs({ args, options, help, addHelpOption: false });
1251-
1252-
assert.strictEqual(result.values.help, true);
1253-
assert.ok(result.helpText.includes('-?, --help display help'));
1254-
});
1255-
1256-
// Test returnHelpText behavior
1257-
test('returnHelpText validation must be boolean', () => {
1258-
const args = ['-f', 'bar'];
1259-
const options = { foo: { type: 'string', short: 'f', help: 'help text' } };
1260-
1261-
assert.throws(() => {
1262-
parseArgs({ args, options, returnHelpText: 'true' });
1263-
}, /The "returnHelpText" argument must be of type boolean/
1264-
);
1265-
});
1266-
1267-
test('returnHelpText is false prevents help text generation', () => {
1268-
const args = ['--foo', 'bar'];
1269-
const options = { foo: { type: 'string', help: 'use the foo filter' } };
1270-
const help = 'utility to control filters';
1271-
1272-
const result = parseArgs({ args, options, help, returnHelpText: false });
1273-
1274-
assert.strictEqual(result.helpText, undefined);
1275-
});
1276-
1277-
test('returnHelpText is true forces help text generation even without general help', () => {
1278-
const args = ['--foo', 'bar'];
1279-
const options = { foo: { type: 'string', help: 'use the foo filter' } };
1280-
1281-
const result = parseArgs({ args, options, returnHelpText: true, addHelpOption: true });
1282-
1283-
assert.ok(result.helpText);
1284-
assert.ok(result.helpText.includes('--foo <arg> use the foo filter'));
1285-
assert.ok(result.helpText.includes('-h, --help Show help'));
1286-
});
1287-
1288-
test('postpone help generation until needed', () => {
1289-
const options = {
1290-
foo: { type: 'boolean', short: 'f', help: 'use the foo filter' },
1291-
bar: { type: 'string', help: 'use the specified bar filter' }
1292-
};
1293-
1294-
const result = parseArgs({
1295-
addHelpOption: true,
1296-
returnHelpText: false,
1297-
options
1298-
});
1299-
1300-
assert.strictEqual(result.helpText, undefined);
1301-
1302-
const { helpText } = parseArgs({
1303-
help: 'utility to control filters',
1304-
options
1305-
});
1306-
1307-
assert.ok(helpText);
1308-
assert.ok(helpText.includes('utility to control filters'));
1309-
});
1310-
1311-
test('when both addHelpOption and returnHelpText are false, no help functionality', () => {
1312-
const args = ['--foo', 'bar'];
1313-
const options = { foo: { type: 'string', help: 'help text' } };
1314-
const help = 'Description text';
1315-
1316-
const result = parseArgs({
1317-
args,
1318-
options,
1319-
help,
1320-
addHelpOption: false,
1321-
returnHelpText: false
1322-
});
1323-
1324-
assert.strictEqual(result.helpText, undefined);
1325-
assert.throws(() => {
1326-
parseArgs({ args: ['--help'], options, help, addHelpOption: false });
1327-
}, { code: 'ERR_PARSE_ARGS_UNKNOWN_OPTION' });
1328-
});
13291217
}

0 commit comments

Comments
 (0)