dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Hinting command when user types an invalid command #151

Open minhchu opened 8 years ago

minhchu commented 8 years ago

Maybe we can add an additional implementation to display similiar command when user types an invalid command Example:

vorpal
    .command('foo', 'Outputs "foo".')
    .action(function(args, callback()) {
        this.log('foo');
        callback();
    });

vorpal
    .catch('[words...]', 'Catches incorrect commands')
    .action(function (args, callback()) {                
        this.log(args.words.join(' ') + ' is not a valid command.');
        // new function
        // something like vorpal.similiar(args.words[0])
        // to display similiar commands
        callback();
    });

When user type node command.js foozz, it will output:

foozz is not a valid command. Maybe you mean:
    foo
dthree commented 8 years ago

Good idea. I would probably want to t urn this on by default, and only if there isn't a vorpal.catch command declared. Then it could be turned off like:

const vorpal = require('vorpal');

vorpal.delimiter('foo').hints(false).show();

What do you think?


You could probably do a PR on this pretty easily, just use the leven module for Levenshtein distance by @sindresorhus , which you could pick the top score under a certain threshhold of edit distance. This isn't computationally expensive and is a small add.


Should probably go in here:

https://github.com/dthree/vorpal/blob/master/lib/vorpal.js#L1027

minhchu commented 8 years ago

Thanks for your advice. I'll try to do a PR.