The lightest .NET utility for parsing command line arguments.
To install Easy Args, run the following command in the Package Manager Console
Install-Package EasyArgs
Let's use the following command as an example. Application
below would be the exe that you are executing.
Application Email=git@er.dun
In our application, we would use EasyArgs like the following :
static void Main(string[] args)
{
var easyArgs = new Args(args);
var email = easyArgs["Email"];
}
The number of arguments, and the order of the arguments does not matter. Also, accessing named arguments and flags is not case sensitive. The following would also work for the example above.
var email = easyArgs["email"];
Another example using object Initialization :
Application Username=Iateyourcookie
Within our application :
static void Main(string[] args)
{
var easyArgs = new Args
{
Arguments = args
};
var email = easyArgs["Username"];
}
EasyArgs also supports flags, take the following command for example :
Application ChickensName=MotherClucker -d
The -d
flag can be placed anywhere in the command and must be prepended with -
How to detect the presence of a flag in the code :
static void Main(string[] args)
{
var easyArgs = new Args
{
Arguments = args
};
var hasFlag = easyArgs.HasFlag("d");
}
Types currently supported :
- byte/sbyte
- int/uint
- short/ushort
- long/ulong
- float
- double
- char
- bool
- string
- decimal
- DateTime
- Guid
Another example :
Application -d KidneyCount=3
How to parse an integer :
static void Main(string[] args)
{
var easyArgs = new Args
{
Arguments = args
};
int kidneyCount = easyArgs["KidneyCount"];
}
EasyArgs uses implicit casting to parse the value to the requested type. If the string cannot be parsed to the requested type, then an exception will be thrown.
Main(string[] args)
is not the only place where Easy Args can be used. We can also do the following :
var command = "Hello=World -d KidneyCount=3 Username=Iateyourcookie Email=git@er.dun";
var args = new Args(command);
string username = args["Username"];