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

How to display the full help of a command when an error occures? #1423

Closed
electrotype opened this issue Dec 23, 2020 · 5 comments
Closed

How to display the full help of a command when an error occures? #1423

electrotype opened this issue Dec 23, 2020 · 5 comments

Comments

@electrotype
Copy link

electrotype commented Dec 23, 2020

Let's say an option is required but is missing. Commander then automatically displays a short "error: required option '--myOption' not specified" message. But what if, in addition to this message, I also want to display the full help of the command? Is this possible?

And, related question:

I do use exitOverride() and a big try/catch block. After parseAsync() has been called on a command with subcommands, is there a way to know what is the subcommand that was actually called? If an error occures and I could know the subcommand in my catch block, I guess I could display the help by myself (using subCommand.outputHelp()).

@shadowspawn
Copy link
Collaborator

Hmm, there is not direct support for either.

(Perhaps the CommanderError could include the failing command as a property.)

On a related note, the unknown command error got modified to include a reference to the help.

% fab unknown
error: unknown command 'unknown'. See 'fab --help'.

@shadowspawn
Copy link
Collaborator

I think you could display the appropriate help by subclassing and overriding .command to add a command specific .exitOverride. Interested, or too heavy-weight?

@electrotype
Copy link
Author

Inspired by your suggestions:

command.exitOverride(err => {
  if (err.code !== 'commander.executeSubCommandAsync') {
    err.command = command;
    throw err;
  } else {
    // Async callback from spawn events, not useful to throw.
  }
});

I then have access to the command in my catch block. Works perfectly!

Thanks.

@shadowspawn
Copy link
Collaborator

An answer was worked out, and no further activity in a month. Closing this as resolved.

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

@shadowspawn
Copy link
Collaborator

Related: .showHelpAfterError() added in prerelease v8.0.0-2 by PR #1534

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

2 participants