Skip to content

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

Merged
shadowspawn merged 1 commit intotj:developfrom
shadowspawn:feature/didyoumean
Feb 8, 2020
Merged

Update README example for .on('command:*')#1176
shadowspawn merged 1 commit intotj:developfrom
shadowspawn:feature/didyoumean

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.

3 participants