Parsing command-line arguments can be deceptively tricky. Often it starts as something very simple to do manually, but as more arguments and formats and flexibility are desired, the effort required to do it well can explode.
So there are several nuget packages for libraries to help with this. I chose NDesk.Options and I liked it. Here's what your command-line argument code might look if you used this library.
public class MyProgramSettings { public string InputFile { get; set; } public string ErrorFile { get; set; } public string OutputFile { get; set; } public bool ShowStackTrace { get; set; } } public static Main(string[] args) { var settings = new MyProgramSettings(); var showHelp = false; var parser = new NDesk.Options.OptionSet() { { "h|?|help", "Show available options", v => showHelp = true }, { "i=|input", "The name of the input file.", v => settings.InputFile = v }, { "e=|error", "The name file to log errors.", v => settings.ErrorFile = v }, { "o=|output", "The name of the output file.", v => settings.OutputFile = v }, { "s|stack ", "Show stack traces.", v => settings.ShowStackTrace = true } }; parser.Parse(args); if(showHelp) { parser.WriteOptionDescriptions(Console.Out); } }
Comments !