From a034b640dab9d08b23270e1ee8255ae92fa8f816 Mon Sep 17 00:00:00 2001 From: Justin Clarke Casey Date: Mon, 9 Feb 2009 20:52:04 +0000 Subject: * Add the ability to type help for more detailed help about a specific command if any is available --- OpenSim/Framework/Console/ConsoleBase.cs | 129 +++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 8 deletions(-) (limited to 'OpenSim/Framework/Console/ConsoleBase.cs') diff --git a/OpenSim/Framework/Console/ConsoleBase.cs b/OpenSim/Framework/Console/ConsoleBase.cs index 380894d..ef9b224 100644 --- a/OpenSim/Framework/Console/ConsoleBase.cs +++ b/OpenSim/Framework/Console/ConsoleBase.cs @@ -38,27 +38,111 @@ namespace OpenSim.Framework.Console public class Commands { + /// + /// Encapsulates a command that can be invoked from the console + /// private class CommandInfo { + /// + /// The module from which this command comes + /// public string module; + + /// + /// Very short BNF description + /// public string help_text; + + /// + /// Longer one line help text + /// public string long_help; + + /// + /// Full descriptive help for this command + /// + public string descriptive_help; + + /// + /// The method to invoke for this command + /// public CommandDelegate fn; } + /// + /// Commands organized by keyword in a tree + /// private Dictionary tree = new Dictionary(); - public List GetHelp() - { + /// + /// Get help for the given help string + /// + /// Parsed parts of the help string. If empty then general help is returned. + /// + public List GetHelp(string[] cmd) + { List help = new List(); + List helpParts = new List(cmd); + + // Remove initial help keyword + helpParts.RemoveAt(0); - help.AddRange(CollectHelp(tree)); - - help.Sort(); + // General help + if (helpParts.Count == 0) + { + help.AddRange(CollectHelp(tree)); + help.Sort(); + } + else + { + help.AddRange(CollectHelp(helpParts)); + } return help; } + + /// + /// See if we can find the requested command in order to display longer help + /// + /// + /// + private List CollectHelp(List helpParts) + { + string originalHelpRequest = string.Join(" ", helpParts.ToArray()); + List help = new List(); + + Dictionary dict = tree; + while (helpParts.Count > 0) + { + string helpPart = helpParts[0]; + + if (!dict.ContainsKey(helpPart)) + break; + + //System.Console.WriteLine("Found {0}", helpParts[0]); + + if (dict[helpPart] is Dictionary) + dict = (Dictionary)dict[helpPart]; + + helpParts.RemoveAt(0); + } + + // There was a command for the given help string + if (dict.ContainsKey(String.Empty)) + { + CommandInfo commandInfo = (CommandInfo)dict[String.Empty]; + help.Add(commandInfo.help_text); + help.Add(commandInfo.long_help); + help.Add(commandInfo.descriptive_help); + } + else + { + help.Add(string.Format("No help is available for {0}", originalHelpRequest)); + } + + return help; + } private List CollectHelp(Dictionary dict) { @@ -79,12 +163,37 @@ namespace OpenSim.Framework.Console } return result; } + + /// + /// Add a command to those which can be invoked from the console. + /// + /// + /// + /// + /// + /// + public void AddCommand( + string module, string command, string help, string longhelp, CommandDelegate fn) + { + AddCommand(module, command, help, longhelp, String.Empty, fn); + } - public void AddCommand(string module, string command, string help, string longhelp, CommandDelegate fn) + /// + /// Add a command to those which can be invoked from the console. + /// + /// + /// + /// + /// + /// + /// + public void AddCommand( + string module, string command, string help, string longhelp, string descriptivehelp, CommandDelegate fn) { string[] parts = Parser.Parse(command); Dictionary current = tree; + foreach (string s in parts) { if (current.ContainsKey(s)) @@ -105,10 +214,12 @@ namespace OpenSim.Framework.Console if (current.ContainsKey(String.Empty)) return; + CommandInfo info = new CommandInfo(); info.module = module; info.help_text = help; info.long_help = longhelp; + info.descriptive_help = descriptivehelp; info.fn = fn; current[String.Empty] = info; } @@ -285,7 +396,9 @@ namespace OpenSim.Framework.Console { DefaultPrompt = defaultPrompt; - Commands.AddCommand("console", "help", "help", "Get command list", Help); + Commands.AddCommand( + "console", "help", "help []", + "Get general command list or more detailed help on a specific command", Help); } private void AddToHistory(string text) @@ -517,7 +630,7 @@ namespace OpenSim.Framework.Console private void Help(string module, string[] cmd) { - List help = Commands.GetHelp(); + List help = Commands.GetHelp(cmd); foreach (string s in help) Output(s); -- cgit v1.1