From 749c3fef8ad2d3af97fcd9ab9c72740675e46715 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Thu, 8 Mar 2012 01:51:37 +0000 Subject: Change "help" to display categories/module list then "help " to display commands in a category. This is to deal with the hundred lines of command splurge when one previously typed "help" Modelled somewhat on the mysql console One can still type help to get per command help at any point. Categories capitalized to avoid conflict with the all-lowercase commands (except for commander system, as of yet). Does not affect command parsing or any other aspects of the console apart from the help system. Backwards compatible with existing modules. --- OpenSim/Framework/Console/CommandConsole.cs | 114 ++++++++++++++++++++++------ 1 file changed, 92 insertions(+), 22 deletions(-) (limited to 'OpenSim/Framework/Console/CommandConsole.cs') diff --git a/OpenSim/Framework/Console/CommandConsole.cs b/OpenSim/Framework/Console/CommandConsole.cs index 0d6288b..2bb7de1 100644 --- a/OpenSim/Framework/Console/CommandConsole.cs +++ b/OpenSim/Framework/Console/CommandConsole.cs @@ -29,6 +29,7 @@ using System; using System.Xml; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -40,6 +41,8 @@ namespace OpenSim.Framework.Console { public class Commands : ICommands { +// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + /// /// Encapsulates a command that can be invoked from the console /// @@ -76,6 +79,8 @@ namespace OpenSim.Framework.Console public List fn; } + public const string GeneralHelpText = "For more information, type 'help ' where is one of the following categories:"; + /// /// Commands organized by keyword in a tree /// @@ -83,6 +88,11 @@ namespace OpenSim.Framework.Console new Dictionary(); /// + /// Commands organized by module + /// + private Dictionary> m_modulesCommands = new Dictionary>(); + + /// /// Get help for the given help string /// /// Parsed parts of the help string. If empty then general help is returned. @@ -98,8 +108,8 @@ namespace OpenSim.Framework.Console // General help if (helpParts.Count == 0) { - help.AddRange(CollectHelp(tree)); - help.Sort(); + help.Add(GeneralHelpText); + help.AddRange(CollectModulesHelp(tree)); } else { @@ -118,6 +128,13 @@ namespace OpenSim.Framework.Console { string originalHelpRequest = string.Join(" ", helpParts.ToArray()); List help = new List(); + + // Check modules first to see if we just need to display a list of those commands + if (TryCollectModuleHelp(originalHelpRequest, help)) + { + help.Insert(0, GeneralHelpText); + return help; + } Dictionary dict = tree; while (helpParts.Count > 0) @@ -161,25 +178,61 @@ namespace OpenSim.Framework.Console return help; } - private List CollectHelp(Dictionary dict) + /// + /// Try to collect help for the given module if that module exists. + /// + /// + /// /param> + /// true if there was the module existed, false otherwise. + private bool TryCollectModuleHelp(string moduleName, List helpText) { - List result = new List(); - - foreach (KeyValuePair kvp in dict) + lock (m_modulesCommands) { - if (kvp.Value is Dictionary) + if (m_modulesCommands.ContainsKey(moduleName)) { - result.AddRange(CollectHelp((Dictionary)kvp.Value)); + List commands = m_modulesCommands[moduleName]; + var ourHelpText = commands.ConvertAll(c => string.Format("{0} - {1}", c.help_text, c.long_help)); + ourHelpText.Sort(); + helpText.AddRange(ourHelpText); + + return true; } else { - if (((CommandInfo)kvp.Value).long_help != String.Empty) - result.Add(((CommandInfo)kvp.Value).help_text+" - "+ - ((CommandInfo)kvp.Value).long_help); + return false; } } - return result; } + + private List CollectModulesHelp(Dictionary dict) + { + lock (m_modulesCommands) + { + List helpText = new List(m_modulesCommands.Keys); + helpText.Sort(); + return helpText; + } + } + +// private List CollectHelp(Dictionary dict) +// { +// List result = new List(); +// +// foreach (KeyValuePair kvp in dict) +// { +// if (kvp.Value is Dictionary) +// { +// result.AddRange(CollectHelp((Dictionary)kvp.Value)); +// } +// else +// { +// if (((CommandInfo)kvp.Value).long_help != String.Empty) +// result.Add(((CommandInfo)kvp.Value).help_text+" - "+ +// ((CommandInfo)kvp.Value).long_help); +// } +// } +// return result; +// } /// /// Add a command to those which can be invoked from the console. @@ -212,21 +265,19 @@ namespace OpenSim.Framework.Console Dictionary current = tree; - foreach (string s in parts) + foreach (string part in parts) { - if (current.ContainsKey(s)) + if (current.ContainsKey(part)) { - if (current[s] is Dictionary) - { - current = (Dictionary)current[s]; - } + if (current[part] is Dictionary) + current = (Dictionary)current[part]; else return; } else { - current[s] = new Dictionary(); - current = (Dictionary)current[s]; + current[part] = new Dictionary(); + current = (Dictionary)current[part]; } } @@ -250,6 +301,24 @@ namespace OpenSim.Framework.Console info.fn = new List(); info.fn.Add(fn); current[String.Empty] = info; + + // Now add command to modules dictionary + lock (m_modulesCommands) + { + List commands; + if (m_modulesCommands.ContainsKey(module)) + { + commands = m_modulesCommands[module]; + } + else + { + commands = new List(); + m_modulesCommands[module] = commands; + } + +// m_log.DebugFormat("[COMMAND CONSOLE]: Adding to category {0} command {1}", module, command); + commands.Add(info); + } } public string[] FindNextOption(string[] cmd, bool term) @@ -607,8 +676,9 @@ namespace OpenSim.Framework.Console { Commands = new Commands(); - Commands.AddCommand("console", false, "help", "help []", - "Get general command list or more detailed help on a specific command", Help); + Commands.AddCommand( + "Help", false, "help", "help []", + "Display help on a particular command or on a list of commands in a category", Help); } private void Help(string module, string[] cmd) -- cgit v1.1