Пример #1
0
        public object Parse(object commandlineinfo, string commandline)
        {
            // Retrieve tokens:
            Queue<string> tokens = new Queue<string>(Tokenize(commandline));

            // Dequeue first argument as it is the executable:
            tokens.Dequeue();

            // Retrieve CommandLineInfo Handler:
            CommandLineInfoHandler handler = new CommandLineInfoHandler(commandlineinfo);

            // Handle tokens:
            int argnumber = 0;
            while (tokens.Count > 0)
            {
                string token = tokens.Dequeue();

                if (token.StartsWith(optionMarker))
                {
                    // Handle option:

                    string option = token.Substring(optionMarker.Length);
                    if (handler.HelpOptions.Contains(option))
                    {
                        // If option is helpoption, mark helpRequested:
                        this.isHelpRequested = true;
                        this.SetOption(commandlineinfo, handler.HelpHandler, true);
                    }
                    else
                    {
                        try
                        {
                            // For other option, first retrieve option name:
                            string optionname;
                            if (this.valueSeparator == " ")
                            {
                                optionname = option;
                            }
                            else if (option.Contains(this.optionMarker))
                            {
                                optionname = option.Substring(0, option.IndexOf(this.optionMarker));
                            }
                            else
                            {
                                optionname = option;
                            }

                            // Retrieve option handler for optionname:
                            MemberInfo opthandler = handler.OptionHandlers[optionname];

                            if (this.IsBooleanOption(opthandler))
                            {
                                // If boolean option, set option to true, as option is set:
                                this.SetOption(commandlineinfo, opthandler, true);
                            }
                            else
                            {
                                // Otherwise retrieve options value:
                                string optionvalue;
                                if (this.valueSeparator == " ")
                                {
                                    if (tokens.Count > 0) optionvalue = tokens.Dequeue();
                                    else optionvalue = null;
                                }
                                else if (option.Contains(this.optionMarker))
                                {
                                    optionvalue = option.Substring(option.IndexOf(this.optionMarker) + this.optionMarker.Length);
                                }
                                else
                                {
                                    optionvalue = null;
                                }

                                // And set option value:
                                this.SetOption(commandlineinfo, opthandler, optionvalue, handler.OptionValues[optionname]);
                            }
                        }
                        catch (KeyNotFoundException)
                        {
                            throw new InvalidOption();
                        }
                    }
                }
                else
                {
                    // Handle argument:

                    if (handler.ArgumentHandlers.Count > argnumber)
                    {
                        this.SetArgument(commandlineinfo, handler.ArgumentHandlers[argnumber], token);
                    }
                    else if (handler.ArgumentListHandler != null)
                    {
                        this.AppendArgument(commandlineinfo, handler.ArgumentListHandler, token);
                    }
                    else
                    {
                        throw new TooManyArgumentsException();
                    }
                    argnumber++;
				}
            }

            // Validate commandline settings:
            if (!this.IsHelpRequested)
            {
                // TODO: check that all mandatory args and options have been set
            }
			if (argnumber < handler.NumberOfRequiredArgs)
				this.isMissingArguments = true;

            // Return the passed commandlineinfo object:
            return commandlineinfo;
        }
        public object Parse(object commandlineinfo, string commandline)
        {
            // Retrieve tokens:
            Queue <string> tokens = new Queue <string>(Tokenize(commandline));

            // Dequeue first argument as it is the executable:
            tokens.Dequeue();

            // Retrieve CommandLineInfo Handler:
            CommandLineInfoHandler handler = new CommandLineInfoHandler(commandlineinfo);

            // Handle tokens:
            int argnumber = 0;

            while (tokens.Count > 0)
            {
                string token = tokens.Dequeue();

                if (token.StartsWith(optionMarker))
                {
                    // Handle option:

                    string option = token.Substring(optionMarker.Length);
                    if (handler.HelpOptions.Contains(option))
                    {
                        // If option is helpoption, mark helpRequested:
                        this.isHelpRequested = true;
                        this.SetOption(commandlineinfo, handler.HelpHandler, true);
                    }
                    else
                    {
                        try
                        {
                            // For other option, first retrieve option name:
                            string optionname;
                            if (this.valueSeparator == " ")
                            {
                                optionname = option;
                            }
                            else if (option.Contains(this.optionMarker))
                            {
                                optionname = option.Substring(0, option.IndexOf(this.optionMarker));
                            }
                            else
                            {
                                optionname = option;
                            }

                            // Retrieve option handler for optionname:
                            MemberInfo opthandler = handler.OptionHandlers[optionname];

                            if (this.IsBooleanOption(opthandler))
                            {
                                // If boolean option, set option to true, as option is set:
                                this.SetOption(commandlineinfo, opthandler, true);
                            }
                            else
                            {
                                // Otherwise retrieve options value:
                                string optionvalue;
                                if (this.valueSeparator == " ")
                                {
                                    if (tokens.Count > 0)
                                    {
                                        optionvalue = tokens.Dequeue();
                                    }
                                    else
                                    {
                                        optionvalue = null;
                                    }
                                }
                                else if (option.Contains(this.optionMarker))
                                {
                                    optionvalue = option.Substring(option.IndexOf(this.optionMarker) + this.optionMarker.Length);
                                }
                                else
                                {
                                    optionvalue = null;
                                }

                                // And set option value:
                                this.SetOption(commandlineinfo, opthandler, optionvalue, handler.OptionValues[optionname]);
                            }
                        }
                        catch (KeyNotFoundException)
                        {
                            throw new InvalidOption();
                        }
                    }
                }
                else
                {
                    // Handle argument:

                    if (handler.ArgumentHandlers.Count > argnumber)
                    {
                        this.SetArgument(commandlineinfo, handler.ArgumentHandlers[argnumber], token);
                    }
                    else if (handler.ArgumentListHandler != null)
                    {
                        this.AppendArgument(commandlineinfo, handler.ArgumentListHandler, token);
                    }
                    else
                    {
                        throw new TooManyArgumentsException();
                    }
                    argnumber++;
                }
            }

            // Validate commandline settings:
            if (!this.IsHelpRequested)
            {
                // TODO: check that all mandatory args and options have been set
            }
            if (argnumber < handler.NumberOfRequiredArgs)
            {
                this.isMissingArguments = true;
            }

            // Return the passed commandlineinfo object:
            return(commandlineinfo);
        }