diff options
Diffstat (limited to 'OpenSim/Framework/Console')
-rw-r--r-- | OpenSim/Framework/Console/CommandConsole.cs | 114 | ||||
-rw-r--r-- | OpenSim/Framework/Console/MockConsole.cs | 59 |
2 files changed, 129 insertions, 44 deletions
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; | |||
29 | using System.Xml; | 29 | using System.Xml; |
30 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
31 | using System.Diagnostics; | 31 | using System.Diagnostics; |
32 | using System.Linq; | ||
32 | using System.Reflection; | 33 | using System.Reflection; |
33 | using System.Text; | 34 | using System.Text; |
34 | using System.Text.RegularExpressions; | 35 | using System.Text.RegularExpressions; |
@@ -40,6 +41,8 @@ namespace OpenSim.Framework.Console | |||
40 | { | 41 | { |
41 | public class Commands : ICommands | 42 | public class Commands : ICommands |
42 | { | 43 | { |
44 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
45 | |||
43 | /// <summary> | 46 | /// <summary> |
44 | /// Encapsulates a command that can be invoked from the console | 47 | /// Encapsulates a command that can be invoked from the console |
45 | /// </summary> | 48 | /// </summary> |
@@ -76,6 +79,8 @@ namespace OpenSim.Framework.Console | |||
76 | public List<CommandDelegate> fn; | 79 | public List<CommandDelegate> fn; |
77 | } | 80 | } |
78 | 81 | ||
82 | public const string GeneralHelpText = "For more information, type 'help <item>' where <item> is one of the following categories:"; | ||
83 | |||
79 | /// <value> | 84 | /// <value> |
80 | /// Commands organized by keyword in a tree | 85 | /// Commands organized by keyword in a tree |
81 | /// </value> | 86 | /// </value> |
@@ -83,6 +88,11 @@ namespace OpenSim.Framework.Console | |||
83 | new Dictionary<string, object>(); | 88 | new Dictionary<string, object>(); |
84 | 89 | ||
85 | /// <summary> | 90 | /// <summary> |
91 | /// Commands organized by module | ||
92 | /// </summary> | ||
93 | private Dictionary<string, List<CommandInfo>> m_modulesCommands = new Dictionary<string, List<CommandInfo>>(); | ||
94 | |||
95 | /// <summary> | ||
86 | /// Get help for the given help string | 96 | /// Get help for the given help string |
87 | /// </summary> | 97 | /// </summary> |
88 | /// <param name="helpParts">Parsed parts of the help string. If empty then general help is returned.</param> | 98 | /// <param name="helpParts">Parsed parts of the help string. If empty then general help is returned.</param> |
@@ -98,8 +108,8 @@ namespace OpenSim.Framework.Console | |||
98 | // General help | 108 | // General help |
99 | if (helpParts.Count == 0) | 109 | if (helpParts.Count == 0) |
100 | { | 110 | { |
101 | help.AddRange(CollectHelp(tree)); | 111 | help.Add(GeneralHelpText); |
102 | help.Sort(); | 112 | help.AddRange(CollectModulesHelp(tree)); |
103 | } | 113 | } |
104 | else | 114 | else |
105 | { | 115 | { |
@@ -118,6 +128,13 @@ namespace OpenSim.Framework.Console | |||
118 | { | 128 | { |
119 | string originalHelpRequest = string.Join(" ", helpParts.ToArray()); | 129 | string originalHelpRequest = string.Join(" ", helpParts.ToArray()); |
120 | List<string> help = new List<string>(); | 130 | List<string> help = new List<string>(); |
131 | |||
132 | // Check modules first to see if we just need to display a list of those commands | ||
133 | if (TryCollectModuleHelp(originalHelpRequest, help)) | ||
134 | { | ||
135 | help.Insert(0, GeneralHelpText); | ||
136 | return help; | ||
137 | } | ||
121 | 138 | ||
122 | Dictionary<string, object> dict = tree; | 139 | Dictionary<string, object> dict = tree; |
123 | while (helpParts.Count > 0) | 140 | while (helpParts.Count > 0) |
@@ -161,25 +178,61 @@ namespace OpenSim.Framework.Console | |||
161 | return help; | 178 | return help; |
162 | } | 179 | } |
163 | 180 | ||
164 | private List<string> CollectHelp(Dictionary<string, object> dict) | 181 | /// <summary> |
182 | /// Try to collect help for the given module if that module exists. | ||
183 | /// </summary> | ||
184 | /// <param name="moduleName"></param> | ||
185 | /// <param name="helpText">/param> | ||
186 | /// <returns>true if there was the module existed, false otherwise.</returns> | ||
187 | private bool TryCollectModuleHelp(string moduleName, List<string> helpText) | ||
165 | { | 188 | { |
166 | List<string> result = new List<string>(); | 189 | lock (m_modulesCommands) |
167 | |||
168 | foreach (KeyValuePair<string, object> kvp in dict) | ||
169 | { | 190 | { |
170 | if (kvp.Value is Dictionary<string, Object>) | 191 | if (m_modulesCommands.ContainsKey(moduleName)) |
171 | { | 192 | { |
172 | result.AddRange(CollectHelp((Dictionary<string, Object>)kvp.Value)); | 193 | List<CommandInfo> commands = m_modulesCommands[moduleName]; |
194 | var ourHelpText = commands.ConvertAll(c => string.Format("{0} - {1}", c.help_text, c.long_help)); | ||
195 | ourHelpText.Sort(); | ||
196 | helpText.AddRange(ourHelpText); | ||
197 | |||
198 | return true; | ||
173 | } | 199 | } |
174 | else | 200 | else |
175 | { | 201 | { |
176 | if (((CommandInfo)kvp.Value).long_help != String.Empty) | 202 | return false; |
177 | result.Add(((CommandInfo)kvp.Value).help_text+" - "+ | ||
178 | ((CommandInfo)kvp.Value).long_help); | ||
179 | } | 203 | } |
180 | } | 204 | } |
181 | return result; | ||
182 | } | 205 | } |
206 | |||
207 | private List<string> CollectModulesHelp(Dictionary<string, object> dict) | ||
208 | { | ||
209 | lock (m_modulesCommands) | ||
210 | { | ||
211 | List<string> helpText = new List<string>(m_modulesCommands.Keys); | ||
212 | helpText.Sort(); | ||
213 | return helpText; | ||
214 | } | ||
215 | } | ||
216 | |||
217 | // private List<string> CollectHelp(Dictionary<string, object> dict) | ||
218 | // { | ||
219 | // List<string> result = new List<string>(); | ||
220 | // | ||
221 | // foreach (KeyValuePair<string, object> kvp in dict) | ||
222 | // { | ||
223 | // if (kvp.Value is Dictionary<string, Object>) | ||
224 | // { | ||
225 | // result.AddRange(CollectHelp((Dictionary<string, Object>)kvp.Value)); | ||
226 | // } | ||
227 | // else | ||
228 | // { | ||
229 | // if (((CommandInfo)kvp.Value).long_help != String.Empty) | ||
230 | // result.Add(((CommandInfo)kvp.Value).help_text+" - "+ | ||
231 | // ((CommandInfo)kvp.Value).long_help); | ||
232 | // } | ||
233 | // } | ||
234 | // return result; | ||
235 | // } | ||
183 | 236 | ||
184 | /// <summary> | 237 | /// <summary> |
185 | /// Add a command to those which can be invoked from the console. | 238 | /// Add a command to those which can be invoked from the console. |
@@ -212,21 +265,19 @@ namespace OpenSim.Framework.Console | |||
212 | 265 | ||
213 | Dictionary<string, Object> current = tree; | 266 | Dictionary<string, Object> current = tree; |
214 | 267 | ||
215 | foreach (string s in parts) | 268 | foreach (string part in parts) |
216 | { | 269 | { |
217 | if (current.ContainsKey(s)) | 270 | if (current.ContainsKey(part)) |
218 | { | 271 | { |
219 | if (current[s] is Dictionary<string, Object>) | 272 | if (current[part] is Dictionary<string, Object>) |
220 | { | 273 | current = (Dictionary<string, Object>)current[part]; |
221 | current = (Dictionary<string, Object>)current[s]; | ||
222 | } | ||
223 | else | 274 | else |
224 | return; | 275 | return; |
225 | } | 276 | } |
226 | else | 277 | else |
227 | { | 278 | { |
228 | current[s] = new Dictionary<string, Object>(); | 279 | current[part] = new Dictionary<string, Object>(); |
229 | current = (Dictionary<string, Object>)current[s]; | 280 | current = (Dictionary<string, Object>)current[part]; |
230 | } | 281 | } |
231 | } | 282 | } |
232 | 283 | ||
@@ -250,6 +301,24 @@ namespace OpenSim.Framework.Console | |||
250 | info.fn = new List<CommandDelegate>(); | 301 | info.fn = new List<CommandDelegate>(); |
251 | info.fn.Add(fn); | 302 | info.fn.Add(fn); |
252 | current[String.Empty] = info; | 303 | current[String.Empty] = info; |
304 | |||
305 | // Now add command to modules dictionary | ||
306 | lock (m_modulesCommands) | ||
307 | { | ||
308 | List<CommandInfo> commands; | ||
309 | if (m_modulesCommands.ContainsKey(module)) | ||
310 | { | ||
311 | commands = m_modulesCommands[module]; | ||
312 | } | ||
313 | else | ||
314 | { | ||
315 | commands = new List<CommandInfo>(); | ||
316 | m_modulesCommands[module] = commands; | ||
317 | } | ||
318 | |||
319 | // m_log.DebugFormat("[COMMAND CONSOLE]: Adding to category {0} command {1}", module, command); | ||
320 | commands.Add(info); | ||
321 | } | ||
253 | } | 322 | } |
254 | 323 | ||
255 | public string[] FindNextOption(string[] cmd, bool term) | 324 | public string[] FindNextOption(string[] cmd, bool term) |
@@ -607,8 +676,9 @@ namespace OpenSim.Framework.Console | |||
607 | { | 676 | { |
608 | Commands = new Commands(); | 677 | Commands = new Commands(); |
609 | 678 | ||
610 | Commands.AddCommand("console", false, "help", "help [<command>]", | 679 | Commands.AddCommand( |
611 | "Get general command list or more detailed help on a specific command", Help); | 680 | "Help", false, "help", "help [<item>]", |
681 | "Display help on a particular command or on a list of commands in a category", Help); | ||
612 | } | 682 | } |
613 | 683 | ||
614 | private void Help(string module, string[] cmd) | 684 | private void Help(string module, string[] cmd) |
diff --git a/OpenSim/Framework/Console/MockConsole.cs b/OpenSim/Framework/Console/MockConsole.cs index a29b370..4d8751f 100644 --- a/OpenSim/Framework/Console/MockConsole.cs +++ b/OpenSim/Framework/Console/MockConsole.cs | |||
@@ -29,6 +29,7 @@ using System; | |||
29 | using System.Threading; | 29 | using System.Threading; |
30 | using System.Collections.Generic; | 30 | using System.Collections.Generic; |
31 | using System.Text; | 31 | using System.Text; |
32 | using System.Xml; | ||
32 | 33 | ||
33 | namespace OpenSim.Framework.Console | 34 | namespace OpenSim.Framework.Console |
34 | { | 35 | { |
@@ -37,28 +38,42 @@ namespace OpenSim.Framework.Console | |||
37 | /// Don't use this except for Unit Testing or you're in for a world of hurt when the | 38 | /// Don't use this except for Unit Testing or you're in for a world of hurt when the |
38 | /// sim gets to ReadLine | 39 | /// sim gets to ReadLine |
39 | /// </summary> | 40 | /// </summary> |
40 | public class MockConsole : CommandConsole | 41 | public class MockConsole : ICommandConsole |
41 | { | 42 | { |
42 | public MockConsole(string defaultPrompt) : base(defaultPrompt) | 43 | private MockCommands m_commands = new MockCommands(); |
43 | { | ||
44 | } | ||
45 | public override void Output(string text) | ||
46 | { | ||
47 | } | ||
48 | public override void Output(string text, string level) | ||
49 | { | ||
50 | } | ||
51 | 44 | ||
52 | public override string ReadLine(string p, bool isCommand, bool e) | 45 | public ICommands Commands { get { return m_commands; } } |
53 | { | 46 | |
54 | //Thread.CurrentThread.Join(1000); | 47 | public void Prompt() {} |
55 | return string.Empty; | 48 | |
56 | } | 49 | public void RunCommand(string cmd) {} |
57 | public override void UnlockOutput() | 50 | |
58 | { | 51 | public string ReadLine(string p, bool isCommand, bool e) { return ""; } |
59 | } | 52 | |
60 | public override void LockOutput() | 53 | public object ConsoleScene { get { return null; } } |
61 | { | 54 | |
62 | } | 55 | public void Output(string text, string level) {} |
56 | public void Output(string text) {} | ||
57 | public void OutputFormat(string format, params object[] components) {} | ||
58 | |||
59 | public string CmdPrompt(string p) { return ""; } | ||
60 | public string CmdPrompt(string p, string def) { return ""; } | ||
61 | public string CmdPrompt(string p, List<char> excludedCharacters) { return ""; } | ||
62 | public string CmdPrompt(string p, string def, List<char> excludedCharacters) { return ""; } | ||
63 | |||
64 | public string CmdPrompt(string prompt, string defaultresponse, List<string> options) { return ""; } | ||
65 | |||
66 | public string PasswdPrompt(string p) { return ""; } | ||
67 | } | ||
68 | |||
69 | public class MockCommands : ICommands | ||
70 | { | ||
71 | public void FromXml(XmlElement root, CommandDelegate fn) {} | ||
72 | public List<string> GetHelp(string[] cmd) { return null; } | ||
73 | public void AddCommand(string module, bool shared, string command, string help, string longhelp, CommandDelegate fn) {} | ||
74 | public void AddCommand(string module, bool shared, string command, string help, string longhelp, string descriptivehelp, CommandDelegate fn) {} | ||
75 | public string[] FindNextOption(string[] cmd, bool term) { return null; } | ||
76 | public string[] Resolve(string[] cmd) { return null; } | ||
77 | public XmlElement GetXml(XmlDocument doc) { return null; } | ||
63 | } | 78 | } |
64 | } | 79 | } \ No newline at end of file |