我想在C#控制台应用程序(CLI)中编写一个框架,细节并不重要。 我不知道,如何干净地识别命令,并很快。 我试图与开关壳体:带命令行界面的C#框架
public static void command_recognizing(string command) // random example
{
string[] tmp_array = command.Split(' ');
switch(tmp_array[0])
{
case("help"):
method_library.help(); // no need argument
break;
case("time"):
method_library.time(); // no need argument
break;
case("shutdown"):
method_library.shutdown(tmp_array); // need argument
break;
default:
Console.WriteLine("Error! {0} is not a known command!",tmp_array[0]);
break;
}
}
我也试过的if-else:
public static void command_recognizing(string command) // random example
{
string[] tmp_array = command.Split(' ');
if(command.Contains("help"))
{
method_library.help(); // no need argument
}
else if(command.Contains("time"))
{
method_library.time(); // no need argument
}
else if(command.Contains("shutdown"))
{
method_library.shutdown(tmp_array); // need argument
}
else
{
Console.WriteLine("Error! {0} is not a known command!",tmp_array[0]);
}
}
我试图命令存储在一个字符串数组,仍然是相同的,长和难看。
还有什么其他方法可以让命令识别更短,更清晰,更容易修改吗? 原谅我的英语。随时纠正我!
尝试谷歌搜索 “C#如何写一个解释” –
看看这篇文章:https://stackoverflow.com/questions/673113/poor-mans-lexer-for-c-sharp –
的NuGet> CommandLineParser –