Introducing Consolas - a console application framework for .NET

consolas-iconI 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
  • Testable
  • Fast

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:

Files added by Consolas

What you need to do yourself to get going is to edit the Program.cs file:

   1: using Consolas.Core;
   2:  
   3: namespace ConsolasApplication
   4: {
   5:     class Program : ConsoleApp
   6:     {
   7:         static void Main(string[] args)
   8:         {
   9:             Match(args);
  10:         }
  11:     }
  12: }

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:

matching args to command by convention

Looks interesting? Be sure to check out the GitHub project site, the Consolas tag where more articles will be posted.

Loading
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.