dthree / vorpal

Node's framework for interactive CLIs

Home Page:http://vorpal.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom autocomplete for multiple required arguments

giodamelio opened this issue · comments

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?

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!

@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!

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

Thanks for the tip.