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

Allow to provide examples of possible values when required option is not passed #2229

Closed
lcswillems opened this issue Jul 27, 2024 · 7 comments

Comments

@lcswillems
Copy link

Let's say that option --docker-image is required.

The user calls the command without specifying it. Commander.js will say that --docker-image must be provided. However, it might not be obvious for the user what is an example of value that would be accepted by this option.

Would there be a way to give examples of values for required options not passed?

@shadowspawn
Copy link
Collaborator

The default error handling shows the flags specified when the option was added. So there is a little information from the text for the option-value.

program.requiredOption('--docker-image <image-name>', 'choose docker image');
% node index.mjs       
error: required option '--docker-image <image-name>' not specified

@shadowspawn
Copy link
Collaborator

There isn't a way to attach custom error messages to a particular option.

You could manually override the error message by using a try/catch (and .exitOverride()), or by using .configureOutput() and overriding writeErr. In both cases you would need to inspect the error message string to see which option caused the error. I suspect this is more manual that you are interested in.

@lcswillems
Copy link
Author

Thanks for the information! Indeed, seems quite cumbersome to do in that case

@shadowspawn
Copy link
Collaborator

Providing extra information for a particular type of error is easier than a custom error per option, like:

program
  .exitOverride()
  .requiredOption('--docker-image <image-name>', 'choose docker image');

try {
  program.parse();
} catch (err) {
  if (err.code === 'commander.missingMandatoryOptionValue') {
    process.stderr.write('Example values for --docker-image are foo, bar\n');
  }
  process.exitCode = 1;
}
% node custom.mjs
error: required option '--docker-image <image-name>' not specified
Example values for --docker-image are foo, bar

@lcswillems
Copy link
Author

Interesting! 🙏 But what if I have multiple commands (in my case)?

@shadowspawn
Copy link
Collaborator

But what if I have multiple commands (in my case)?

Multiple commands with different required options? Hmmm...

Ok, I think you could override .exitOverride() on each command that has required options, and output an additional message in the callback. Reasonably tidy.

(Want to see an example of that?)

@shadowspawn
Copy link
Collaborator

An answer was provided, 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.

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