コード例 #1
0
        public ParsedCommandLine ParseFile
        (
            [NotNull] string fileName,
            [NotNull] Encoding encoding
        )
        {
            Code.NotNullNorEmpty(fileName, "fileName");
            Code.NotNull(encoding, "encoding");

            string text = File.ReadAllText(fileName, encoding);

            string[]          arguments = CommandLineUtility.SplitText(text);
            ParsedCommandLine result    = Parse(arguments);

            return(result);
        }
コード例 #2
0
        public ParsedCommandLine Merge
        (
            [NotNull] ParsedCommandLine other
        )
        {
            Code.NotNull(other, "other");

            PositionalArguments.AddRange(other.PositionalArguments);

            foreach (CommandLineSwitch otherSwitch in other.Switches)
            {
                AddSwitch(otherSwitch);
            }

            return(this);
        }
コード例 #3
0
        public ParsedCommandLine Parse
        (
            [NotNull][ItemNotNull] string[] arguments
        )
        {
            Code.NotNull(arguments, "arguments");

            ParsedCommandLine result = new ParsedCommandLine();

            foreach (string argument in arguments)
            {
                if (string.IsNullOrEmpty(argument))
                {
                    Log.Error
                    (
                        "CommandLineParser::Parse: "
                        + "empty argument"
                    );

                    throw new ArgumentException();
                }

                char firstChar = argument[0];
                if (firstChar == CommandLineSettings.ArgumentDelimiter)
                {
                    if (argument.Length == 1)
                    {
                        Log.Error
                        (
                            "CommandLineParser::Parse: "
                            + "premature end of argument"
                        );

                        throw new ArgumentException();
                    }
                    if (argument[argument.Length - 1] != '"')
                    {
                        Log.Error
                        (
                            "CommandLineParser::Parse: "
                            + "unclosed colon"
                        );

                        throw new ArgumentException();
                    }

                    string trimmed = argument.Substring(1, argument.Length - 2);
                    if (trimmed.Length == 0)
                    {
                        result.PositionalArguments.Add(string.Empty);
                        continue;
                    }

                    char secondChar = trimmed[0];
                    if (secondChar == CommandLineSettings.ResponsePrefix)
                    {
#if !WINMOBILE && !PocketPC
                        string            fileName = trimmed.Substring(1);
                        ParsedCommandLine inner    = ParseFile(fileName);
                        result.Merge(inner);
#endif
                    }
                    else if (secondChar == CommandLineSettings.SwitchPrefix)
                    {
                        CommandLineSwitch item = _ParseSwitch(trimmed);
                        result.AddSwitch(item);
                    }
                    else
                    {
                        result.PositionalArguments.Add(argument);
                    }
                }
                else if (firstChar == CommandLineSettings.ResponsePrefix)
                {
#if !WINMOBILE && !PocketPC
                    string            fileName = argument.Substring(1);
                    ParsedCommandLine inner    = ParseFile(fileName);
                    result.Merge(inner);
#endif
                }
                else if (firstChar == CommandLineSettings.SwitchPrefix)
                {
                    CommandLineSwitch item = _ParseSwitch(argument);
                    result.AddSwitch(item);
                }
                else
                {
                    result.PositionalArguments.Add(argument);
                }
            }

            return(result);
        }