我正在尝试解释字符串命令以运行关联的脚本。这是我到目前为止所拥有的:
// A typical command that can come from a TextBox control
string command = "Create";
// Remove all white space
command = System.Text.RegularExpressions.Regex.Replace(command, @"\s", "");
// Make case insesitive by converting to lowercase
command = command.ToLower();
switch (command)
{
case "create": /* Run create script*/ break;
case "delete": /* Run delete script*/ break;
// etc.
}
在我引入与特定命令关联的参数之前,这很好用。
我认为这可以用圆括号表示,因此更复杂的命令如下所示:
string command = "Create(paramA, paramB, etc.)"
假设我使用这种方法走在正确的道路上,那么检测和解释参数的好方法是什么?
该命令的规则是:
'(' ')' 打开和关闭参数集 ',' 分隔每个参数
换句话说,如何检测参数的开头和结尾并正确分隔每个参数?
另一个问题当然是将命令本身与参数分开。
我考虑过使用:
command.StartsWith("create"); // etc.
但这在 switch case 条件结构中不起作用。