Esempio n. 1
0
        public override CommandResultCode Execute(IExecutionContext context, CommandArguments args)
        {
            if (args.Count > 1)
            {
                return(CommandResultCode.SyntaxError);
            }

            string commandName = null;

            if (args.MoveNext())
            {
                commandName = args.Current;
            }

            // nothing given: provide generic help.

            Application.Error.WriteLine();

            int maxPad = 0;

            if (commandName == null)
            {
                ICollection <Command> commands = Application.Commands.RegisteredCommands;

                // process the command groups first...
                Dictionary <string, List <CommandHelp> > groups = new Dictionary <string, List <CommandHelp> >();
                foreach (Command command in commands)
                {
                    string groupName = command.GroupName;
                    if (groupName == null || groupName.Length == 0)
                    {
                        groupName = "commands";
                    }

                    List <CommandHelp> list;
                    if (!groups.TryGetValue(groupName, out list))
                    {
                        list = new List <CommandHelp>();
                        groups[groupName] = list;
                    }

                    CommandHelp commandHelp = new CommandHelp();

                    StringBuilder cmdPrint = new StringBuilder(" ");
                    string[]      aliases  = command.Aliases;

                    cmdPrint.Append(command.Name);

                    if (aliases != null && aliases.Length > 0)
                    {
                        cmdPrint.Append(" | ");
                        for (int i = 0; i < aliases.Length; i++)
                        {
                            if (i != 0)
                            {
                                cmdPrint.Append(" | ");
                            }
                            cmdPrint.Append(aliases[i]);
                        }
                    }

                    commandHelp.Name = cmdPrint.ToString();

                    string description = command.ShortDescription;
                    if (description == null)
                    {
                        // no description ... try to get the groups description...
                    }

                    commandHelp.Description = description;

                    maxPad = Math.Max(maxPad, cmdPrint.Length);

                    list.Add(commandHelp);
                }

                foreach (KeyValuePair <string, List <CommandHelp> > entry in groups)
                {
                    string groupName = entry.Key;
                    Application.Error.Write(groupName);
                    Application.Error.Write(":");
                    Application.Error.WriteLine();

                    List <CommandHelp> commandList = entry.Value;
                    foreach (CommandHelp command in commandList)
                    {
                        Application.Error.Write("  ");
                        Application.Error.Write(command.Name);

                        if (command.Description != null)
                        {
                            for (int i = 0; i < maxPad - command.Name.Length; ++i)
                            {
                                Application.Error.Write(" ");
                            }

                            Application.Error.Write(" : ");
                            Application.Error.Write(command.Description);
                        }

                        Application.Error.WriteLine();
                    }
                }
            }
            else
            {
                CommandDispatcher disp = Application.Commands;

                string  cmdString = disp.CompleteCommandName(commandName);
                Command c         = disp.GetCommand(cmdString);
                if (c == null)
                {
                    Application.Error.WriteLine("Help: unknown command '" + cmdString + "'");
                    Application.Error.WriteLine();
                    return(CommandResultCode.ExecutionFailed);
                }

                WriteDescription(c);
            }

            Application.Error.WriteLine();
            return(CommandResultCode.Success);
        }