/// <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); }
private static ColoredMultistring GetUsageInfo( CommandLineParserEngine engine, int?columns, string commandName, UsageInfoOptions options, object destination = null) { if (!columns.HasValue) { columns = GetCurrentConsoleWidth(); } return(engine.GetUsageInfo(columns.Value, commandName, options, destination)); }
/// <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); }
/// <summary> /// Generate possible completions for the specified set of command-line /// tokens. /// </summary> /// <param name="type">Type of the parsed arguments object.</param> /// <param name="tokens">The tokens.</param> /// <param name="indexOfTokenToComplete">Index of the token to complete. /// </param> /// <param name="options">Parsing options.</param> /// <param name="destObjectFactory">If non-null, provides a factory /// function that can be used to create an object suitable to being /// filled out by this parser instance.</param> /// <returns>The candidate completions for the specified token. /// </returns> public static IEnumerable <string> GetCompletions(Type type, IEnumerable <string> tokens, int indexOfTokenToComplete, CommandLineParserOptions options, Func <object> destObjectFactory) { if (type == null) { throw new ArgumentNullException(nameof(tokens)); } if (tokens == null) { throw new ArgumentNullException(nameof(tokens)); } var engine = new CommandLineParserEngine( type, null /* default values */, options); return(engine.GetCompletions(tokens, indexOfTokenToComplete, destObjectFactory)); }
/// <summary> /// Tokenizes the provided input text line, observing quotes. /// </summary> /// <param name="line">Input line to parse.</param> /// <param name="options">Options for tokenizing.</param> /// <returns>Enumeration of tokens.</returns> internal static IEnumerable <Token> Tokenize(string line, CommandLineTokenizerOptions options = CommandLineTokenizerOptions.None) => CommandLineParserEngine.Tokenize(line, options);