예제 #1
0
        // 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);
        }
예제 #2
0
        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.UnusedArguments.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);
                }
            }
        }