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

Customize help message for a command #1023

Closed
RonWang opened this issue Aug 18, 2019 · 11 comments
Closed

Customize help message for a command #1023

RonWang opened this issue Aug 18, 2019 · 11 comments
Milestone

Comments

@RonWang
Copy link

RonWang commented Aug 18, 2019

Our goal: customize the help message for the root command.

Existing solution won't help, because we need to append the message on top of auto-gen help, and also modify the auto-gen help a little bit.

Context for our cmd app: below is the root cmd, and we have a sub-cmd under the root as well as a git-style-sub-command.

commander
    .description('')
    .command('sub-cmd')
    .command('git-style-sub-cmd')
    .version(require('../package.json').version)
    .option('-h, --help', 'output help message', () => {
        console.log('custom help msg');
    })
    .parse(process.argv);

In the example I tried using option('-h, --help'), the console.log works for me with my demanded help output. But when I try sub-cmd and git-style-sub-cmd, the help message for the root command is always coming along when I type "cmd sub-cmd --help" and "cmd git-style-sub-cmd --help".

I noticed #242 , where @shadowspawn mentioned it won't be supported. Reopen it as it'd be an important feature for us.

@RonWang
Copy link
Author

RonWang commented Aug 18, 2019

lol great post! #1111111111(2)

@shadowspawn
Copy link
Collaborator

I'll try and explain what is happening with what you tried.

    .option('-h, --help', 'output help message', () => {
        console.log('custom help msg');
    })

This adds an option which effectively overrides the built-in help since it uses the same flags. The callback is used for "Custom option processing" and covered in the README.

Flags are treated as "global" and these three calls get processed much the same way and call your action handler:

my-command --help
my-command --help sub-cmd
my-command sub-cmd --help

This approach would let you customise the help for your main command, but you do miss out on the built-in support for help on the subcommands.

@shadowspawn
Copy link
Collaborator

Related issue, prefix help with some text: #578

@madanalogy

This comment has been minimized.

@shadowspawn

This comment has been minimized.

@madanalogy

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

Currently there is a single event which is emitted after the help is displayed, --help.

A possible approach for allowing more control might be to emit three events:

  • help:header
  • help:body
  • help:footer

If there is a listener for help:body then it is called instead of the normal help.

@shadowspawn
Copy link
Collaborator

This issue has not had any activity in over six months. It isn't likely to get acted on due to this report.

I quite like the help:header, help:body, help:footer idea, and will be keeping an eye on new issues to see if it would be useful.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

@shadowspawn shadowspawn added the pending release Merged into a branch for a future release, but not released yet label Sep 9, 2020
@shadowspawn shadowspawn added this to the v7.0.0 milestone Sep 9, 2020
@shadowspawn shadowspawn removed the pending release Merged into a branch for a future release, but not released yet label Jan 15, 2021
@DiaxManPl
Copy link

Can anyone give me example usage of help:body?

@shadowspawn
Copy link
Collaborator

The help:body pattern did not get implemented. The features that did get added were .addHelpText() and .configureHelp(), and a Help class for more complex overrides.

@DiaxManPl
Copy link

Ok. Sorry.

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

No branches or pull requests

4 participants