public static void Help_OnCommand(KonsoleCommandEventArgs e) { try { if (e.Length > 0 && e.Arguments[0].ToUpper() != "ALL") { KonsoleCommandEntry c = (KonsoleCommandEntry)Server.Konsole.KonsoleCommands.Commands[e.Arguments[0]]; if (c == null) { Console.WriteLine("\nType 'help all' for a list of commands or help <command>"); return; } else { HelpHeader(); ShowCommandInfo(c, null); HelpFooter(); } } else if (e.Length > 0 && e.Arguments[0].ToUpper() == "ALL") { HelpHeader(); ArrayList list = new ArrayList(Server.Konsole.KonsoleCommands.Commands.Values); Clean(list); foreach (CategoryAttribute cat in categories) { Console.WriteLine("|=- {0} -=|", cat.Category); foreach (KonsoleCommandEntry c in list) { ShowCommandInfo(c, cat); } Console.WriteLine("|"); } HelpFooter(); } else { Console.WriteLine("\nType 'help all' for a list of commands or help <command>"); } } catch (Exception ex) { Console.WriteLine(ex.Message, "\r\n", ex.StackTrace); } }
private static void Clean(ArrayList list) { for (int i = 0; i < list.Count; ++i) { KonsoleCommandEntry e = (KonsoleCommandEntry)list[i]; for (int j = i + 1; j < list.Count; ++j) { KonsoleCommandEntry c = (KonsoleCommandEntry)list[j]; if (e.Handler.Method == c.Handler.Method) { list.RemoveAt(j); --j; } } } }
private static void ShowCommandInfo(KonsoleCommandEntry e, CategoryAttribute cat) { try { MethodInfo mi = e.Handler.Method; object[] attrs = mi.GetCustomAttributes(typeof(CategoryAttribute), false); CategoryAttribute category; if (attrs.Length == 0) { category = new CategoryAttribute("[General]"); } else { category = attrs[0] as CategoryAttribute; } if (cat != null && category.Category != cat.Category) { return; } attrs = mi.GetCustomAttributes(typeof(UsageAttribute), false); if (attrs.Length == 0) { return; } UsageAttribute usage = attrs[0] as UsageAttribute; attrs = mi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs.Length == 0) { return; } DescriptionAttribute desc = attrs[0] as DescriptionAttribute; if (usage == null || desc == null) { return; } attrs = mi.GetCustomAttributes(typeof(AliasesAttribute), false); AliasesAttribute aliases = (attrs.Length == 0 ? null : attrs[0] as AliasesAttribute); string alias = ""; if (aliases != null) { string[] v = aliases.Aliases; for (int i = 0; i < v.Length; ++i) { if (i != 0) { alias = alias + ", "; } alias = alias + v[i]; } } string strInfo = usage.Usage + "\t" + desc.Description + "\t" + alias; Console.WriteLine("| " + strInfo); } catch (Exception ex) { Console.WriteLine("Error retrieving method information: " + ex.Message); } }