With Argument Reader command line arguments can be passed into a .net core application. The standard project assist 4 types of arguments:
- Boolean
- Strings (*)
- Integers (#)
- Doubles (##)
Own argument types can be build with own classes. They need to inherit from the ArgumentMarshalerLib. Libraries are loading dynamically on startup. It is not necessary to recompile the complete solution.
Arguments parameter = new Arguments("Path to Marshaler Libraries", "Schema", "Argument Array");- BooleanMarshalerLib.dll
- StringMarshalerLib.dll
- IntegerMarshalerLib.dll
- DoubleMarshalerLib.dll
- Parameter name
- Marshaler type
Example
Arguments parameter = new Arguments("...", "enabled,text*,number#,comma##", "...");
bool enabled = parameter.GetValue<bool>("enabled");
string text = parameter.GetValue<string>("text");
int number = parameter.GetValue<int>("number");
double comma = parameter.GetValue<double>("comma");Arguments.exe -astatic void Main(string[] args)
{
Arguments parameter = new Arguments(@".\Marshaler", "a,b*", args);
bool a = parameter.GetValue<bool>("a"); // True
bool b = parameter.GetValue<bool>("b"); // False
// ...
}Arguments.exe -a "This is a Text"static void Main(string[] args)
{
Arguments parameter = new Arguments(@".\Marshaler", "a*", args);
string a = parameter.GetValue<string>("a"); // This is a Text
// ...
}Arguments.exe -a 1234static void Main(string[] args)
{
Arguments parameter = new Arguments(@".\Marshaler", "a#", args);
int a = parameter.GetValue<int>("a"); // 1234
// ...
}Arguments.exe -a 1234,4321static void Main(string[] args)
{
Arguments parameter = new Arguments(@".\Marshaler", "a##", args);
double a = parameter.GetValue<double>("a"); // 1234,4321
// ...
}- Create a new VisualStudio .NET Standard Classlibrary (??MarshalerLib)
- Link a new project reference to ArgumentMarshalerLib.dll (in this repository)
- Write Marshaler (See example code below)
- Copy the TestMarshalerLib.dll to the Marshaler directory in your project
- Implement the ? in your schema (e.g. "mymarshaler?")
using System;
using ArgumentMarshalerLib;
namespace TestMarshalerLib
{
public class TestMarshalerLib : ArgumentMarshaler
{
// Only schemas allowed that are not used (string.Empty, *, #, ## are already used from standard marshalers)
public override string Schema => "?";
public override void Set(Iterator<string> currentArgument)
{
try
{
// If implementation should use an argument behind the command (e.g. -a "??"),
// it is necessary to move the Iterator to the next position.
Value = currentArgument.Next();
}
catch (ArgumentOutOfRangeException)
{
throw new TestMarshalerException(ErrorCode.MISSING);
}
// If no argument behind the command is used just add your value
Value = "This is my personal marshaler";
}
public class TestMarshalerException : ArgumentsException
{
public TestMarshalerException() { }
public TestMarshalerException(ErrorCode errorCode) : base(errorCode) { }
public override string ErrorMessage()
{
switch (ErrorCode)
{
case ErrorCode.MISSING:
return $"Could not find test parameter for -{ErrorArgumentId}";
default:
return string.Empty;
}
}
}
}
}The original Argument Marshaler was written in Java and published by Robert C. Martin in his book Clean Code. This project adapt his implementations and extends it dynamically.