/// <summary> /// Parses command-line arguments into a reference-type object. Use /// ArgumentAttributes to control parsing behavior. /// </summary> /// <typeparam name="T">Type of the parsed arguments object.</typeparam> /// <param name="arguments">The actual arguments.</param> /// <param name="destination">The resulting parsed arguments.</param> /// <param name="options">Optionally provides additional options /// controlling how parsing proceeds.</param> /// <returns>True if no errors were detected.</returns> public static bool Parse <T>(IEnumerable <string> arguments, T destination, CommandLineParserOptions options) where T : class { Debug.Assert(arguments != null); Debug.Assert(arguments.All(arg => arg != null)); Debug.Assert(destination != null, "destination cannot be null"); var engine = new CommandLineParserEngine(destination.GetType(), destination, options); return(engine.Parse(arguments, destination)); }
private static bool Parse <T>(IEnumerable <string> args, out T parsedArgs) where T : new() { parsedArgs = new T(); var parser = new CommandLineParserEngine(typeof(T)); if (parser.Parse(args.ToList(), parsedArgs)) { return(true); } parsedArgs = default(T); return(false); }
/// <summary> /// Parses command-line arguments into a reference-type object. /// Displays usage message if invalid arguments are encountered. /// </summary> /// <typeparam name="T">Type of the parsed arguments object.</typeparam> /// <param name="arguments">The actual arguments.</param> /// <param name="destination">The resulting parsed arguments.</param> /// <param name="options">Optionally provides additional options /// controlling how parsing proceeds.</param> /// <param name="usageInfoOptions">Options for how to display usage /// information, in case it's presented.</param> /// <returns>True if no errors were detected.</returns> public static bool ParseWithUsage <T>(IEnumerable <string> arguments, T destination, CommandLineParserOptions options, UsageInfoOptions usageInfoOptions) where T : class { if (arguments == null) { throw new ArgumentNullException(nameof(arguments)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if (options == null) { options = new CommandLineParserOptions { Reporter = DefaultReporter }; } Debug.Assert(arguments.All(a => a != null)); // Check if the object inherits from HelpArgumentsBase. var helpDestination = destination as HelpArgumentsBase; // Parse! var engine = new CommandLineParserEngine(destination.GetType(), destination, options); if (!engine.Parse(arguments, destination)) { // An error was encountered in arguments. Display the usage // message. options.Reporter?.Invoke(ColoredMultistring.FromString(Environment.NewLine)); options.Reporter?.Invoke(GetUsageInfo(engine, null, null, usageInfoOptions, destination)); return(false); } // We parsed the arguments, but check if we were requested to // display the usage help message anyway. if ((helpDestination != null) && helpDestination.Help) { options.Reporter?.Invoke(GetUsageInfo(engine, null, null, usageInfoOptions, destination)); return(false); } return(true); }
protected static void RunParserWith(string[] args) { error = Catch.Exception(() => results = sut.Parse(args).ToList()); }