Skip to content

YounesCheikh/Cliargs.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cliargs.NET

Command Line Interface Arguments parser for dotnet

NuGet Badge License
.NET Test Coverage
image Build history

Knowledge base

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 Manager

Install-Package Cliargs.NET

Dotnet CLI

dotnet add package Cliargs.NET

Quick comparison

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 of old school way: 😔

👉 Example on gist 👈

New way with Cliargs.NET 🤩

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!");
}