Question

Any ideas on how to implement tab completion for a .NET (C#) Console Application? And I mean within an application that is run and then loops for user input (like if you run ftp.exe without any arguments), like this:

        string line = string.Empty;
        while (line != "exit")
        {
            //do something here
            Console.ReadLine();
        }

I know I probably couldn't actually use readline, but I would like to be able to do tab completion at that same point where you retrieve input from the user.

Was it helpful?

Solution

Take a look at this code from the Mono project http://tirania.org/blog/archive/2008/Aug-26.html I played with it some the other day. It does a lot of command line editingy, but I don't think it does line completion.

OTHER TIPS

Do a Console.ReadKey().

If you get a Tab, look at what you have in the command buffer, and loop through your available commands. If someCommand.Name.BeginsWith(currentinput), you have a winner, and you can write to screen a list of possible commands.

If there is only one(TM) you can substitute it with what the user had typed :)

I created a small lib to add this functionality to an app i made:

https://github.com/fjunqueira/hinter

It might not suit your needs, but you can feel free to edit it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top