/// <summary> /// Set the error callback to print also the helper /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p"></param> /// <param name="cliInfo"></param> /// <param name="output"></param> /// <returns></returns> public static Parser PrintingHelperOnErrorAndQuit <T>(this Parser p, ICLIInfo cliInfo, Action <string> output) where T : ICLIArguments { if (p == null) { throw new ArgumentNullException(nameof(p)); } if (cliInfo == null) { throw new ArgumentNullException(nameof(cliInfo)); } if (String.IsNullOrEmpty(cliInfo.Alias)) { throw new ArgumentNullException(nameof(cliInfo)); } if (output == null) { throw new ArgumentNullException(nameof(output)); } p.OnError(err => { output("**********************************| Unexpected Error during argument parsing |***************************************"); output(err.Message); output(err.StackTrace); output(""); output(" Please, run exe without params to see the helper"); UsagePrinterHelper.PrintOnConsoleFor <T>(cliInfo.Alias); Environment.Exit(-2); }); return(p); }
static void Main(string[] args) { try { // configure parser var info = new CLIInfo(); var parser = ParserHelper.DefaultParser() .UsingThisVersionInOutput(info, Console.WriteLine) .PrintingHelperOnErrorAndQuit <TestArguments>(info, Console.WriteLine); // try to parse arguments; if thorws errors, the program quits var options = parser.Parse <TestArguments>(args); Console.WriteLine(options != null ? "Options succesfully parsed" : "Options is currently null"); UsagePrinterHelper.PrintOnConsoleFor <TestArguments>(info.Alias); Environment.Exit(0); } catch (Exception ex) { Console.WriteLine("*****************************************************************************"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Environment.Exit(1); } }