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

Closed
wants to merge 3 commits into from

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Apr 2, 2024

Pull Request

Problem

See: #1897
Related PR: #1908 #1910

Solution

This is a proof of concept adding a map of group titles to the Help configuration. This sort of mapping has some advantages compared with adding a group property to each Option:

  • specify group title once instead of multiple times
  • support for grouping help and version without needing to create custom Options
  • support for grouping user-created options without needing to create custom Options
  • (for Command groups, would support putting external commands into groups without extra work)

However, directly supplying the map does not feel very Commander-like. Adding a method on Command to add a single option group at a time might be more in the chaining style while retaining the practical advantages. Adding a group property to the Option would be more object-oriented, and other libraries allow this (#1897 (comment)).

Playing with the idea partly to think about how Help could be refactored to make this more feasible to implement with an author supplied override.

Example use:

const { program, Option } = require('commander');

program
  .option('-c, --carbon')
  .option('-r, --rabbit', 'cudly little friends')
  .option('-o, --oxygen')
  .addOption(new Option('-s, --sheep'))
  .option('--no-sheep') // negated
  .option('-n', 'neon') // short
  .option('--armadillo') // long
  .option('--zzz')
  .option('--dog', 'faithful furry companions')
  .option('--no-dog')

program.configureHelp({
  optionGroups: {
    'Animal Options:': ['rabbit', 'armadillo', 'sheep', 'dog'],
    'Element Options:': ['carbon', 'oxygen', 'n' /* neon */],
    'Help Options:': ['help']
    }
  }),

program.parse();
% node map2-options.js --help
Usage: map2-options [options]

Options:
  --zzz

Animal Options:
  -r, --rabbit  cudly little friends
  -s, --sheep
  --no-sheep
  --armadillo
  --dog         faithful furry companions
  --no-dog

Element Options:
  -c, --carbon
  -o, --oxygen
  -n            neon

Help Options:
  -h, --help    display help for command

@shadowspawn
Copy link
Collaborator Author

Decent proof of concept. Still wishing was easier for authors to customise the behaviour in large formatHelp without copying.

A defect in this PR is using .configureHelp() in a subcommand to get grouping will replace (lose) other changes made at root level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant