Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5,308 changes: 2,705 additions & 2,603 deletions doc/api/cli.md

Large diffs are not rendered by default.

119 changes: 80 additions & 39 deletions lib/internal/main/print_help.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const {
ArrayPrototypeConcat,
ArrayPrototypeMap,
ArrayPrototypePush,
ArrayPrototypeSort,
Boolean,
MathFloor,
Expand All @@ -16,7 +18,7 @@ const {
StringPrototypeTrimStart,
} = primordials;

const { types } = internalBinding('options');
const { helpSections, types } = internalBinding('options');
const hasCrypto = Boolean(process.versions.openssl);

const {
Expand Down Expand Up @@ -77,21 +79,21 @@ const envVars = new SafeMap(ArrayPrototypeConcat([
'libuv\'s threadpool' }],
], hasIntl ? [
['NODE_ICU_DATA', { helpText: 'data path for ICU (Intl object) data' +
hasSmallICU ? '' : ' (will extend linked-in data)' }],
] : []), (hasNodeOptions ? [
(hasSmallICU ? '' : ' (will extend linked-in data)') }],
] : [], hasNodeOptions ? [
['NODE_OPTIONS', { helpText: 'set CLI options in the environment via a ' +
'space-separated list' }],
] : []), hasCrypto ? [
] : [], hasCrypto ? [
['OPENSSL_CONF', { helpText: 'load OpenSSL configuration from file' }],
['SSL_CERT_DIR', { helpText: 'sets OpenSSL\'s directory of trusted ' +
'certificates when used in conjunction with --use-openssl-ca' }],
['SSL_CERT_FILE', { helpText: 'sets OpenSSL\'s trusted certificate file ' +
'when used in conjunction with --use-openssl-ca' }],
] : []);
] : []));


function indent(text, depth) {
return RegExpPrototypeSymbolReplace(/^/gm, text, StringPrototypeRepeat(' ', depth));
return RegExpPrototypeSymbolReplace(/^(?!$)/gm, text, StringPrototypeRepeat(' ', depth));
}

function fold(text, width) {
Expand Down Expand Up @@ -121,12 +123,7 @@ function getArgDescription(type) {
}
}

function format(
{ options, aliases = new SafeMap(), firstColumn, secondColumn, nameStyle = [] },
) {
let text = '';
let maxFirstColumnUsed = 0;

function getEntries(options, aliases = new SafeMap()) {
const sortedOptions = ArrayPrototypeSort(
[...options.entries()],
({ 0: name1, 1: option1 }, { 0: name2, 1: option2 }) => {
Expand All @@ -140,6 +137,7 @@ function format(
},
);

const entries = [];
for (const {
0: name, 1: { helpText, type, defaultIsTrue },
} of sortedOptions) {
Expand Down Expand Up @@ -181,29 +179,51 @@ function format(
displayHelpText += ' (currently set)';
}

ArrayPrototypePush(entries, { displayName, helpText: displayHelpText });
}
return entries;
}

// Returns the width of the first column, shrunk to fit the longest name across
// all of the given entry lists so that every section lines up.
function fitFirstColumn(entryLists, firstColumn) {
let maxFirstColumnUsed = 0;
for (const entries of entryLists) {
for (const { displayName } of entries) {
maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length);
}
}
// If we have more than 4 blank gap spaces, reduce first column width.
if (maxFirstColumnUsed < firstColumn - 4)
return maxFirstColumnUsed + 2;
return firstColumn;
}

function format(entries, firstColumn, secondColumn, nameStyle) {
let text = '';
for (const { displayName, helpText } of entries) {
text += style(nameStyle, displayName);
maxFirstColumnUsed = MathMax(maxFirstColumnUsed, displayName.length);
if (displayName.length >= firstColumn)
text += '\n' + StringPrototypeRepeat(' ', firstColumn);
else
text += StringPrototypeRepeat(' ', firstColumn - displayName.length);

text += StringPrototypeTrimStart(
indent(fold(displayHelpText, secondColumn), firstColumn)) + '\n';
indent(fold(helpText, secondColumn), firstColumn)) + '\n';
}
return text;
}

if (maxFirstColumnUsed < firstColumn - 4) {
// If we have more than 4 blank gap spaces, reduce first column width.
return format({
options,
aliases,
firstColumn: maxFirstColumnUsed + 2,
secondColumn,
nameStyle,
});
// Splits the options into the --help sections that node_options.cc assigns
// them to, in the order they are defined in node_options.h.
function groupOptions(options) {
const grouped = ArrayPrototypeMap(helpSections, (title) => ({
title, options: new SafeMap(),
}));
for (const { 0: name, 1: info } of options) {
grouped[info.helpSection].options.set(name, info);
}

return text;
return grouped;
}

function print(stream) {
Expand All @@ -215,24 +235,45 @@ function print(stream) {
const firstColumn = MathFloor(width * 0.4);
const secondColumn = MathFloor(width * 0.57);

// These are handled by the argument parser rather than defined as options.
// Section 0 is HelpSection::kGeneral.
options.set('-', { helpText: 'script read from stdin ' +
'(default if no file name is provided, ' +
'interactive mode if a tty)' });
options.set('--', { helpText: 'indicate the end of node options' });
let helpText = (
style('bold',
'Usage: node [options] [ script.js ] [arguments]\n' +
' node inspect [options] [ script.js | host:port ] [arguments]') +
'\n\n' + style('bold', 'Options:') + '\n');
helpText += (indent(format({
options, aliases, firstColumn, secondColumn, nameStyle: ['bold', 'green'],
}), 2));
'interactive mode if a tty)',
helpSection: 0 });
options.set('--', { helpText: 'indicate the end of node options',
helpSection: 0 });

const optionSections = [];
for (const { title, options: sectionOptions } of groupOptions(options)) {
const entries = getEntries(sectionOptions, aliases);
if (entries.length > 0) ArrayPrototypePush(optionSections, { title, entries });
}
const optionsFirstColumn = fitFirstColumn(
ArrayPrototypeMap(optionSections, ({ entries }) => entries), firstColumn);

// One line per top-level run mode, i.e. each way of choosing what the
// process runs.
let helpText = style('bold',
'Usage: node [options] [ script.js ] [arguments]\n' +
' node [options] -e "script" [arguments]\n' +
' node [options] -p "script" [arguments]\n' +
' node inspect [options] [ script.js | host:port ] [arguments]\n' +
' node --run [ script ] [-- arguments]\n' +
' node --watch [options] script.js [arguments]\n' +
' node --test [options] [ patterns... ]\n' +
' node --bench --experimental-bench [options] patterns...') +
'\n';
for (const { title, entries } of optionSections) {
helpText += '\n' + style('bold', `${title}:`) + '\n';
helpText += indent(format(entries, optionsFirstColumn, secondColumn,
['bold', 'green']), 2);
}

const envEntries = getEntries(envVars);
helpText += ('\n' + style('bold', 'Environment variables:') + '\n');

helpText += (format({
options: envVars, firstColumn, secondColumn, nameStyle: ['bold', 'magenta'],
}));
helpText += format(envEntries, fitFirstColumn([envEntries], firstColumn),
secondColumn, ['bold', 'magenta']);

helpText += ('\nDocumentation can be found at ' +
style(['blue', 'underline'], 'https://nodejs.org/'));
Expand Down
1 change: 1 addition & 0 deletions src/env_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
V(h2_string, "h2") \
V(handle_string, "handle") \
V(hash_algorithm_string, "hashAlgorithm") \
V(help_section_string, "helpSection") \
V(help_text_string, "helpText") \
V(homedir_string, "homedir") \
V(host_string, "host") \
Expand Down
17 changes: 17 additions & 0 deletions src/node_options-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace options_parser {
template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
bool (*getter)(Options*),
void (*setter)(Options*, bool),
OptionEnvvarSettings env_setting,
Expand All @@ -44,13 +45,15 @@ void OptionsParser<Options>::AddOption(const char* name,
std::make_shared<BitFieldOptionField>(getter, setter),
env_setting,
help_text,
help_section,
default_is_true,
NamespaceEnumToString(namespace_id)});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
uint64_t Options::*field,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id,
Expand All @@ -61,6 +64,7 @@ void OptionsParser<Options>::AddOption(const char* name,
std::make_shared<SimpleOptionField<uint64_t>>(field),
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id),
strict});
Expand All @@ -69,6 +73,7 @@ void OptionsParser<Options>::AddOption(const char* name,
template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
int64_t Options::*field,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id) {
Expand All @@ -78,13 +83,15 @@ void OptionsParser<Options>::AddOption(const char* name,
std::make_shared<SimpleOptionField<int64_t>>(field),
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id)});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
std::string Options::*field,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id) {
Expand All @@ -94,13 +101,15 @@ void OptionsParser<Options>::AddOption(const char* name,
std::make_shared<SimpleOptionField<std::string>>(field),
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id)});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
std::vector<std::string> Options::*field,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id) {
Expand All @@ -111,13 +120,15 @@ void OptionsParser<Options>::AddOption(const char* name,
std::make_shared<SimpleOptionField<std::vector<std::string>>>(field),
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id)});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
HostPort Options::*field,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id) {
Expand All @@ -127,13 +138,15 @@ void OptionsParser<Options>::AddOption(const char* name,
std::make_shared<SimpleOptionField<HostPort>>(field),
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id)});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
NoOp no_op_tag,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id) {
Expand All @@ -142,13 +155,15 @@ void OptionsParser<Options>::AddOption(const char* name,
nullptr,
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id)});
}

template <typename Options>
void OptionsParser<Options>::AddOption(const char* name,
const char* help_text,
HelpSection help_section,
V8Option v8_option_tag,
OptionEnvvarSettings env_setting,
OptionNamespaces namespace_id) {
Expand All @@ -157,6 +172,7 @@ void OptionsParser<Options>::AddOption(const char* name,
nullptr,
env_setting,
help_text,
help_section,
false,
NamespaceEnumToString(namespace_id)});
}
Expand Down Expand Up @@ -241,6 +257,7 @@ auto OptionsParser<Options>::Convert(
Convert(original.field, get_child),
original.env_setting,
original.help_text,
original.help_section,
original.default_is_true,
original.namespace_id,
original.strict};
Expand Down
Loading