If you are like me and you are about to write just another command line parser for your little app (or you big app, it doesn’t matter), just stop right there and follow the following steps:
- Grab this file and include it in you project
- Read the documentation. In fact, scroll down and read just the sample code.
- Promise you will never write another command line arguments parser. Ever.
Seriously, Mono.Options from the Mono Project is efficient, elegant and a pleasure to use. And best of all is just one file that you can include in your project, so no need to reference external libraries.
And by the way, if you want to use it with C# 2.0 simply get rid of those lambdas:
// .... code omitted
bool show_help = false;
List<string> names = new List<string> ();
int repeat = 1;
Options p = new OptionSet();
p.Add("n|name=", "the {NAME} of someone to greet.", delegate(string v) { names.Add(v); });
p.Add("r|repeat=", "the number of {TIMES} to repeat the greeting.", delegate(int v) { repeat = v; });
p.Add("h|help", "show this message and exit", delegate(string v) { show_help = v!=null; });
// code omitted ...