我不确定这个问题是否已在其他地方得到解答,我似乎无法通过谷歌找到任何不是“Hello World”示例...我在C#.NET 4.0中进行编码。
我正在尝试开发一个控制台应用程序,它将打开,显示文本,然后等待用户输入命令,其中命令将运行特定的业务逻辑。
例如:如果用户打开应用程序并输入“help”,我想显示一些语句等等。我不知道如何为用户输入编写“事件处理程序”代码。
希望这是有道理的。任何帮助将非常感激! 欢呼声。
答案 0 :(得分:20)
您需要几个步骤来实现这一目标,但不应该那么难。首先,您需要某种解析您所写内容的解析器。要阅读每个命令,只需使用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();
.
.
.
.
}
}
}
答案 1 :(得分:1)
我知道这是一个老问题,但我也在寻找答案。我找不到一个简单的,所以我建立了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);
}
答案 2 :(得分:0)
这很简单,只需使用Console.WriteLine
和Console.ReadLine()
方法即可。从ReadLine你得到一个字符串。你可能会有一个可怕的if语句来验证已知/预期输入。更好的是拥有一个查找表。最复杂的是编写解析器。这实际上取决于输入的复杂程度。
答案 3 :(得分:0)
Console.WriteLine
Console.ReadLine
和Console.ReadKey
是您的朋友。 ReadLine和ReadKey等待用户输入。 string[] args
将包含所有参数,例如“帮助”。通过用空格分隔命令行参数来创建数组。
答案 4 :(得分:0)
switch (Console.ReadLine())
{
case "Help":
// print help
break;
case "Other Command":
// do other command
break;
// etc.
default:
Console.WriteLine("Bad Command");
break;
}
如果您正在寻找解析其中包含其他内容的命令(例如“manipulate file.txt”),那么仅此一项将无效。但是你可以使用String.Split
将输入分成命令和参数。
答案 5 :(得分:0)
这非常简单,但可能符合您的需求。
// 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;
}
}
}
答案 6 :(得分:0)
样本:
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;
}
}
}
}