Skip to content

Commit 98c0d7e

Browse files
lib: remove enableHelpPrinting option
1 parent c54f347 commit 98c0d7e

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
@@ -1979,9 +1979,6 @@ changes:
19791979
the tokens in different ways.
19801980
**Default:** `false`.
19811981
* `help` {string} General help text to display at the beginning of help output.
1982-
* `enableHelpPrinting` {boolean} When `true`, if any options have help text
1983-
configured and a `--help` entry was provided, the help will be printed to stdout and the process will exit
1984-
with code 0. **Default:** `false`.
19851982

19861983
* Returns: {Object} The parsed command line arguments:
19871984
* `values` {Object} A mapping of parsed option names with their {string}
@@ -2038,9 +2035,11 @@ console.log(values, positionals);
20382035

20392036
### `parseArgs` help text
20402037

2041-
`parseArgs` can generate and display help text for command-line options. To enable
2042-
this functionality, add `help` text to individual options and optionally provide
2043-
general help text via the `help` config property.
2038+
`parseArgs` supports automatic help text generation for command-line options. To use this feature, add a `help`
2039+
property to each option and optionally provide general help text using the `help` config property.
2040+
2041+
When both general help text is provided and `--help` is present in the `args`, `parseArgs`
2042+
will automatically print the help message and exit with code 0.
20442043

20452044
```mjs
20462045
import { parseArgs } from 'node:util';
@@ -2080,12 +2079,20 @@ if (result.printUsage) {
20802079
}
20812080

20822081
// Or automatically print help and exit
2082+
const args = ['-h'];
20832083
parseArgs({
2084+
args,
20842085
options,
20852086
help: 'My CLI Tool v1.0\n\nProcess files with various options.',
2086-
enableHelpPrinting: true,
20872087
});
2088-
// Prints help and exits with code 0
2088+
// Prints:
2089+
// My CLI Tool v1.0
2090+
//
2091+
// Process files with various options.
2092+
// -v, --verbose Enable verbose output
2093+
// -h, --help. Prints command line options
2094+
// --output <arg> Output directory
2095+
// exit with code 0
20892096
```
20902097

20912098
```cjs
@@ -2108,7 +2115,7 @@ const options = {
21082115
},
21092116
};
21102117

2111-
// Get help text in result
2118+
// Get serialized help text in result
21122119
const result = parseArgs({
21132120
options,
21142121
help: 'My CLI Tool v1.0\n\nProcess files with various options.',
@@ -2126,12 +2133,20 @@ if (result.printUsage) {
21262133
}
21272134

21282135
// Or automatically print help and exit
2136+
const args = ['-h'];
21292137
parseArgs({
2138+
args,
21302139
options,
21312140
help: 'My CLI Tool v1.0\n\nProcess files with various options.',
2132-
enableHelpPrinting: true,
21332141
});
2134-
// Prints help and exits with code 0
2142+
// Prints:
2143+
// My CLI Tool v1.0
2144+
//
2145+
// Process files with various options.
2146+
// -v, --verbose Enable verbose output
2147+
// -h, --help. Prints command line options
2148+
// --output <arg> Output directory
2149+
// exit with code 0
21352150
```
21362151

21372152
### `parseArgs` `tokens`

lib/internal/util/parse_args/parse_args.js

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

@@ -361,7 +360,6 @@ const parseArgs = (config = kEmptyObject) => {
361360
validateBoolean(allowNegative, 'allowNegative');
362361
validateObject(options, 'options');
363362
validateString(help, 'help');
364-
validateBoolean(enableHelpPrinting, 'enableHelpPrinting');
365363
ArrayPrototypeForEach(
366364
ObjectEntries(options),
367365
({ 0: longOption, 1: optionConfig }) => {
@@ -465,12 +463,10 @@ const parseArgs = (config = kEmptyObject) => {
465463
});
466464

467465
const helpRequested = result.values.help;
468-
if (enableHelpPrinting && helpRequested) {
466+
if (help && helpRequested) {
469467
const console = require('internal/console/global');
470-
if (printUsage.length > 0 || help) {
468+
if (printUsage.length > 0) {
471469
console.log(printUsage);
472-
} else {
473-
console.log('No help text available.');
474470
}
475471
process.exit(0);
476472
} 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)