“高级”控制台应用程序
我不确定这个问题是否已在其他地方得到解答,而且我似乎无法通过 google 找到任何不是“Hello World”示例的内容...我正在使用 C# .NET 4.0 进行编码。
我正在尝试开发一个控制台应用程序,它将打开、显示文本,然后等待用户输入命令,其中命令将运行特定的业务逻辑。
例如:如果用户打开应用程序并输入“help”,我想显示许多语句等。但我不确定如何为用户输入编写“事件处理程序”。
希望这是有道理的。任何帮助将不胜感激! 干杯。
I'm not sure if this question has been answered elsewhere and I can't seem to find anything through google that isn't a "Hello World" example... I'm coding in C# .NET 4.0.
I'm trying to develop a console application that will open, display text, and then wait for the user to input commands, where the commands will run particular business logic.
For example: If the user opens the application and types "help", I want to display a number of statements etc etc. I'm not sure how to code the 'event handler' for user input though.
Hopefully this makes sense. Any help would be much appreciated!
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要几个步骤才能实现这一目标,但这应该不那么难。首先,您需要某种解析器来解析您编写的内容。要读取每个命令,只需使用 var command = Console.ReadLine() ,然后解析该行。并执行命令...主要逻辑应该有一个看起来像这样(有点)的基础:
某种程度上,您可能可以将其更改为更复杂。
Parser
和命令的代码很简单:You need several steps to achieve this but it shouldn't be that hard. First you need some kind of parser that parses what you write. To read each command just use
var command = Console.ReadLine()
, and then parse that line. And execute the command... Main logic should have a base looking this (sort of):Sort of, you could probably change that to be more complicated.
The code for the
Parser
and command is sort of straight forward:我知道这是一个老问题,但我也在寻找答案。但我无法找到一个简单的,所以我构建了 InteractivePrompt。它以 NuGet 包 的形式提供,您可以轻松扩展 GitHub。它还具有当前会话的历史记录。
问题中的功能可以通过 InteractivePrompt 来实现:
I know this is an old question, but I was searching for an answer too. I was unable to find a simple one though, so I built InteractivePrompt. It's available as a NuGet Package and you can easily extend the code which is on GitHub. It features a history for the current session also.
The functionality in the question could be implemented this way with InteractivePrompt:
这很简单,只需使用
Console.WriteLine
和Console.ReadLine()
方法即可。从 ReadLine 你得到一个字符串。您可能有一个可怕的 if 语句来根据已知/预期的输入来验证这一点。最好有一个查找表。最复杂的是编写一个解析器。这实际上取决于输入的复杂程度。This is easy enough, just use the
Console.WriteLine
andConsole.ReadLine()
methods. From the ReadLine you get a string. You could have a horrible if statement that validate this against known/expected inputs. Better would be to have a lookup table. Most sophisticated would be to write a parser. It really depends on how complex the inputs can be.Console.WriteLine
Console.ReadLine
和Console.ReadKey
是你的朋友。 ReadLine 和 ReadKey 等待用户输入。string[] args
将包含所有参数,例如“help”。该数组是通过用空格分隔命令行参数来创建的。Console.WriteLine
Console.ReadLine
andConsole.ReadKey
are your friends. ReadLine and ReadKey waits for user input. Thestring[] args
will have all of your parameters such as 'help' in them. The array is created by separating the command line arguments by spaces.如果您要解析包含其他内容(例如参数)的命令,例如“manipulate file.txt”,那么仅此方法是行不通的。但是您可以使用 String.Split 将输入分隔为命令和参数。
If you're looking to parse commands that have other things in them like parameters, for example "manipulate file.txt" then this alone won't work. But you could for example use
String.Split
to separate the input into a command and arguments.样本:
A sample:
这非常简单,但可能会满足您的需求。
This is very simplistic, but might meet your needs.
有一个 C# nuget 包,名为 'ReadLine',作者为 'tornerdo'。语句
ReadLine.Read("prompt > ");
会在CustomAutoCompletionHandler.PossibleAutoCompleteValues
中提供的选项中提示用户。此外,您可以更改每个提示的
CustomAutoCompletionHandler.PossibleAutoCompleteValues
。这确保用户可以从可用\支持的选项列表中选择一个选项。不易出错。There is a C# nuget package called 'ReadLine' by 'tornerdo'. The statement
ReadLine.Read(" prompt > ");
prompts the user within options provided inCustomAutoCompletionHandler.PossibleAutoCompleteValues
.Additionally, you can change the
CustomAutoCompletionHandler.PossibleAutoCompleteValues
for each prompt. This ensures that the user get to choose an option from available\ supported list of options. Less error prone.