To the extent possible under law, Codeaddicts has waived all copyright and related or neighboring rights to Codeaddicts.libArgument.
libArgument is a .NET library for handling command-line arguments like a boss.
It allows you to validate arguments on-the-fly and saves you hours of work.
If you want to test the latest features, please download the source and compile it yourself.
The release may not be up-to-date.
You can always get the latest stable release from nuget.
Features:
- The generic parsing method works with any class
- Supports primitive type conversions
- Supports enum type conversions
- Supports one-dimensional array type conversions
- Supports Windows-style arguments:
/arg value - Supports POSIX style switches and arguments:
-a -b value - Supports POSIX style merged switches:
-abcequals-a -b -c - Supports GNU long-style arguments:
--arg=value - Supports arbitrarily many argument names for any variable
- Styles can be mixed:
-a --arg1 value /arg2 value --arg3=value - Automatically casts the argument to the correct type
- Automatically infers the argument name from the variable name if no argument name is given
It's really easy.
Here's a small example:
usingCodeaddicts.libArgument;usingCodeaddicts.libArgument.Attributes;// A simple class with some command-line optionspublicclassMyOptions{[Argument]publicstringinput;[Argument]publicstringoutput;[Switch]publicboolverbose;}publicclassProgram{publicstaticvoidMain(string[]args){// Let the magic happenvaroptions=ArgumentParser.Parse<MyOptions>(args);// That's it! Now you can use the variablesConsole.WriteLine("Input: {0}",options.input);Console.WriteLine("Output: {0}",options.output);Console.WriteLine("Verbose: {0}",options.verbose?"yes":"no");}}Call the program like that:
$ MyApp.exe --input "path/to/input" --output "path/to/output" --verbose
Sure! You can do pretty much everything :P
Here's some more advanced code for you.
usingCodeaddicts.libArgument;usingCodeaddicts.libArgument.Attributes;// A simple class with some command-line optionspublicclassMyOptions{// --str "Hello, World!"[Argument]publicstringstr;// --input path/to/file[Argument("--input")]publicstringInput;// -o path/to/file// --output path/to/file[Argument("-o","--output")]publicstringOutput;// --test "Test"// --woop "Test"[Argument("--test")][Argument("--woop")]publicstringTest;// --num 123[Argument("--num")]publicUInt64ANumber;// --log[Switch]publicboollog;// --enable-something[Switch("--enable-something")]publicboolSomething;// -a// --annoyme[Switch("-a","--annoyme")]publicboolAnotherSwitch;}publicclassProgram{publicstaticvoidMain(string[]args){// This is where the magic happensvaroptions=ArgumentParser.Parse<MyOptions>(args);// Now you can access the fields!Console.WriteLine("Input file: "+options.Input);Console.WriteLine("Output file: "+options.Output);Console.WriteLine("Your number: "+options.ANumber);
...}}Now call the application like that:
$ MyApp.exe -i test -o test --log --num 123 or
$ MyApp.exe --input test --output test --log --num 123
Absolutely no problem!
Just add a parameterless constructor to your Options class and initialize
your optional variables there. It's a piece of cake!