/// <summary> /// <para> /// Parse the given argument list. /// </para> /// <para> /// If parsing fails, error details is immediately written /// to the error console and help information is displayed. /// </para> /// <para> /// WARNING: This method may call <see cref="Environment.Exit"/> on /// error. /// </para> /// </summary> /// <param name="obj">Parsed arguments are stored here.</param> /// <param name="args">Argument list to parse.</param> public static void StrictParse <TS>(string[] args, TS obj) where TS : class { CliParser <TS> parser = null; try { parser = new CliParser <TS>(obj); } catch (ArgumentIntegrityException e) { Console.Error.WriteLine(e.Message); if (parser != null && parser.Config != null) { Console.Error.WriteLine( new AutomaticHelpGenerator <TS>().GetUsage(parser.Config)); } Environment.Exit(2); } catch (Utils.AggregateException ex) { if (parser != null && parser.Config != null) { Console.Error.WriteLine( new AutomaticHelpGenerator <TS>().GetUsage(parser.Config)); } ex.Handle(e => { Console.Error.WriteLine(e.Message); return(true); }); Environment.Exit(2); } parser.StrictParse(args); }
/// <summary> /// Parse the given argument list. /// </summary> /// <exception cref="ParseException"> /// An error happened while parsing. /// </exception> /// <exception cref="ParserExit"> /// Either the help or version information were triggered so /// parsing was aborted. /// </exception> /// <param name="args">Argument list to parse.</param> /// <param name="obj">Parsed arguments will be store here.</param> public static void Parse <TS>(string[] args, TS obj) where TS : class { var parser = new CliParser <TS>(obj); parser.Parse(args); }
/// <summary> /// Make an attempt to parse the arguments and return true if /// parsing was successful. If it returns false, the destination /// object may be left in an incomplete state. /// </summary> /// <typeparam name="TS"></typeparam> /// <param name="obj">Destination object.</param> /// <param name="args">Argument list to parse.</param> /// <returns>True if parsing succeeded.</returns> public static bool TryParse <TS>(string[] args, TS obj) where TS : class { var parser = new CliParser <TS>(obj); return(parser.TryParse(args)); }