dthree / vorpal

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

Custom autocomplete for multiple required arguments #314

Open giodamelio opened 6 years ago

giodamelio commented 6 years ago

I have a command that has multiple required arguments, and I want to make custom autocomplete that is different between the arguments.

As far as I can tell there is no way to do this right now. Am I wrong?

lminiero commented 5 years ago

Found this issue because I was wondering the same thing, so from the lack of feedback I guess that this is indeed not possible... A partial hack could be using an argument and one or more options (depending on how many arguments you need), but in my case that doesn't work either, because the autocomplete for one of the arguments depends on the value set in the other, and apparently there is no way to get the current input so that I can make an informed decision. Pity, as I really like vorpal so far!

lminiero commented 5 years ago

@giodamelio not sure if you still need this, but I got it to work eventually, with the help of events. All you need to do is intercept the keypress event, and use that to update some global variable where you can keep track of what the user typed in so far:

    cli.on('keypress', function(event) {
        currentInput = event.value;
    });

Then in your autocomplete async method just process that variable to do different autocompletes depending on the position of the argument in the text. I used a regular expression to make sure I only had one space in between arguments, and a split to figure out how many arguments were already typed down (since as anticipated I needed one to depend on the previous). Hope this helps!

giodamelio commented 5 years ago

Ah, that is a clever way to do this. A bit of a hack, but it works 😀 .

Thanks for the tip.