C#:如何检测输入到控制台应用程序中的参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/316463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: How to detect arguments typed into console application?
提问by
How would I go upon detecting input for a console application in C#?
我将如何在 C# 中检测控制台应用程序的输入?
Let's say for example I want the console application to start up by writing: Welcome To Food Hut (cursor to type stuff here after the first line)
例如,假设我希望控制台应用程序通过编写以下内容启动:Welcome To Food Hut(光标在第一行之后在此处输入内容)
I would want the console application to detect two commands:
我希望控制台应用程序检测两个命令:
1: /help - which will display some help gibberish.
1: /help - 这将显示一些帮助乱码。
2: /food pizza -t pepperoni -d pepsi - which will display "So you would like a Pizza with Pepperoni and Pepsi to drink?"
2: /foodizza -t 意大利辣香肠 -d pepsi - 这将显示“所以你想要一个带有意大利辣香肠和百事可乐的披萨来喝?”
How would I go upon detecting first what /command was typed and also reading the arguments like -t pepperoni (topping) and -d pepsi (to drink) if /food pizza was typed?
如果输入了 /food 披萨,我将如何首先检测输入的 /command 并读取诸如 -t 意大利辣香肠(浇头)和 -d 百事可乐(喝)之类的参数?
My main problem is figuring out how to detect the first word ever typed, figuring out that if it was /help then call some method that would post some help text into the console or if the command is /food then to read what is after the /food command, -t, and -p.
我的主要问题是弄清楚如何检测输入的第一个单词,确定它是否是 /help 然后调用一些方法将一些帮助文本发布到控制台,或者如果命令是 /food 然后读取后面的内容/food 命令、-t 和 -p。
static void Main(string[] args)
{
Console.WriteLine("Welcome To Food Hut");
Console.ReadLine();
// if readline equals to /help then display some help text.
// if /food command is typed, read first argument after /food Pizza, -t TheTopping
// and -p ForWhatToDrink
// and then display, 'So you would like a Pizza with Pepperoni and Pepsi to drink?'
}
回答by Ana Betts
Look into String.Contains
查看 String.Contains
回答by Juliet
My immediate suggestion would be something like this:
我的直接建议是这样的:
string input = Console.ReadLine();
if (input == "/help") { }
else if (input.StartsWith("/food")) { }
else { //... }
Barring that, the next simplest solution involves regex. If you need something truly extensible, you might look into a lexer/parser.
除此之外,下一个最简单的解决方案涉及正则表达式。如果您需要真正可扩展的东西,您可以查看词法分析器/解析器。
But then again, something tells me you're approaching the problem the wrong way. Maybe it would just be easier to display a menu to your user, something like this:
但话又说回来,有些事情告诉我你正在以错误的方式解决问题。也许向用户显示菜单会更容易,如下所示:
Food:
1) Tofu
2) Tempeh
3) Seitan
4) Soup
Choice (1-4)? [users input]
Drink:
1) Pepsi
2) Coffee
3) Water
4) Tea
5) Juice
Choice (1-5)? [users input]
Numbered menus might be a little easier for your users to digest.
编号菜单可能更容易让您的用户消化。
回答by Alan
Paul is right, you will need to use the various String members to accomplish this task.
Paul 是对的,您将需要使用各种 String 成员来完成此任务。
If you are feeling really enterprise, you can look at using the String.Text.RegularExpression class to parse the text, but to start out, use the string members.
如果您真的很有企业精神,您可以考虑使用 String.Text.RegularExpression 类来解析文本,但首先,请使用字符串成员。
Also try googling C# String parsing.
也尝试谷歌搜索 C# 字符串解析。
回答by David Vidmar
To skip coding basics, you might want to take a look inside Genghis project's Command line parsing sub-project by Chris Sells. It's first one in the table on linked page.
要跳过编码基础知识,您可能需要查看Chris Sells 的Genghis 项目的命令行解析子项目。它是链接页面上表格中的第一个。
回答by David Vidmar
Use string.split to find all words separated by a space.
使用 string.split 查找由空格分隔的所有单词。
input = Console.ReadLine();
string[] commands = input.Split(' ');
if(commands[0] == "/food")
{
if(commands[1] == "Pizza");
.....
}
回答by Stefan Schultze
I recommend Richard Lopes' Command Line Arguments Parser. It is powerful and very simple to use. Also, it accepts various ways of specifying the arguments, for example:
我推荐Richard Lopes 的 Command Line Arguments Parser。它功能强大且使用起来非常简单。此外,它还接受各种指定参数的方式,例如:
- /name=Stefan
- --name=Stefan
- --name="Multiple words"
- -name 'Stefan'
- /姓名=斯蒂芬
- --name=斯蒂芬
- --name="多个单词"
- -名字“斯蒂芬”
Example Code:
示例代码:
static void Main(string[] args)
{
Arguments cmdline = new Arguments(args);
Console.WriteLine(cmdline["name"]);
}
回答by Adrian
What about this case:
这个案子呢:
mssinp.exe -cf "C:\Temp\config.txt"
the value for the parameter will be splited as
参数的值将被拆分为
[0] 'C'
[1] '\Temp\config.txt'
回答by deepak
Came across gsscoder/commandline. A clean Object Oriented implementation, available through NuGet.
遇到了gscoder/commandline。一个干净的面向对象的实现,可通过 NuGet 获得。