Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sections to allow some customisation of help layout #2196

Closed
wants to merge 1 commit into from
Closed
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
78 changes: 56 additions & 22 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,21 @@ class Help {
return argument.description;
}

/**
* visibleSections
*
* @typedef {object} Section
* @property {string} name
* @property {string} title - may be empty
* @property {string} body
*
* @param {Section[]} sections
* @returns
*/
visibleSections(sections) {
return sections.filter((section) => !!section.body);
}

/**
* Generate the built-in help text.
*
Expand All @@ -384,20 +399,27 @@ class Help {
return term;
}
function formatList(textArray) {
if (textArray.length === 0) return '';
return textArray.join('\n').replace(/^/gm, ' '.repeat(itemIndentWidth));
}

// @type {Section[]}
const sections = [];

// Usage
let output = [`Usage: ${helper.commandUsage(cmd)}`, ''];
sections.push({
name: 'usage',
title: '',
body: `Usage: ${helper.commandUsage(cmd)}`,
});

// Description
const commandDescription = helper.commandDescription(cmd);
if (commandDescription.length > 0) {
output = output.concat([
helper.wrap(commandDescription, helpWidth, 0),
'',
]);
}
sections.push({
name: 'description',
title: '',
body: helper.wrap(commandDescription, helpWidth, 0),
});

// Arguments
const argumentList = helper.visibleArguments(cmd).map((argument) => {
Expand All @@ -406,9 +428,11 @@ class Help {
helper.argumentDescription(argument),
);
});
if (argumentList.length > 0) {
output = output.concat(['Arguments:', formatList(argumentList), '']);
}
sections.push({
name: 'arguments',
title: 'Arguments:',
body: formatList(argumentList),
});

// Options
const optionList = helper.visibleOptions(cmd).map((option) => {
Expand All @@ -417,9 +441,11 @@ class Help {
helper.optionDescription(option),
);
});
if (optionList.length > 0) {
output = output.concat(['Options:', formatList(optionList), '']);
}
sections.push({
name: 'options',
title: 'Options:',
body: formatList(optionList),
});

if (this.showGlobalOptions) {
const globalOptionList = helper
Expand All @@ -430,13 +456,11 @@ class Help {
helper.optionDescription(option),
);
});
if (globalOptionList.length > 0) {
output = output.concat([
'Global Options:',
formatList(globalOptionList),
'',
]);
}
sections.push({
name: 'globalOptions',
title: 'Global Options:',
body: formatList(globalOptionList),
});
}

// Commands
Expand All @@ -447,10 +471,20 @@ class Help {
);
});
if (commandList.length > 0) {
output = output.concat(['Commands:', formatList(commandList), '']);
sections.push({
name: 'commands',
title: 'Commands:',
body: formatList(commandList),
});
}

return output.join('\n');
const visibleSections = this.visibleSections(sections);

const chunks = visibleSections.reduce((chunks, section) => {
if (section.title) return chunks.concat(section.title, section.body, '');
return chunks.concat(section.body, '');
}, []);
return chunks.join('\n');
}

/**
Expand Down