C#:如何检测键入到控制台应用程序中的参数? [英] C#: How to detect arguments typed into console application?
问题描述
在C#中检测到控制台应用程序的输入时我将如何处理?
How would I go upon detecting input for a console application in C#?
例如,我希望通过编写以下内容来启动控制台应用程序:
Welcome to Food Hut
(光标在第一行之后输入内容)
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)
我希望控制台应用程序检测到两个命令:
I would want the console application to detect two commands:
1:/帮助-将显示一些帮助乱码。
1: /help - which will display some help gibberish.
2:/食品披萨-t意大利辣香肠-d百事可乐-它将显示所以,您想喝含意大利辣香肠和百事可乐的比萨饼吗?
2: /food pizza -t pepperoni -d pepsi - which will display "So you would like a Pizza with Pepperoni and Pepsi to drink?"
在首先检测到键入的/ command并读取参数后,我将如何处理?
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?
我的主要问题是弄清楚如何检测有史以来的第一个单词,例如-t意大利辣香肠(顶部)和-d百事可乐(饮用),例如?键入,弄清楚如果是/ help,则调用某种方法,该方法会将一些帮助文本发布到控制台中,或者如果命令是/ food,则读取/ food命令-t和-p之后的内容。 / p>
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.
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?'
}
推荐答案
我建议 Richard Lopes的命令线参数解析器。它功能强大且使用非常简单。此外,它接受各种指定参数的方式,例如:
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:
- / name = Stefan
- -name = Stefan
- -name =多个单词
- -名称'Stefan'
- /name=Stefan
- --name=Stefan
- --name="Multiple words"
- -name 'Stefan'
示例代码:
static void Main(string[] args)
{
Arguments cmdline = new Arguments(args);
Console.WriteLine(cmdline["name"]);
}
这篇关于C#:如何检测键入到控制台应用程序中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!