ucswift / Consolas2

Consolas2 - a console application framework for the .Net Framework and DotNetCore

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consolas2

Consolas2 is a dotnetcore update to the Consolas console application framework developed by Rickard Nilsson. Consolas2 simplifies the creation of everything from simple throw away apps to bigger, more complex tools with lots of arguments.


Build status License

Deltas to the original Consolas

  • No RazorEngine view engine support
  • Nustache dependency replaced with Stubble
  • Assembly migrated to .NetStandard 2.0, unit tests are DotNetCore 3.0

Goals of this update were to use core Consolas functionality in .Net Framework and DotNetCore based applications. Utilizing .NetStandard 2.0 the assembly will work cross framework. A factor in migrating to .Net Standard you will need to manually create the "Args", "Commands" and "Views" folders in your console application.

Features

  • Convention over configuration
  • Small fingerprint
  • Testable
  • Mustache view engine

How to get it

Simply create a new Console Application and install the Nuget package Consolas2 or run the following command in the Package Manager Console

PM> Install-Package Consolas2

After installation you will need to create 3 Directories in the root of your Console application, "Args", "Commands" and "Views". Your structure will look like the one below.

Console App
   |-> Args
       |-> [MyParam]Args.cs
   |-> Commands
       |-> [MyParam]Command.cs
   |-> Views
       |-> [MyParam].template

At a minimum you need a file in the Args directory (i.e. HelpArgs.cs) and in the Commands directory (i.e. HelpCommand.cs) for all the commands you want your cli application to accept.

Simple example

class Program : ConsoleApp<Program>
{
    static void Main(string[] args)
    {
        Match(args);
    }
}

public class HelpArgs
{
    public bool Help { get; set; }
}

public class HelpCommand : Command
{
    public string Execute(HelpArgs args)
    {
        return "Using: Program.exe ...";
    }
}

Running the above program from a console

C> program.exe -Help
Using: Program.exe ...

Unit testing

Unit tests

Unit testing Consolas Commands is easy:

[TestFixture]
public class GrepCommandTests
{
    [Test]
    public void Execute_ValidArgument_ReturnsGrepedText()
    {
        var command = new GrepCommand();

        var result = command.Execute(new GrepArgs
        {
            FileName = "doc.txt",
            Regex = "foo"
        });

        StringAssert.Contains("foo bar baz", result);
    }
}

End to end tests

The following is a sample testing a console application from end to end:

[TestFixture]
public class EndToEndTests
{
    private StringBuilder _consoleOut;
    private TextWriter _outWriter;

    [SetUp]
    public void Setup()
    {
        _outWriter = Console.Out;
        _consoleOut = new StringBuilder();
        Console.SetOut(new StringWriter(_consoleOut));
    }

    [TearDown]
    public void TearDown()
    {
        Console.SetOut(_outWriter);
    }

    [Test]
    public void Version()
    {
        Program.Main(new []{ "-version"});
        StringAssert.Contains("2.4.2", _consoleOut.ToString());
    }
}

Advanced examples

Author's

License

BSD 2-Clause License

Acknowledgments

Consolas2 makes use of the following OSS projects:

About

Consolas2 - a console application framework for the .Net Framework and DotNetCore

License:BSD 2-Clause "Simplified" License


Languages

Language:C# 100.0%