我不確定這個問題是否已在別處得到解答,我似乎無法通過谷歌找到任何不是「Hello World」示例的內容......我使用C# .NET 4.0。'高級'控制檯應用程序
我想開發一個控制檯應用程序,它將打開,顯示文本,然後等待用戶輸入命令,其中命令將運行特定的業務邏輯。
例如:如果用戶打開應用程序並鍵入「幫助」,我想顯示一些語句等等等等。我不知道如何編寫用於用戶輸入的'事件處理程序'。
希望這是有道理的。任何幫助將非常感激! 乾杯。
我不確定這個問題是否已在別處得到解答,我似乎無法通過谷歌找到任何不是「Hello World」示例的內容......我使用C# .NET 4.0。'高級'控制檯應用程序
我想開發一個控制檯應用程序,它將打開,顯示文本,然後等待用戶輸入命令,其中命令將運行特定的業務邏輯。
例如:如果用戶打開應用程序並鍵入「幫助」,我想顯示一些語句等等等等。我不知道如何編寫用於用戶輸入的'事件處理程序'。
希望這是有道理的。任何幫助將非常感激! 乾杯。
你需要幾個步驟來實現這一點,但它不應該那麼難。首先,你需要某種解析器來解析你寫的東西。要閱讀每個命令只需使用var command = Console.ReadLine()
,然後解析該行。並執行命令......主要邏輯應該有一個基地尋找這個(排序):
public static void Main(string[] args)
{
var exit = false;
while(exit == false)
{
Console.WriteLine();
Console.WriteLine("Enter command (help to display help): ");
var command = Parser.Parse(Console.ReadLine());
exit = command.Execute();
}
}
排序的,你很可能改變這種狀況要複雜的多。
的Parser
和命令的代碼是那種直截了當:
public interface ICommand
{
bool Execute();
}
public class ExitCommand : ICommand
{
public bool Execute()
{
return true;
}
}
public static Class Parser
{
public static ICommand Parse(string commandString) {
// Parse your string and create Command object
var commandParts = commandString.Split(' ').ToList();
var commandName = commandParts[0];
var args = commandParts.Skip(1).ToList(); // the arguments is after the command
switch(commandName)
{
// Create command based on CommandName (and maybe arguments)
case "exit": return new ExitCommand();
.
.
.
.
}
}
}
非常感謝,這絕對是我在尋找的東西 – keynesiancross 2011-01-05 14:44:22
對不起,界面是如何工作的這種情況?我沒有使用接口,所以這可能是我的問題... – keynesiancross 2011-01-05 14:50:40
接口只是指定角色對象應該具有哪種方法,並且可以定義幾個不同的繼承自接口的對象,讓你的解析器爲你創建這些對象,你可以將接口改變爲你想要的,我只是用一個簡單的'Execute'來完成,如果程序應該退出就返回'true',我可以用一個簡單的命令來更新,樣本 – 2011-01-05 14:57:09
這很簡單,只需使用Console.WriteLine
和Console.ReadLine()
方法即可。從ReadLine中獲得一個字符串。你可能會有一個可怕的if語句來驗證這個反對已知/預期的輸入。最好是有一個查詢表。最複雜的將是編寫一個解析器。這實際上取決於輸入的複雜程度。
但是使用Console.ReadLine()方法,那麼如何編碼不同的答案等排列呢?例如Console.ReadLine(),if(Console.ReadLine()==「help){} etc etc – keynesiancross 2011-01-05 14:42:16
Console.WriteLine
Console.ReadLine
和Console.ReadKey
是你的朋友。 ReadLine和ReadKey等待用戶輸入。 string[] args
將包含所有參數,例如「幫助」。該數組是通過用空格分隔命令行參數來創建的。
switch (Console.ReadLine())
{
case "Help":
// print help
break;
case "Other Command":
// do other command
break;
// etc.
default:
Console.WriteLine("Bad Command");
break;
}
如果你正在尋找能夠解析對他們有其他的東西像參數的命令,例如「操縱file.txt的」,那麼這不會單獨工作。但是,例如,您可以使用String.Split
將輸入分隔爲一個命令和參數。
這很簡單,但可能會滿足您的需求。
// somewhere to store the input
string userInput="";
// loop until the exit command comes in.
while (userInput != "exit")
{
// display a prompt
Console.Write("> ");
// get the input
userInput = Console.ReadLine().ToLower();
// Branch based on the input
switch (userInput)
{
case "exit":
break;
case "help":
{
DisplayHelp();
break;
}
case "option1":
{
DoOption1();
break;
}
// Give the user every opportunity to invoke your help system :)
default:
{
Console.WriteLine ("\"{0}\" is not a recognized command. Type \"help\" for options.", userInput);
break;
}
}
}
樣本:
static void Main(string[] args)
{
Console.WriteLine("Welcome to test console app, type help to get some help!");
while (true)
{
string input = Console.ReadLine();
int commandEndIndex = input.IndexOf(' ');
string command = string.Empty;
string commandParameters = string.Empty;
if (commandEndIndex > -1)
{
command = input.Substring(0, commandEndIndex);
commandParameters = input.Substring(commandEndIndex + 1, input.Length - commandEndIndex - 1);
}
else
{
command = input;
}
command = command.ToUpper();
switch (command)
{
case "EXIT":
{
return;
}
case "HELP":
{
Console.WriteLine("- enter EXIT to exit this application");
Console.WriteLine("- enter CLS to clear the screen");
Console.WriteLine("- enter FORECOLOR value to change text fore color (sample: FORECOLOR Red) ");
Console.WriteLine("- enter BACKCOLOR value to change text back color (sample: FORECOLOR Green) ");
break;
}
case "CLS":
{
Console.Clear();
break;
}
case "FORECOLOR":
{
try
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
case "BACKCOLOR":
{
try
{
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
default:
{
Console.WriteLine("!!! Bad command");
break;
}
}
}
}
這不是一個好的設計,因爲你的功能是做所有的工作。你應該分工,所以每個部分都有一個責任。 – 2011-01-05 15:06:41
我同意這一點,但我發現他正在尋找他可以學習的代碼,但並不太複雜。你是正確的,邏輯應該分開。最後,你和我的結果都是一樣的。 – HABJAN 2011-01-05 15:12:58
它最終的結果肯定是一樣的,但我認爲我的解決方案不是那麼複雜,重要的是你要學會如何以正確的方式做事......儘管我可能不適合每個人但設計是你應該考慮的事情。我甚至認爲我的解決方案比較容易,因爲當你閱讀它時它更「流利」,如果你不需要它們,你可以跳過所有的細節。在高層次上,只讀,讀取命令,解析命令並最後執行命令。也就是說,您可以跳過命令解析和執行方式的細節。 – 2011-01-05 15:22:45
我知道這是一個老問題,但我正在尋找一個答案了。我無法找到一個簡單的,所以我建立了InteractivePrompt。它的格式爲NuGet Package,您可以輕鬆擴展GitHub上的代碼。它還具有當前會話的歷史記錄。
在討論的功能可以以這種方式實現與InteractivePrompt:
static string Help(string strCmd)
{
// ... logic
return "Help text";
}
static string OtherMethod(string strCmd)
{
// ... more logic
return "Other method";
}
static void Main(string[] args)
{
var prompt = "> ";
var startupMsg = "BizLogic Interpreter";
InteractivePrompt.Run(
((strCmd, listCmd) =>
{
string result;
switch (strCmd.ToLower())
{
case "help":
result = Help(strCmd);
break;
case "othermethod":
result = OtherMethod(strCmd);
break;
default:
result = "I'm sorry, I don't recognize that command.";
break;
}
return result + Environment.NewLine;
}), prompt, startupMsg);
}
代碼的簡單應用程序,說:「錯誤的命令」,不管什麼類型的用戶。然後發佈你的代碼,並提出更具體的問題。 – Jon 2011-01-05 14:40:32
我肯定會,但我真的沒有任何代碼,因爲我不知道從哪裏開始... – keynesiancross 2011-01-05 14:45:51