Skip to content

Add support for custom help groups #1897

Closed
@smeijer

Description

@smeijer

I think it'd be helpful if we had a way to group commands and options, to create sections in --help.

There's a prior issue to this; #78, but that's over 9 years old. I'm also aware of addHelpText (#1296), but when using that to group commands, it adds boilerplate due to the need of needing to "hide" commands, and later formatting and properly indenting help ourselves.

What I wish to achieve is something like:

Usage: magicbell [options] [command]

Work with MagicBell from the command line

Options:
  -V, --version             output the version number
  -p, --profile <string>    Profile to use (default: "default")
  -h, --help                display help for command

Commands:
  broadcasts                Manage broadcasts
  imports                   Manage imports
  listen                    Listen to events for a users inbox
  metrics                   Manage metrics
  notification-preferences  Manage notification preferences
  notifications             Send and retrieve notifications
  push-subscriptions        Manage push subscriptions
  subscriptions             Manage subscriptions
  users                     Manage all known users

Other commands:
  config                    Display or change config values
  login                     Login to your account
  logout                    Logout of your account
  help                      Display help for a command

This separates 'config commands' from 'core commands'. If you'd run stripe --help or gh --help you can see similar command grouping.

At this moment, I achieve the above via:

for (const command of commands) {
  program.addCommand(command);
}

for (const command of otherCommands) {
  program.addCommand(command, { hidden: true });
}

program.addHelpCommand(false);
const padding = Object.values([...commands, ...otherCommands]).reduce((acc, command) => {
  return Math.max(acc, command.name().length);
}, 0);

program.addHelpText(
  'after',
  `
Other commands:
  ${otherCommands.map((c) => `${c.name().padEnd(padding, ' ')}  ${c.description()}`).join('\n  ')}
  ${'help'.padEnd(padding, ' ')}  Display help for a command
`,
);

That's doable, but I think it'd be nice to have native support for it. Something like:

program.addCommand(command, { group: 'Other commands' });
program.addOption(option, { group: 'Global flags' });

The group can both serve as group title, and grouping key.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions