示例#1
0
        private static void Main()
        {
            AdvancedConsole.Title = "Simple Commands System";

            // If you aren't using System.Console in your program
            // edit the SCS.System.AdvancedConsole class
            // and edit or don't use the SCS.Commands.ConsoleCommands class

            // Setting standard values
            Command.StandardPrefix      = "!";
            Command.StandardDescription = "No desc.";

            // The default of SCS.System.Command.StandardPrefix is "/"
            // It using for the help command from the SCS.MainCommands class

            // Preparing commands for use
            Command.RegisterCommands <MainCommands>();                            // 1 variant
            Command.RegisterCommands <ConsoleCommands>();
            Command.RegisterCommands(typeof(MathCommands), typeof(FileCommands)); // 2 variant

            // Writing "Enter !help to get a list of commands" with colored "!help"
            AdvancedConsole.ColoredWriteLine(new ColoredString("Enter "),
                                             new ColoredString(ConsoleColor.DarkCyan, "!"), new ColoredString(ConsoleColor.Cyan, "help "),
                                             new ColoredString("to get a list of commands. Don't use commas between arguments and use string arguments in quotes.\n"));

            while (true)
            {
                AdvancedConsole.Write("Enter a command: ");
                string message = AdvancedConsole.ReadLine();

                AdvancedConsole.WriteLine();
                Command.Execute(message); // Parses the message to a command and executes it
                AdvancedConsole.WriteLine();
            }
        }
        public static void HelpCommand(string prefix = null)
        {
            List <Command> commands;
            ListScanner    ignoreInHelpFilter = new ListScanner("Help Ignore", ListScanner.TargetOfScanner.Tags, ListScanner.ScannerCondition.NotContains);

            if (prefix != null && !Command.Prefixes.Contains(prefix))
            {
                if (Command.Prefixes.Count <= 1)
                {
                    AdvancedConsole.Warn(AdvancedConsole.WarningType.WrongArguments);
                }
                else
                {
                    AdvancedConsole.Warn("Invalid prefix!");
                }
                return;
            }

            if (prefix == null)
            {
                #region Colored text
                AdvancedConsole.ColoredWriteLine(new CStr("Command to enter:\n"), new CStr(ConsoleColor.DarkCyan, "prefix "),
                                                 new CStr(ConsoleColor.Cyan, "name "), new CStr(ConsoleColor.Magenta, "argument1 argument2 argumentN\n"));

                AdvancedConsole.ColoredWriteLine(new CStr("Command in help:\n"),
                                                 new CStr(ConsoleColor.DarkCyan, "prefix "), new CStr(ConsoleColor.Cyan, "name "),
                                                 new CStr(ConsoleColor.DarkYellow, "parameter-type1 "), new CStr(ConsoleColor.Yellow, "parameter-name1"), new CStr(", "),
                                                 new CStr(ConsoleColor.DarkYellow, "parameter-type2 "), new CStr(ConsoleColor.Yellow, "parameter-name2 "),
                                                 new ColoredString("= "), new CStr(ConsoleColor.Magenta, "default-value "), new CStr(ConsoleColor.DarkGray, "| Description.\n"));
                #endregion
            }

            if (Command.Prefixes.Count == 1)
            {
                prefix = Command.Prefixes[0];
            }

            if (prefix == null)
            {
                commands = Command.Find(ignoreInHelpFilter, new ListScanner("Help"));
            }
            else
            {
                ListScanner          ignoreHelpCommandsFilter = new ListScanner("Help", ListScanner.TargetOfScanner.Tags, ListScanner.ScannerCondition.NotContains);
                SimpleCommandScanner prefixFilter             = new SimpleCommandScanner(prefix, SimpleCommandScanner.TargetOfScanner.Prefix);

                commands = Command.Find(ignoreInHelpFilter, ignoreHelpCommandsFilter, prefixFilter);
            }

            AdvancedConsole.WriteLine(commands.Count > 0 ? "Commands:" : "Sorry, commands not found :(");
            foreach (Command command in commands)
            {
                AdvancedConsole.ColoredWrite(ConsoleColor.DarkCyan, command.Prefix);
                AdvancedConsole.ColoredWrite(ConsoleColor.Cyan, command.Name);

                foreach (ParameterInfo parameter in command.Parameters)
                {
                    AdvancedConsole.ColoredWrite(ConsoleColor.DarkYellow, " " + parameter.ParameterType.Name + " ");
                    AdvancedConsole.ColoredWrite(ConsoleColor.Yellow, parameter.Name);

                    if (parameter.HasDefaultValue)
                    {
                        AdvancedConsole.Write(" = ");
                        AdvancedConsole.ColoredWrite(ConsoleColor.Magenta, $"{parameter.DefaultValue ?? "null"}");
                    }
                    if (parameter != command.Parameters.Last())
                    {
                        AdvancedConsole.Write(",");
                    }
                }

                AdvancedConsole.ColoredWriteLine(ConsoleColor.DarkGray, $" | {command.Description}");
            }
        }