OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
ConsolePluginCommand.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 using System;
29 
30 namespace OpenSim.Framework.Console
31 {
32  public delegate void ConsoleCommand(string[] comParams);
33 
39  public class ConsolePluginCommand
40  {
44  private ConsoleCommand m_commandDelegate;
48  private string m_helpText;
52  private string[] m_cmdText;
53 
63  public ConsolePluginCommand(string command, ConsoleCommand dlg, string help)
64  {
65  m_cmdText = command.Split(new char[] { ' ' });
66  m_commandDelegate = dlg;
67  m_helpText = help;
68  }
69 
79  public int matchLength(string cmdWithParams)
80  {
81  // QUESTION: have a case insensitive flag?
82  cmdWithParams = cmdWithParams.ToLower().Trim();
83  string matchText = String.Join(" ",m_cmdText).ToLower().Trim();
84  if (cmdWithParams.StartsWith(matchText))
85  {
86  // QUESTION Instead return cmdText.Length; ?
87  return matchText.Length;
88  }
89  return 0;
90  }
91 
95  public void Run(string cmd, string[] cmdParams)
96  {
97  int skipParams = 0;
98  if (m_cmdText.Length > 1)
99  {
100  int currentParam = 1;
101  while (currentParam < m_cmdText.Length)
102  {
103  if (cmdParams[skipParams].ToLower().Equals(m_cmdText[currentParam].ToLower()))
104  {
105  skipParams++;
106  }
107  currentParam++;
108  }
109 
110  }
111  string[] sendCmdParams = cmdParams;
112  if (skipParams > 0)
113  {
114  sendCmdParams = new string[cmdParams.Length-skipParams];
115  for (int i=0;i<sendCmdParams.Length;i++) {
116  sendCmdParams[i] = cmdParams[skipParams++];
117  }
118  }
119  m_commandDelegate(sendCmdParams);//.Trim().Split(new char[] { ' ' }));
120  }
121 
125  public void ShowHelp(ConsoleBase console)
126  {
127  console.Output(String.Join(" ", m_cmdText) + " - " + m_helpText + "\n");
128  }
129 
133  public bool IsHelpfull(string cmdWithParams)
134  {
135  cmdWithParams = cmdWithParams.ToLower();
136  return cmdWithParams.Contains(String.Join(" ", m_cmdText).ToLower()) || m_helpText.ToLower().Contains(cmdWithParams);
137  }
138  }
139 }
void Run(string cmd, string[] cmdParams)
Run the delegate the incomming string may contain the command, if so, it is chopped off the cmdParams...
bool IsHelpfull(string cmdWithParams)
return true if the ShowHelp(..) method might be helpfull
int matchLength(string cmdWithParams)
Returns the match length this command has upon the 'cmdWithParams' At least a higher number for "show...
void ShowHelp(ConsoleBase console)
Shows help information on the console's Notice method
ConsolePluginCommand(string command, ConsoleCommand dlg, string help)
Construct a new ConsolePluginCommand
delegate void ConsoleCommand(string[] comParams)
Holder object for a new console plugin command