示例#1
0
        /// <summary>
        /// Parse command line
        /// </summary>
        /// <param name="commandLine">Command line</param>
        /// <returns></returns>
        public static IEnumerable <CommandToken> Parse(string commandLine)
        {
            CommandToken lastToken = null;

            for (int index = 0, count = commandLine.Length; index < count;)
            {
                switch (commandLine[index])
                {
                case ' ':
                {
                    lastToken = CommandSpaceToken.Parse(commandLine, ref index);
                    yield return(lastToken);

                    break;
                }

                case '"':
                case '\'':
                {
                    if (lastToken is CommandQuoteToken quote)
                    {
                        // "'"

                        if (quote.Value[0] != commandLine[index])
                        {
                            goto default;
                        }
                    }

                    lastToken = CommandQuoteToken.Parse(commandLine, ref index);
                    yield return(lastToken);

                    break;
                }

                default:
                {
                    lastToken = CommandStringToken.Parse(commandLine, ref index,
                                                         lastToken is CommandQuoteToken quote ? quote : null);

                    yield return(lastToken);

                    break;
                }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Parse command line spaces
        /// </summary>
        /// <param name="commandLine">Command line</param>
        /// <param name="index">Index</param>
        /// <param name="quote">Quote (could be null)</param>
        /// <returns>CommandSpaceToken</returns>
        internal static CommandStringToken Parse(string commandLine, ref int index, CommandQuoteToken quote)
        {
            int end;
            int offset = index;

            if (quote != null)
            {
                var ix = index;

                do
                {
                    end = commandLine.IndexOf(quote.Value[0], ix + 1);

                    if (end == -1)
                    {
                        throw new ArgumentException("String not closed");
                    }

                    if (IsScaped(commandLine, end - 1))
                    {
                        ix  = end;
                        end = -1;
                    }
                }while (end < 0);
            }
            else
            {
                end = commandLine.IndexOf(' ', index + 1);
            }

            if (end == -1)
            {
                end = commandLine.Length;
            }

            var ret = new CommandStringToken(offset, commandLine.Substring(index, end - index));

            index += end - index;
            return(ret);
        }