-
Notifications
You must be signed in to change notification settings - Fork 280
Docs | Autocomplete
Vorpal supports powerful, robust tabbed autocompletion by default, along with custom autocompletion on both commands and options.
This feature is designed to exactly mirror the Linux-style tabbed autocompletion you are familiar with.
Let's suppose you are running a Vorpal app with three commands registered: eat
, feed
and water
. When you start the app and press tab
twice, you'll get those three commands suggested:
$ node ./myapp.js
app$ [tab] [tab]
eat feed water
Typing f
and then pressing tab
will autocomplete to feed
:
app$ f [tab]
app$ feed
Now suppose we want to suggest that one should feed the cat
, dog
or horse
. We would register the following command in our app:
vorpal.command('feed [animal]')
.autocomplete(['cat', 'dog', 'horse'])
.action(feedThem);
On pressing tab now, we would get those suggestions:
$ node ./myapp.js
app$ feed [tab] [tab]
cat dog horse
If the list of animals we feed is somewhat dynamic, we can run a function to determine it:
vorpal.command('feed [animal]')
.autocomplete({
data: function () {
return getAnimals;
}
})
.action(feedThem);
If we need it to be async, we can pass in a callback as the second parameter (the first is what the user has typed so far):
vorpal.command('feed [animal]')
.autocomplete({
data: function (input, callback) {
getAnimals(function (array) {
callback(array);
});
}
})
.action(feedThem);
We can also do a Promise:
vorpal.command('feed [animal]')
.autocomplete({
data: function () {
return getAnimalsPromise;
}
})
.action(feedThem);