添加"--help" C#控制台应用程序的参数 [英] Adding "--help" parameter to C# console application
问题描述
我通过命令行使用的几乎所有.exe都有一个"--help"命令调用的帮助功能.
Almost all the .exe's I use via command line have a help function that gets called by the "--help" command.
如何在C#中实现此功能?就像检查args []中的参数是否为字符串"--help"一样简单吗?
How do I implement this in C#? Is it as simple as checking whether or not the parameter in args[] is the string "--help"??
推荐答案
一个用于处理多种文化的命令行的C#代码段是...
A C# snippet for processing the command line across multiple cultures is...
string[] args = Environment.GetCommandLineArgs();
if (args.Length == 2)
{
if (args[1].ToLower(CultureInfo.InvariantCulture).IndexOf("help", System.StringComparison.Ordinal) >= 0)
{
// give help
}
}
检测逻辑可以与?"组合或者 "/?"或涵盖所有预期情况的任何其他组合.
The detection logic can be combined with "?" or "/?" or any other combination that covers all expected cases.
注意:当您从环境中获取参数时,加载器将填充arg [0].第一个用户"参数在arg [1]中.
NOTE: when you get the arguments from the Environment, arg[0] is populated by the loader. The first 'user' argument is in arg[1].
这篇关于添加"--help" C#控制台应用程序的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!