When I’m writing a console app and I need some flexibility in “parsing” arguments…I want it done ASAP and without much extra work. I tried a few command line option libraries and it always seem those libraries get in your way, so I wrote my own. Its based on the simple premise “convention over configuration”.
Basically it can be used as follows:
var cmdLine = new CmdLineParser();
cmdLine.Setup<int>("option1");
cmdLine.Setup<string>("option2");
cmdLine.Parse(args);
then I can use the options…
Console.WriteLine(cmdLine.Opts.option1);
Console.WriteLine(cmdLine.Opts.option2);
Setup can take 3 arguments
- option name
- required (optional)
- help message (optional)
Parse just takes a string array. Parse will throw an exception (CmdLineParserException), if it encounters the following cases:
- an required option that is missing
- an option with a invalid type (an option which takes a number but has a string)
- an option that is specified more then once
- –help or -h is encounter (then use the HelpMessage property to print the message) The following show exceptional cases…
var cmdLine = CmdLineParser();
cmdLine.Setup<int>("option",true,"help message");
cmdLine.Parse({});
cmdLine.Parse({"--option", "3", "--option","4"});
cmdLine.Parse({"--option", "abc"});
cmdLine.Parse({"--help"});
more information can be found at: https://bitbucket.org/sanjosep43/simplecmdline/wiki/Home
It can also be downloaded form NuGet (SimpleCmdLine).