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

Editing error #1498

Closed
DiaxManPl opened this issue Apr 7, 2021 · 6 comments
Closed

Editing error #1498

DiaxManPl opened this issue Apr 7, 2021 · 6 comments

Comments

@DiaxManPl
Copy link

Is there any way to edit this:
image
I want to make custom "unknown option" text.

@shadowspawn
Copy link
Collaborator

There is not direct support for changing the error messages.

You could perhaps override outputError and intercept the message, or if you want additional text after the current message is displayed could catch the matching error and add the text.

https://github.com/tj/commander.js#override-exit-and-output-handling

@DiaxManPl
Copy link
Author

Thanks. outputError doesn't work, but custom error handling does.

@DiaxManPl
Copy link
Author

Oh. Custom error handling does not replace the message. It just adds custom text to the message.

@DiaxManPl DiaxManPl reopened this Apr 8, 2021
@shadowspawn
Copy link
Collaborator

This is how I thought outputError could be used.

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

program.option('-p, --port <number>');

program.configureOutput({
  outputError: (str, write) => {
    if (str.startsWith('error: unknown option'))
      write('custom unknown option error\n');
    else
      write(str);
  }
});

program.parse();
$ node index.js --port
error: option '-p, --port <number>' argument missing
$ node index.js --surprise
custom unknown option error

@DiaxManPl
Copy link
Author

Thanks a lot! It works!

@shadowspawn
Copy link
Collaborator

There is an open issue about adding support for modifying and/or localising error messages: #1801

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