示例#1
0
        /// <summary>
        /// Parses a response file.
        /// </summary>
        /// <param name="responseFile">The file to parse.</param>
        /// <returns>The array of arguments.</returns>
        private static List <string> ParseResponseFile(string responseFile)
        {
            string arguments;

            using (StreamReader reader = new StreamReader(responseFile))
            {
                arguments = reader.ReadToEnd();
            }

            return(X_CommandLine.ParseArgumentsToArray(arguments));
        }
示例#2
0
        public static X_CommandLine Parse(string[] commandLineArguments, Func <X_CommandLine, string, bool> parseCommand, Func <X_CommandLine, string, bool> parseArgument)
        {
            var cmdline = new X_CommandLine();

            cmdline.FlattenArgumentsWithResponseFilesIntoOriginalArguments(commandLineArguments);

            cmdline.QueueArgumentsAndLoadExtensions(cmdline.OriginalArguments);

            cmdline.ProcessRemainingArguments(parseArgument, parseCommand);

            return(cmdline);
        }
示例#3
0
        private void FlattenArgumentsWithResponseFilesIntoOriginalArguments(string[] commandLineArguments)
        {
            List <string> args = new List <string>();

            foreach (var arg in commandLineArguments)
            {
                if ('@' == arg[0])
                {
                    var responseFileArguments = X_CommandLine.ParseResponseFile(arg.Substring(1));
                    args.AddRange(responseFileArguments);
                }
                else
                {
                    args.Add(arg);
                }
            }

            this.OriginalArguments = args.ToArray();
        }
示例#4
0
 public static X_CommandLine Parse(string[] commandLineArguments, Func <X_CommandLine, string, bool> parseArgument)
 {
     return(X_CommandLine.Parse(commandLineArguments, null, parseArgument));
 }
示例#5
0
        public static X_CommandLine Parse(string commandLineString, Func <X_CommandLine, string, bool> parseArgument)
        {
            var arguments = X_CommandLine.ParseArgumentsToArray(commandLineString).ToArray();

            return(X_CommandLine.Parse(arguments, null, parseArgument));
        }
示例#6
0
        /// <summary>
        /// Parses an argument string into an argument array based on whitespace and quoting.
        /// </summary>
        /// <param name="arguments">Argument string.</param>
        /// <returns>Argument array.</returns>
        private static List <string> ParseArgumentsToArray(string arguments)
        {
            // Scan and parse the arguments string, dividing up the arguments based on whitespace.
            // Unescaped quotes cause whitespace to be ignored, while the quotes themselves are removed.
            // Quotes may begin and end inside arguments; they don't necessarily just surround whole arguments.
            // Escaped quotes and escaped backslashes also need to be unescaped by this process.

            // Collects the final list of arguments to be returned.
            var argsList = new List <string>();

            // True if we are inside an unescaped quote, meaning whitespace should be ignored.
            var insideQuote = false;

            // Index of the start of the current argument substring; either the start of the argument
            // or the start of a quoted or unquoted sequence within it.
            var partStart = 0;

            // The current argument string being built; when completed it will be added to the list.
            var arg = new StringBuilder();

            for (int i = 0; i <= arguments.Length; i++)
            {
                if (i == arguments.Length || (Char.IsWhiteSpace(arguments[i]) && !insideQuote))
                {
                    // Reached a whitespace separator or the end of the string.

                    // Finish building the current argument.
                    arg.Append(arguments.Substring(partStart, i - partStart));

                    // Skip over the whitespace character.
                    partStart = i + 1;

                    // Add the argument to the list if it's not empty.
                    if (arg.Length > 0)
                    {
                        argsList.Add(X_CommandLine.ExpandEnvVars(arg.ToString()));
                        arg.Length = 0;
                    }
                }
                else if (i > partStart && arguments[i - 1] == '\\')
                {
                    // Check the character following an unprocessed backslash.
                    // Unescape quotes, and backslashes followed by a quote.
                    if (arguments[i] == '"' || (arguments[i] == '\\' && arguments.Length > i + 1 && arguments[i + 1] == '"'))
                    {
                        // Unescape the quote or backslash by skipping the preceeding backslash.
                        arg.Append(arguments.Substring(partStart, i - 1 - partStart));
                        arg.Append(arguments[i]);
                        partStart = i + 1;
                    }
                }
                else if (arguments[i] == '"')
                {
                    // Add the quoted or unquoted section to the argument string.
                    arg.Append(arguments.Substring(partStart, i - partStart));

                    // And skip over the quote character.
                    partStart = i + 1;

                    insideQuote = !insideQuote;
                }
            }

            return(argsList);
        }