Skip to content

Commit 2be1787

Browse files
lib: remove enableHelpPrinting option
1 parent eadccbb commit 2be1787

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

doc/api/util.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,9 +2051,6 @@ 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-
* `enableHelpPrinting` {boolean} When `true`, if any options have help text
2055-
configured and a `--help` entry was provided, the help will be printed to stdout and the process will exit
2056-
with code 0. **Default:** `false`.
20572054

20582055
* Returns: {Object} The parsed command line arguments:
20592056
* `values` {Object} A mapping of parsed option names with their {string}
@@ -2110,9 +2107,11 @@ console.log(values, positionals);
21102107

21112108
### `parseArgs` help text
21122109

2113-
`parseArgs` can generate and display help text for command-line options. To enable
2114-
this functionality, add `help` text to individual options and optionally provide
2115-
general help text via the `help` config property.
2110+
`parseArgs` supports automatic help text generation for command-line options. To use this feature, add a `help`
2111+
property to each option and optionally provide general help text using the `help` config property.
2112+
2113+
When both general help text is provided and `--help` is present in the `args`, `parseArgs`
2114+
will automatically print the help message and exit with code 0.
21162115

21172116
```mjs
21182117
import { parseArgs } from 'node:util';
@@ -2152,12 +2151,20 @@ if (result.printUsage) {
21522151
}
21532152

21542153
// Or automatically print help and exit
2154+
const args = ['-h'];
21552155
parseArgs({
2156+
args,
21562157
options,
21572158
help: 'My CLI Tool v1.0\n\nProcess files with various options.',
2158-
enableHelpPrinting: true,
21592159
});
2160-
// Prints help and exits with code 0
2160+
// Prints:
2161+
// My CLI Tool v1.0
2162+
//
2163+
// Process files with various options.
2164+
// -v, --verbose Enable verbose output
2165+
// -h, --help. Prints command line options
2166+
// --output <arg> Output directory
2167+
// exit with code 0
21612168
```
21622169

21632170
```cjs
@@ -2180,7 +2187,7 @@ const options = {
21802187
},
21812188
};
21822189

2183-
// Get help text in result
2190+
// Get serialized help text in result
21842191
const result = parseArgs({
21852192
options,
21862193
help: 'My CLI Tool v1.0\n\nProcess files with various options.',
@@ -2198,12 +2205,20 @@ if (result.printUsage) {
21982205
}
21992206

22002207
// Or automatically print help and exit
2208+
const args = ['-h'];
22012209
parseArgs({
2210+
args,
22022211
options,
22032212
help: 'My CLI Tool v1.0\n\nProcess files with various options.',
2204-
enableHelpPrinting: true,
22052213
});
2206-
// Prints help and exits with code 0
2214+
// Prints:
2215+
// My CLI Tool v1.0
2216+
//
2217+
// Process files with various options.
2218+
// -v, --verbose Enable verbose output
2219+
// -h, --help. Prints command line options
2220+
// --output <arg> Output directory
2221+
// exit with code 0
22072222
```
22082223

22092224
### `parseArgs` `tokens`

lib/internal/util/parse_args/parse_args.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ const parseArgs = (config = kEmptyObject) => {
350350
const allowNegative = objectGetOwn(config, 'allowNegative') ?? false;
351351
const options = objectGetOwn(config, 'options') ?? { __proto__: null };
352352
const help = objectGetOwn(config, 'help') ?? '';
353-
const enableHelpPrinting = objectGetOwn(config, 'enableHelpPrinting') ?? false;
354353
// Bundle these up for passing to strict-mode checks.
355354
const parseConfig = { args, strict, options, allowPositionals, allowNegative };
356355

@@ -362,7 +361,6 @@ const parseArgs = (config = kEmptyObject) => {
362361
validateBoolean(allowNegative, 'allowNegative');
363362
validateObject(options, 'options');
364363
validateString(help, 'help');
365-
validateBoolean(enableHelpPrinting, 'enableHelpPrinting');
366364
ArrayPrototypeForEach(
367365
ObjectEntries(options),
368366
({ 0: longOption, 1: optionConfig }) => {
@@ -466,12 +464,10 @@ const parseArgs = (config = kEmptyObject) => {
466464
});
467465

468466
const helpRequested = result.values.help;
469-
if (enableHelpPrinting && helpRequested) {
467+
if (help && helpRequested) {
470468
const console = require('internal/console/global');
471-
if (printUsage.length > 0 || help) {
469+
if (printUsage.length > 0) {
472470
console.log(printUsage);
473-
} else {
474-
console.log('No help text available.');
475471
}
476472
process.exit(0);
477473
} else if (printUsage.length > 0) {

test/parallel/test-parse-args.mjs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,17 +1138,6 @@ test('when help value is added, then add initial help text', () => {
11381138
assert.deepStrictEqual(result, expected);
11391139
});
11401140

1141-
test('enableHelpPrinting config must be a boolean', () => {
1142-
const args = ['-f', 'bar'];
1143-
const options = { foo: { type: 'string', short: 'f', help: 'help text' } };
1144-
const help = 'Description for some awesome stuff:';
1145-
const enableHelpPrinting = 'not a boolean';
1146-
assert.throws(() => {
1147-
parseArgs({ args, options, help, enableHelpPrinting });
1148-
}, /The "enableHelpPrinting" argument must be of type boolean/
1149-
);
1150-
});
1151-
11521141
function setupConsoleAndExit() {
11531142
const originalLog = console.log;
11541143
const originalExit = process.exit;
@@ -1172,13 +1161,12 @@ function setupConsoleAndExit() {
11721161
return { getOutput: () => output, getExitCode: () => exitCode, restore };
11731162
}
11741163

1175-
test('when enableHelpPrinting config is true, print all help text and exit', () => {
1164+
test('when --help flag is present with help arg, prints all help text and exit', () => {
11761165
const { getOutput, getExitCode, restore } = setupConsoleAndExit();
11771166

11781167
try {
11791168
const args = [
1180-
'-h', '-a', 'val1', '--beta', '-c', 'val3', '--delta', 'val4', '-e',
1181-
'--foxtrot', 'val6', '--golf', '--hotel', 'val8', '--india', 'val9', '-j',
1169+
'-h', '-a', 'val1',
11821170
];
11831171
const options = {
11841172
help: { type: 'boolean', short: 'h', help: 'Prints command line options' },
@@ -1200,7 +1188,7 @@ test('when enableHelpPrinting config is true, print all help text and exit', ()
12001188
};
12011189
const help = 'Description for some awesome stuff:';
12021190

1203-
parseArgs({ args, options, help, enableHelpPrinting: true });
1191+
parseArgs({ args, options, help });
12041192
} finally {
12051193
restore();
12061194
}
@@ -1223,18 +1211,19 @@ test('when enableHelpPrinting config is true, print all help text and exit', ()
12231211
assert.strictEqual(getOutput(), expectedOutput);
12241212
});
12251213

1226-
test('when enableHelpPrinting config is true, but no help text is available', () => {
1214+
test('when --help flag is present with help arg but no help text is available, prints help text and exit', () => {
12271215
const { getOutput, getExitCode, restore } = setupConsoleAndExit();
12281216

12291217
try {
12301218
const args = ['-a', 'val1', '--help'];
1219+
const help = 'Description for some awesome stuff:';
12311220
const options = { alpha: { type: 'string', short: 'a' }, help: { type: 'boolean' } };
12321221

1233-
parseArgs({ args, options, enableHelpPrinting: true });
1222+
parseArgs({ args, options, help });
12341223
} finally {
12351224
restore();
12361225
}
12371226

12381227
assert.strictEqual(getExitCode(), 0);
1239-
assert.strictEqual(getOutput(), 'No help text available.\n');
1228+
assert.strictEqual(getOutput(), 'Description for some awesome stuff:\n');
12401229
});

0 commit comments

Comments
 (0)