I build small console applications all the time just to try something new or make small tools, like for integration purposes. This post introduces a new, light weight, framework for building anything from small throw aways to larger, powerful console applications in .NET. In most project templates there’s some form of guidance and support. For ASP.NET we have MVC framework and for windows applications, we have WPF or Win RT. If you create a new console application you only get an empty main method and that’s it.
I’m calling it Consolas and I’ve designed it around a few core principles.
- Small fingerprint
- Convention over configuration
How to get it?
Simply create a new Console Application and install the Nuget package Consolas or run the following command in the Package Manager Console
1: PM> Install-Package Consolas
That will add the follow files to your project:
What you need to do yourself to get going is to edit the Program.cs file:
1: using Consolas.Core;
3: namespace ConsolasApplication
5: class Program : ConsoleApp
7: static void Main(string args)
Now Consolas will start to match program arguments to POCO classes in your project, e.g. HelpArgs added by the Nuget package. Thus, running the program from the console with the argument –help will execute the Execute method in the HelpCommand class.
1: C:\...> program.exe -Help
2: Using: program.exe ...
In the same manor, you can start building your console application by defining classes which represent the possible arguments for a command, and the corresponding command classes them selves. By convention any class that has properties matching program arguments, Consolas will try to match it to a command. The argument class is matched by looking for commands which takes the argument class as parameter in the command’s execute method. Here is the general convention:
Looks interesting? Be sure to check out the GitHub project site, the Consolas tag where more articles will be posted.