tags:

views:

442

answers:

4

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.

+3  A: 

Console.ReadKey

Andrew Burns
+4  A: 

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.

kenny
A: 

@Kenny Line 17 of that file says completion support is not complete. However I still upped your answer as that is VERY handy and MIT licensed.

Andrew Burns
+1  A: 

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 :)

Lars Mæhlum