.net - Best way to parse command line arguments in C#? -
when building console applications take parameters, can use arguments passed main(string[] args)
.
in past i've indexed/looped array , done few regular expressions extract values. however, when commands more complicated, parsing can pretty ugly.
so i'm interested in:
- libraries use
- patterns use
assume commands adhere common standards such answered here.
i suggest using ndesk.options (documentation) and/or mono.options (same api, different namespace). example documentation:
bool show_help = false; list<string> names = new list<string> (); int repeat = 1; var p = new optionset () { { "n|name=", "the {name} of greet.", v => names.add (v) }, { "r|repeat=", "the number of {times} repeat greeting.\n" + "this must integer.", (int v) => repeat = v }, { "v", "increase debug message verbosity", v => { if (v != null) ++verbosity; } }, { "h|help", "show message , exit", v => show_help = v != null }, }; list<string> extra; try { = p.parse (args); } catch (optionexception e) { console.write ("greet: "); console.writeline (e.message); console.writeline ("try `greet --help' more information."); return; }
Comments
Post a Comment