More examples and documentation is on Cliargs.NET Knowledge base
Cliargs.NET is a dotnet library helps you to parse and use the Command Line Interface arguments in easy way.
The main goal of Cliargs.NET is to help C# developers reduce their programming time without dealing with all validations and casting of the user input.
Cliargs.NET makes all for you, all you have to do is write your Setup configuration in order to configure the Arguments container, then, from key and values parsing, to validation is automatically done on app startup.
Install-Package Cliargs.NET
dotnet add package Cliargs.NET
In this example, you see the difference between managing the command line arguments by yourself, or by Cliargs.NET, for an application with two arguments:
Argument | type | key | short key | Optional |
---|---|---|---|---|
User Name | string | --name | -n | no |
User age | uint | --age | -a | yes |
The objective is to display the following message in a console app:
Dear {user name}, you're {user age} years old!
👉 Example on gist 👈
Create your Setup class and implement the Configure
method to create your app arguments:
public class CliArgsSetup : ICliArgsSetup
{
public void Configure(ICliArgsContainer container)
{
var nameArg = CliArg.New<string>("name")
.AsRequired()
.WithShortName("n");
var ageArg = CliArg.New<uint>("age")
.AsOptional()
.WithShortName("a");
container.Register(nameArg);
container.Register(ageArg);
}
}
Initialize the AppCliArgs instance by calling Initialize
method at the begining of your app main method:
AppCliArgs.Initialize<CliArgsSetup>(new CustomFormat());
The finally start using the arguments values
var name = AppCliArgs.GetArgValue<string>("name");
if (AppCliArgs.IsSet("age"))
{
var age = AppCliArgs.GetArgValue<uint>("age");
Console.WriteLine($"Dear {name}, you're {age} years old.");
}
else
{
Console.WriteLine($"Dear {name}, we don't know your age!");
}