Posts
8
Comments
25
Trackbacks
0
A command line interface base class

I needed a simpler way build basic command line interfaces so I cobbled together a useful C# base class.

I made the class as simple as possible to create entry point (start up) classes.

class Program :BaseConsole {

public override string Name {
get { return "Console Application"; }
}

protected override void LoadCommands() {
this.Commands.Add("print", () => Console.WriteLine("print out"));
this.Commands.Add("another", () => Console.WriteLine("another print out"));
}

static void Main(string[] args) {
new Program().Run();
}
}
 
 
Just run a console application that starts the Main() of the Program class. Typing in any commands that have been added to Commands will invoke the associated action.
 
Console Application
=====================================
Type 'help' to get available commands
Type 'exit','done' or 'quit' to exit

>>print
print out

>>another
another print out

>>
 
If you type "help" it will print a list of available commands. You can exit the program by typing "exit","quit" or "done".
 
The commands themselves are stored in a Dictionary<string,Action> - the new lambdas in C# 3 make writing inline functions so simple.
 

Download it here.

kick it on DotNetKicks.com

posted on Thursday, January 03, 2008 6:51 AM Print
Comments
Gravatar
# re: A command line interface base class
Judah
1/8/2008 10:44 AM
Have you seen the one from CodePlex? http://www.codeplex.com/ConsoleFx
Gravatar
# re: A command line interface base class
Vijay Santhanam
1/8/2008 10:56 AM
Hi Judah,

I hadn't seen ConsoleFX before, but it looks great! For 90% of my console apps, it's probably a bit heavy handed.

I planned to add argument parsing into this base class, but i might just use ConsoleFX instead when I need argument parsing.
Gravatar
# re: A command line interface base class
Yoav Sion
4/18/2008 4:15 PM
Can't kick this one. Is that on purpose?
Gravatar
# re: A command line interface base class
Vijay Santhanam
5/4/2008 6:59 PM
Wasn't intentional, I've embedded the Kick It tag for kicking convenience.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 7 and 1 and type the answer here: