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

Update README example for .on('command:*') #1176

Merged
merged 1 commit into from Feb 8, 2020

Conversation

shadowspawn
Copy link
Collaborator

Pull Request

Problem

A handler for .on('command:*') is no longer needed just to show an error message, as we do that by default, so would like a new example in the README.

It was suggested we add built-in support to recommend matching command, but do not want to that for now since it would add a dependency: #1015

The example code directly calls process.exit() which is not recommended.

Was:

// error on unknown commands
program.on('command:*', function () {
  console.error('Invalid command: %s\nSee --help for a list of available commands.', program.args.join(' '));
  process.exit(1);
});

Solution

Use actual code for didYouMean as the example.

Follow best practice and do not directly call .exit()

program.on('command:*', function (operands) {
  console.error(`error: unknown command '${operands[0]}'`);
  const availableCommands = program.commands.map(cmd => cmd.name());
  const suggestion = didYouMean(operands[0], availableCommands);
  if (suggestion)
    console.error(`Did you mean '${suggestion}'?`);
  process.exitCode = 1;
});

Copy link
Collaborator

@abetomo abetomo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +560 to +562
const suggestion = didYouMean(operands[0], availableCommands);
if (suggestion)
console.error(`Did you mean '${suggestion}'?`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be misleading. Say for instance, a first time user makes use of this code snippet to handle unknown commands ending up with an error saying that didYouMean is not defined.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative I considered was to use a clearly write-your-own routine like:

const availableCommands = program.commands.map(cmd => cmd.name());
const suggestion = myFindBestMatch(operands[0], availableCommands);
console.error(`Did you mean '${suggestion}'?`);
process.exitCode = 1;

Do you think this would be preferable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

3 participants