Commander.js: Have in-house support in-order to recommend matching commands

Created on 11 Aug 2019  路  15Comments  路  Source: tj/commander.js

yargs comes shipped with a recommendCommands() method as part of the public API. It would be great if commander.js would have it as well :clap:

program
  .name('sample')
  .usage('<command>)
  .version(version)
  .recommendCommands();

program
  .command('init')
  .action(() => {});

program
  .command('create');
  .action(() => {});

sample creat

Unknown command creat.
Did you mean create?
enhancement

Most helpful comment

Only for me, I think it's not necessary. People can realize it by

program.command("*").action(cmd => {
  suggestCommand(cmd);
});

And we can find a npm package named didyoumean to customzie suggestCommand(cmd) easily.

I'm not familiar with yargs, it seems that it doesn't have an api to match regex cmd like '*' so it has recommendCommands() to handle this. But commander do.

All 15 comments

Only for me, I think it's not necessary. People can realize it by

program.command("*").action(cmd => {
  suggestCommand(cmd);
});

And we can find a npm package named didyoumean to customzie suggestCommand(cmd) easily.

I'm not familiar with yargs, it seems that it doesn't have an api to match regex cmd like '*' so it has recommendCommands() to handle this. But commander do.

I do not think it likely I would accept a PR for this currently, as we still have some improvements to make in whether Commander warns about unrecognised commands in the first place. (So not "in-house support".)

I am interested in how a user might add possible matches, and currently that would be in their own code or in an add-on package. Also interested in how popular an idea this is, so feel free to leave this issue open even if you decide not to investigate yourself.

My limited knowledge (not researched) is most computer programs use the Levenshtein distance algorithm. I was casually looking at didyoumean and didyoumean2 in weekend. (Using these as examples rather than recommendations.)

(Both roll-your-own solutions and didyoumean also suggested by @oGsLP, thanks for comment )

I came across an implementation in comments: https://github.com/tj/commander.js/issues/432#issuecomment-175785162

@shadowspawn what about documenting how to achieve it with didyoumean.
I would happily submit a PR :+1:

Same feedback as building it in, I do not think it likely I would accept a PR for this currently. If the code is not huge, you could simply add it as a comment here though.

A typical implementaion would be:-

const didYouMean = require('didyoumean');

const suggestCommands = cmd => {
  const availableCommands = program.commands.map(c => c._name);

  const suggestion = didYouMean(cmd, availableCommands);
  if (suggestion) {
    console.log(`Did you mean ${suggestion}?`));
  }
};

program.arguments('<command>').action(cmd => {
  program.outputHelp();
  console.log(`Unknown command ${cmd}.`));
  console.log();
  suggestCommands(cmd);
});

I have opened a PR to use didYouMean in the README as the example for an unrecognised command #1176

You could add the require to https://github.com/tj/commander.js/issues/1015#issuecomment-536262298

Ig it would make much sense for a dedicated section to be introduced within README containing the instructions for command suggestion implementation.

I do not wish to put a dedicated section in the README for this.

I have considered adding a separate page and examples for such code, and possibly links to modules which implement add-ons to Commander and the like. There has not been compelling enough information so far, but this is one such example.

Sounds good :+1:

But the present state with #1176 is essentially causing trouble.

This issue didn't get any upvotes in six months. I am happy to leave it up to authors for now to roll their own suggestions.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oknoorap picture oknoorap  路  4Comments

DeoLeung picture DeoLeung  路  4Comments

shadowspawn picture shadowspawn  路  4Comments

mathiasbynens picture mathiasbynens  路  3Comments

aminch picture aminch  路  3Comments