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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -554,11 +554,13 @@ program.on('option:verbose', function () {
process.env.VERBOSE = this.verbose;
});

// custom error on unknown command
program.on('command:*', function (operands) {
console.error(`Invalid command '${operands[0]}'. Did you mean:`);
console.error(mySuggestions(operands[0]));
process.exit(1);
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}'?`);
Comment on lines +560 to +562
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.

process.exitCode = 1;
});
```

Expand Down