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 helpGroup for options and commands #1910

Closed
wants to merge 4 commits into from

Conversation

shadowspawn
Copy link
Collaborator

@shadowspawn shadowspawn commented Jul 18, 2023

Problem

Some people with larger programs would like to break up their commands and options into groups in the help. There have not been a lot of requests for this, but it is something I had noticed as a possible enhancement.

See: #78 #374 (comment) #1897

Help groups are common in "big" programs, but big programs often have custom help systems anyway! (e.g. git, npm)

Solution

Add .helpGroup() to Command and Option. Use the help groups as titles in the help.

To support built-in features and external commands, add {helpGroup} configuration to:

  • .version()
  • .helpOption()
  • .addHelpCommand()
  • external command configuration
program.command('build').helpGroup('Demo Commands:').action(() => {});
program.command('serve', 'external command', { helpGroup: 'Demo Commands:' });
program.addOption(new Option('-e, --example').helpGroup('Demo Options:'));
program.version('1.2.3', '--version', 'Show version', { helpGroup: 'Demo Options:' });
program.helpOption('-s, --support', 'Show help', { helpGroup: 'Demo Options:' });
program.addHelpCommand('help [command]', 'Show help', { helpGroup: 'Demo Commands:' });
% node pr-example.js
Usage: pr-example [options] [command]

Demo Options:
  -e, --example
  --version       Show version
  -s, --support   Show help

Demo Commands:
  build
  serve           external command
  help [command]  Show help

ChangeLog

@sinedied
Copy link

sinedied commented Aug 2, 2023

Looks interesting, and would perfectly fix my current issue :)

@shadowspawn
Copy link
Collaborator Author

I am still wondering about:

program.version('1.2.3', '--version', 'Show version', { helpGroup: 'Demo Options:' });
program.helpOption('-s, --support', 'Show help', { helpGroup: 'Demo Options:' });
program.addHelpCommand('help [command]', 'Show help', { helpGroup: 'Demo Commands:' });

I want to think a bit more about consistency across the API and whether an options-bag is the best approach.

(And have been busy reviewing other PRs.)

@sinedied
Copy link

sinedied commented Aug 7, 2023

It's not the prettiest but I can't think of any better alternative, and it makes sense in regard to the existing api.
It's also probably not something that everyone wants to use all the time, so having it as an extra option seems the best approach to me.

@shadowspawn
Copy link
Collaborator Author

(Thanks for comments @sinedied )

@shadowspawn
Copy link
Collaborator Author

If I can't decide for now on a built-in way of customising the version option and help option and help command, could maybe leave it to the author! Thinking of this as an interim work-around rather than permanent solution.

class MyCommand extends Command {
  createCommand(name) {
    const cmd =  new MyCommand(name);

    // Set group on the special built-in command.
    if (cmd.name() === 'help')
      cmd.helpGroup('Demo Commands:');

    return cmd;
  }

  createOption(flags, description) {
    const option = super.createOption(flags, description);

    // Set group on the special built-in options.
    if (option.long === '--version' || option.long === '--help')
      option.helpGroup('Demo Options:');

    return option;
  }
}

@sinedied
Copy link

This would be totally fine for me given that's probably an infrequent use case.
Though the . helpGroup() method isn't implemented yet, right?

@shadowspawn
Copy link
Collaborator Author

Though the .helpGroup() method isn't implemented yet, right?

Correct. Still need that work from this PR.

@shadowspawn
Copy link
Collaborator Author

There hasn't been much interest in this feature to date. There is a reasonable chunk of work to do a full solution. Putting this draft away for now.

This issue prompted a couple of refactors so may be easier to customise built-in help option and help command in future: #2006 #2087

I liked "section" approach used in #1897 example implementation which could possibly also help with #1870. (Not included in this draft.)

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.

None yet

2 participants