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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have in-house support in-order to recommend matching commands #1015

Closed
jamesgeorge007 opened this issue Aug 11, 2019 · 15 comments
Closed

Have in-house support in-order to recommend matching commands #1015

jamesgeorge007 opened this issue Aug 11, 2019 · 15 comments
Assignees

Comments

@jamesgeorge007
Copy link
Contributor

jamesgeorge007 commented Aug 11, 2019

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 馃憦

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?
@oGsLP
Copy link
Contributor

oGsLP commented Aug 14, 2019

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.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Aug 14, 2019

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 )

@shadowspawn
Copy link
Collaborator

I came across an implementation in comments: #432 (comment)

@jamesgeorge007
Copy link
Contributor Author

@shadowspawn what about documenting how to achieve it with didyoumean.
I would happily submit a PR 馃憤

@shadowspawn
Copy link
Collaborator

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.

@jamesgeorge007
Copy link
Contributor Author

jamesgeorge007 commented Sep 29, 2019

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);
});

@shadowspawn
Copy link
Collaborator

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

@jamesgeorge007
Copy link
Contributor Author

@jamesgeorge007
Copy link
Contributor Author

https://github.com/tj/commander.js/pull/1176/files#r376756601

#1181 does makes sense in this regard.

@shadowspawn

This comment has been minimized.

@jamesgeorge007
Copy link
Contributor Author

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

@shadowspawn
Copy link
Collaborator

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.

@jamesgeorge007
Copy link
Contributor Author

Sounds good 馃憤

@jamesgeorge007
Copy link
Contributor Author

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

@shadowspawn
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants