Inheritance: ICommandLine, IEnumerable
Ejemplo n.º 1
0
        public ICommandLine Parse(Options options, string[] arguments, IEnumerable <KeyValuePair <string, string> > properties, bool stopAtNonOption)
        {
            if (options == null)
            {
                return(new CommandLine(false));
            }

            if (values != null)
            {
                // clear out the data in options in case it's been used before
                foreach (OptionValue option in values.Values)
                {
                    option.ClearValues();
                }
            }

            cmd = new CommandLine(true);

            bool eatTheRest = false;

            if (arguments == null)
            {
                arguments = new string[0];
            }

            string[] tokenList = Flatten(options, arguments, stopAtNonOption);

            int tokenCount = tokenList.Length;

            for (int i = 0; i < tokenCount; i++)
            {
                string t = tokenList[i];

                // the value is the double-dash
                if ("--".Equals(t))
                {
                    eatTheRest = true;
                }

                // the value is a single dash
                else if ("-".Equals(t))
                {
                    if (stopAtNonOption)
                    {
                        eatTheRest = true;
                    }
                    else
                    {
                        cmd.AddArgument(t);
                    }
                }

                // the value is an option
                else if (t.StartsWith("-"))
                {
                    if (stopAtNonOption && !options.HasOption(t))
                    {
                        eatTheRest = true;
                        cmd.AddArgument(t);
                    }
                    else
                    {
                        ProcessOption(options, t, tokenList, ref i);
                    }
                }

                // the value is an argument
                else
                {
                    cmd.AddArgument(t);

                    if (stopAtNonOption)
                    {
                        eatTheRest = true;
                    }
                }

                // eat the remaining tokens
                if (eatTheRest)
                {
                    while (++i < tokenCount)
                    {
                        String str = tokenList[i];

                        // ensure only one double-dash is added
                        if (!"--".Equals(str))
                        {
                            cmd.AddArgument(str);
                        }
                    }
                }
            }

            ProcessProperties(options, properties);
            CheckRequiredOptions(options);

            return(cmd);
        }