コード例 #1
0
ファイル: CLICommands.cs プロジェクト: onixion/MAD
        public override string Execute(int consoleWidth)
        {
            if (!OParUsed("id"))
            {
                output += "<color><yellow>Type 'help -id <COMMAND-ID>' to get more information about a command.\n";
                output += "<color><yellow>Available Commands:\n\n<color><white>";

                int      i2       = 0;
                string[] _buffer2 = new string[3] {
                    "", "", ""
                };
                for (int i = 0; i < _commands.Count; i++)
                {
                    _buffer2[i2] = "[" + i + "] " + _commands[i].command;

                    if (i2 == 2)
                    {
                        i2       = -1;
                        output  += ConsoleTable.FormatStringArray(consoleWidth, _buffer2);
                        _buffer2 = new string[3] {
                            "", "", ""
                        };
                    }

                    if (i == _commands.Count - 1)
                    {
                        output += ConsoleTable.FormatStringArray(consoleWidth, _buffer2);
                    }

                    i2++;
                }

                output += "\n";
            }
            else
            {
                int commandIndex = (int)pars.GetPar("id").argValues[0];

                try
                {
                    CommandOptions commandOptions = _commands[commandIndex];
                    Type           commandType    = commandOptions.commandType;
                    Command        tempCommand;

                    System.Reflection.ConstructorInfo cInfo = commandType.GetConstructor(new Type[1] {
                        typeof(object[])
                    });

                    if (cInfo == null)
                    {
                        cInfo       = commandType.GetConstructor(new Type[0]);
                        tempCommand = (Command)cInfo.Invoke(null);
                    }
                    else
                    {
                        tempCommand = (Command)cInfo.Invoke(new object[] { commandOptions.commandObjects });
                    }

                    output += "<color><yellow>COMMAND     <color><white>" + commandOptions.command + "<color><darkyellow> [" + commandIndex + "]\n";
                    output += "<color><yellow>DESCRIPTION <color><white>" + tempCommand.description + "\n";
                    output += "<color><yellow>USAGE       <color><white>" + GenUsageText(tempCommand, commandOptions) + "\n";
                    output += "<color><yellow>PARAMETER(S)\n";

                    if (!(tempCommand.rPar.Count == 0 && tempCommand.oPar.Count == 0))
                    {
                        if (tempCommand.rPar.Count != 0)
                        {
                            output += "\t<color><yellow>REQUIRED PARAMETER(S)\n\n";

                            foreach (ParOption _temp in tempCommand.rPar)
                            {
                                output += "\t<color><darkyellow>-" + _temp.par + " <color><white>";

                                if (_temp.multiargs)
                                {
                                    output += "<arg_1> <arg_2> ...";
                                }
                                else
                                if (!_temp.argEmpty)
                                {
                                    output += "<arg>";
                                }

                                output += "\n";
                                output += "\t<color><gray>" + _temp.description + "\n\n";
                            }
                        }

                        if (tempCommand.oPar.Count != 0)
                        {
                            output += "\t<color><yellow>OPTIONAL PARAMETER(S)\n\n";

                            foreach (ParOption _temp in tempCommand.oPar)
                            {
                                output += "\t<color><darkyellow>-" + _temp.par + " <color><white>";

                                if (_temp.multiargs)
                                {
                                    output += "<arg_1> <arg_2> ...";
                                }
                                else
                                if (!_temp.argEmpty)
                                {
                                    output += "<arg>";
                                }

                                output += "\n";
                                output += "\t<color><gray>" + _temp.description + "\n\n";
                            }
                        }
                    }
                    else
                    {
                        output += "\n\t<color><gray>(command does not use any parameters)\n";
                    }
                }
                catch (Exception)
                {
                    output = "<color><red>Command with the ID '" + commandIndex + "' does not exist!";
                }
            }

            return(output);
        }