コード例 #1
0
        private int ParseParameter(string cmdLine, int index)
        {
            int startIndex = index;

            while (cmdLine[index] == '-' && index < cmdLine.Length)
            {
                index++;
            }

            if (cmdLine.Length == index || cmdLine[index] == ' ')
            {
                throw new CommandLineParserException("Parameter without name.");
            }
            if (!char.IsLetter(cmdLine[index]))
            {
                throw new CommandLineParserException("Parameter name must start with letter.");
            }

            int nameStartIndex = index;

            while (index < cmdLine.Length && (char.IsLetterOrDigit(cmdLine[index]) || cmdLine[index] == '-'))
            {
                index++;
            }

            Parameter newParam = CreateNewParameter(cmdLine.Substring(nameStartIndex, index - nameStartIndex));

            if (cmdLine.Length <= index || cmdLine[index] == ' ')
            {
                return(index - startIndex);
            }

            if (cmdLine[index] != ':' && cmdLine[index] != '=')
            {
                throw new CommandLineParserException("Parameter name contains invalid character '" + cmdLine[index] + "'.");
            }

            index++;
            if (cmdLine[index] == ' ')
            {
                newParam.AddValue(string.Empty);
                m_Params[newParam.Name] = newParam;
                return((index - startIndex) + 1);
            }

            int  valStartIndex = index;
            char quote         = cmdLine[index];

            if ('\'' == quote || '"' == quote)
            {
                valStartIndex = ++index;
            }

            while (index < cmdLine.Length)
            {
                if ('\'' == quote || '"' == quote)
                {
                    if (cmdLine[index] == quote)
                    {
                        break;
                    }
                }
                else if (' ' == cmdLine[index])
                {
                    break;
                }

                index++;
            }
            if (cmdLine.Length == index && ('\'' == quote || '"' == quote))
            {
                throw new CommandLineParserException("Missing quote.");
            }

            newParam.AddValue(cmdLine.Substring(valStartIndex, index - valStartIndex));
            m_Params[newParam.Name] = newParam;
            return(index - startIndex);
        }