OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
Command.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 using System.Collections.Generic;
30 using OpenSim.Region.Framework.Interfaces;
31 using OpenMetaverse;
32 
33 namespace OpenSim.Region.CoreModules.Framework.InterfaceCommander
34 {
39  public class Command : ICommand
40  {
41  //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
42  private List<CommandArgument> m_args = new List<CommandArgument>();
43 
44  private Action<object[]> m_command;
45  private string m_help;
46  private string m_name;
47  private CommandIntentions m_intentions; //A permission type system could implement this and know what a command intends on doing.
48 
49  public Command(string name, CommandIntentions intention, Action<Object[]> command, string help)
50  {
51  m_name = name;
52  m_command = command;
53  m_help = help;
54  m_intentions = intention;
55  }
56 
57  #region ICommand Members
58 
59  public void AddArgument(string name, string helptext, string type)
60  {
61  m_args.Add(new CommandArgument(name, helptext, type));
62  }
63 
64  public string Name
65  {
66  get { return m_name; }
67  }
68 
69  public CommandIntentions Intentions
70  {
71  get { return m_intentions; }
72  }
73 
74  public string Help
75  {
76  get { return m_help; }
77  }
78 
79  public Dictionary<string, string> Arguments
80  {
81  get
82  {
83  Dictionary<string, string> tmp = new Dictionary<string, string>();
84  foreach (CommandArgument arg in m_args)
85  {
86  tmp.Add(arg.Name, arg.ArgumentType);
87  }
88  return tmp;
89  }
90  }
91 
92  public string ShortHelp()
93  {
94  string help = m_name;
95 
96  foreach (CommandArgument arg in m_args)
97  {
98  help += " <" + arg.Name + ">";
99  }
100 
101  return help;
102  }
103 
104  public void ShowConsoleHelp()
105  {
106  Console.WriteLine("== " + Name + " ==");
107  Console.WriteLine(m_help);
108  Console.WriteLine("= Parameters =");
109  foreach (CommandArgument arg in m_args)
110  {
111  Console.WriteLine("* " + arg.Name + " (" + arg.ArgumentType + ")");
112  Console.WriteLine("\t" + arg.HelpText);
113  }
114  }
115 
116  public void Run(Object[] args)
117  {
118  Object[] cleanArgs = new Object[m_args.Count];
119 
120  if (args.Length < cleanArgs.Length)
121  {
122  Console.WriteLine("ERROR: Missing " + (cleanArgs.Length - args.Length) + " argument(s)");
123  ShowConsoleHelp();
124  return;
125  }
126  if (args.Length > cleanArgs.Length)
127  {
128  Console.WriteLine("ERROR: Too many arguments for this command. Type '<module> <command> help' for help.");
129  return;
130  }
131 
132  int i = 0;
133  foreach (Object arg in args)
134  {
135  if (string.IsNullOrEmpty(arg.ToString()))
136  {
137  Console.WriteLine("ERROR: Empty arguments are not allowed");
138  return;
139  }
140  try
141  {
142  switch (m_args[i].ArgumentType)
143  {
144  case "String":
145  m_args[i].ArgumentValue = arg.ToString();
146  break;
147  case "Integer":
148  m_args[i].ArgumentValue = Int32.Parse(arg.ToString());
149  break;
150  case "Double":
151  m_args[i].ArgumentValue = Double.Parse(arg.ToString(), OpenSim.Framework.Culture.NumberFormatInfo);
152  break;
153  case "Boolean":
154  m_args[i].ArgumentValue = Boolean.Parse(arg.ToString());
155  break;
156  case "UUID":
157  m_args[i].ArgumentValue = UUID.Parse(arg.ToString());
158  break;
159  default:
160  Console.WriteLine("ERROR: Unknown desired type for argument " + m_args[i].Name + " on command " + m_name);
161  break;
162  }
163  }
164  catch (FormatException)
165  {
166  Console.WriteLine("ERROR: Argument number " + (i + 1) +
167  " (" + m_args[i].Name + ") must be a valid " +
168  m_args[i].ArgumentType.ToLower() + ".");
169  return;
170  }
171  cleanArgs[i] = m_args[i].ArgumentValue;
172 
173  i++;
174  }
175 
176  m_command.Invoke(cleanArgs);
177  }
178 
179  #endregion
180  }
181 
185  public class CommandArgument
186  {
187  private string m_help;
188  private string m_name;
189  private string m_type;
190  private Object m_val;
191 
192  public CommandArgument(string name, string help, string type)
193  {
194  m_name = name;
195  m_help = help;
196  m_type = type;
197  }
198 
199  public string Name
200  {
201  get { return m_name; }
202  }
203 
204  public string HelpText
205  {
206  get { return m_help; }
207  }
208 
209  public string ArgumentType
210  {
211  get { return m_type; }
212  }
213 
214  public Object ArgumentValue
215  {
216  get { return m_val; }
217  set { m_val = value; }
218  }
219  }
220 }
A single function call encapsulated in a class which enforces arguments when passing around as Object...
Definition: Command.cs:39
Interactive OpenSim region server
Definition: OpenSim.cs:55
void AddArgument(string name, string helptext, string type)
Definition: Command.cs:59
Command(string name, CommandIntentions intention, Action< Object[]> command, string help)
Definition: Command.cs:49
A single command argument, contains name, type and at runtime, value.
Definition: Command.cs:185