// Parse arguments -- the main show public ParseArgumentsResult ParseArguments(string[] args) { // keep track of results ParseArgumentsResult results = new ParseArgumentsResult(); results.options = this; // parse arguments ParseArguments(args, results); return(results); }
private void ParseArguments(string[] args, ParseArgumentsResult results) { foreach (string arg in args) { if (arg.Length == 0) { continue; } if (arg[0] == '/') { // option processing // find the named option int index = 1; while (index < arg.Length) { if ((!Char.IsLetter(arg, index)) && (arg[index] != '?')) { break; } index++; } string key = arg.Substring(1, index - 1); string value = arg.Substring(index); // invoke the appropriate logic if (map.ContainsKey(key)) { Option option = (Option)map[key]; ParseResult result = option.ParseArgument(value); if (result != ParseResult.Success) { results.errors.Add(arg, result); } } else { results.errors.Add(arg, ParseResult.UnrecognizedOption); } } else if (arg[0] == '@') { string responseFile = arg.Substring(1); List <string> responses = new List <string>(); using (TextReader reader = File.OpenText(responseFile)) { while (true) { string response = reader.ReadLine(); if (response == null) { break; } responses.Add(response); } } ParseArguments(responses.ToArray(), results); } else { // non-option processing results.nonoptions.Add(arg); } } // make sure the required arguments were present foreach (Option option in map.Values) { option.processed = true; if ((option.IsRequired) && (!option.IsPresent)) { results.errors.Add(option.Name, ParseResult.MissingOption); } } }