Skip to content

Commit

Permalink
bootstrap: print --help message using console.log
Browse files Browse the repository at this point in the history
PR-URL: nodejs#51463
Fixes: nodejs#51448
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Marco Ippolito <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
  • Loading branch information
jcbhmr authored and bmeck committed Jun 22, 2024
1 parent 8958d92 commit baf5e39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
12 changes: 7 additions & 5 deletions lib/internal/main/print_help.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ function format(

function print(stream) {
const { options, aliases } = getCLIOptionsInfo();
const console = require('internal/console/global');

// Use 75 % of the available width, and at least 70 characters.
const width = MathMax(70, (stream.columns || 0) * 0.75);
Expand All @@ -212,21 +213,22 @@ function print(stream) {
'(default if no file name is provided, ' +
'interactive mode if a tty)' });
options.set('--', { helpText: 'indicate the end of node options' });
stream.write(
let helpText = (
'Usage: node [options] [ script.js ] [arguments]\n' +
' node inspect [options] [ script.js | host:port ] [arguments]\n\n' +
'Options:\n');
stream.write(indent(format({
helpText += (indent(format({
options, aliases, firstColumn, secondColumn,
}), 2));

stream.write('\nEnvironment variables:\n');
helpText += ('\nEnvironment variables:\n');

stream.write(format({
helpText += (format({
options: envVars, firstColumn, secondColumn,
}));

stream.write('\nDocumentation can be found at https://nodejs.org/\n');
helpText += ('\nDocumentation can be found at https://nodejs.org/');
console.log(helpText);
}

prepareMainThreadExecution();
Expand Down
13 changes: 12 additions & 1 deletion test/parallel/test-cli-node-print-help.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const common = require('../common');
// returns the proper set of cli options when invoked

const assert = require('assert');
const { exec } = require('child_process');
const { exec, spawn } = require('child_process');
const { once } = require('events');
let stdOut;


Expand Down Expand Up @@ -53,3 +54,13 @@ function testForSubstring(options) {
}

startPrintHelpTest();

// Test closed stdout for `node --help`. Like `node --help | head -n5`.
(async () => {
const cp = spawn(process.execPath, ['--help'], {
stdio: ['inherit', 'pipe', 'inherit'],
});
cp.stdout.destroy();
const [exitCode] = await once(cp, 'exit');
assert.strictEqual(exitCode, 0);
})().then(common.mustCall());

0 comments on commit baf5e39

Please sign in to comment.