/// <summary> /// This is used to parse the command line options and return the results /// </summary> /// <param name="args">The array of option strings to parse</param> /// <param name="results">The results of the parsing operation</param> private void ParseArguments(string[] args, ParseArgumentsResult results) { foreach (string arg in args) { if (arg.Length == 0) { continue; } // Is it an option? if (arg[0] == '/' || arg[0] == '-') { // Find the named option int index = 1; while (index < arg.Length && (Char.IsLetter(arg, index) || arg[index] == '?')) { index++; } string key = arg.Substring(1, index - 1); string value = arg.Substring(index); // Invoke the appropriate logic if (map.ContainsKey(key)) { BaseOption option = map[key]; ParseResult result = option.ParseArgument(value); if (result != ParseResult.Success) { results.AddError(arg, result); } } else { results.AddError(arg, ParseResult.UnrecognizedOption); } } else if (arg[0] == '@') // Is it a response file? { this.ParseArguments(File.ReadAllLines(arg.Substring(1)), results); } else { results.AddNonOption(arg); // Non-option } } }
//===================================================================== /// <summary> /// Parse an array of command line option strings into command line option instances /// </summary> /// <param name="args">The array of options to parse</param> /// <returns>The results of parsing the command line option strings</returns> public ParseArgumentsResult ParseArguments(string[] args) { ParseArgumentsResult results = new ParseArgumentsResult(this); this.ParseArguments(args, results); // Make sure the required options were present foreach (BaseOption option in this.Where(o => !o.IsPresent && !String.IsNullOrEmpty(o.RequiredMessage))) { results.AddError(option.Name, ParseResult.MissingOption); } return(results); }