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 support for help groups for options #2176

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 42 additions & 5 deletions lib/help.js
Expand Up @@ -15,6 +15,7 @@ class Help {
this.sortSubcommands = false;
this.sortOptions = false;
this.showGlobalOptions = false;
this.optionGroups = {};
}

/**
Expand Down Expand Up @@ -330,6 +331,41 @@ class Help {
return argument.description;
}

/**
*
* @param {Command} cmd
* @param {Help} helper
* @returns { object }
*/
visibleOptionGroups(cmd, helper) {
const result = { 'Options:': [] };

// Invert the optionGroups object to a map of optionName to groupName.
const groupLookup = new Map();
Object.keys(this.optionGroups).concat().forEach((title) => {
result[title] = [];
this.optionGroups[title].forEach(optionName => {
// (only supporting option appearing in one group for now, last one wins)
groupLookup.set(optionName, title);
});
});

// Build list of options in each group title (in order as returned by visibleOptions).
helper.visibleOptions(cmd).forEach((option) => {
const title = groupLookup.get(option.attributeName()) ?? 'Options:';
result[title].push(option);
});

// Remove empty groups.
Object.keys(result).forEach((title) => {
if (result[title].length === 0) {
delete result[title];
}
});

return result;
}

/**
* Generate the built-in help text.
*
Expand Down Expand Up @@ -372,12 +408,13 @@ class Help {
}

// Options
const optionList = helper.visibleOptions(cmd).map((option) => {
return formatItem(helper.optionTerm(option), helper.optionDescription(option));
const visibleOptionGroups = helper.visibleOptionGroups(cmd, helper);
Object.keys(visibleOptionGroups).forEach((groupTitle) => {
const optionList = visibleOptionGroups[groupTitle].map((opt) => {
return formatItem(helper.optionTerm(opt), helper.optionDescription(opt));
});
output = output.concat([groupTitle, formatList(optionList), '']);
});
if (optionList.length > 0) {
output = output.concat(['Options:', formatList(optionList), '']);
}

if (this.showGlobalOptions) {
const globalOptionList = helper.visibleGlobalOptions(cmd).map((option) => {
Expand Down