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.
- 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.
- Convention over configuration
- Small fingerprint
- Testable
- Mustache view engine
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.
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 ...";
}
}
C> program.exe -Help Using: Program.exe ...
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);
}
}
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());
}
}
- Original Consolas author Rickard Nilsson
- Shawn Jackson (Twitter: @DesignLimbo Blog: http://designlimbo.com)
Consolas2 makes use of the following OSS projects:
- SimpleInjector released under the MIT license: https://simpleinjector.codeplex.com/license
- Stubble released under the MIT license: https://github.com/StubbleOrg/Stubble/blob/master/licence.md
- NUnit released under the NUnit license: http://nunit.org/nuget/license.html
- Fluent Assertions released under the Apache 2.0 license: https://github.com/fluentassertions/fluentassertions/blob/master/LICENSE
- NSubstitute released under the BSD license: https://raw.githubusercontent.com/nsubstitute/NSubstitute/master/LICENSE.txt