C# 在C中处理命令行选项的最佳方法#

C# 在C中处理命令行选项的最佳方法#,c#,.net,command-line-arguments,C#,.net,Command Line Arguments,我非常熟悉命令行参数以及如何使用它们,但我以前从未处理过选项(或标志)。我指的是如下内容: $ sort -f -s -u letters.txt 在上面的bash脚本示例中,我们有3个选项或标志,后跟常规参数。我想在C#应用程序中执行类似的操作。处理命令行选项的最佳方法是什么?参数可以用以下形式给出 $ prog [-options] [args...] 我建议使用这样的库来处理这个问题。它支持开箱即用的可选参数,例如您的示例。我建议您尝试使用;,这是一个“基于回调的C#程序选项解析器”

我非常熟悉命令行参数以及如何使用它们,但我以前从未处理过选项(或标志)。我指的是如下内容:

$ sort -f -s -u letters.txt
在上面的bash脚本示例中,我们有3个选项或标志,后跟常规参数。我想在C#应用程序中执行类似的操作。处理命令行选项的最佳方法是什么?参数可以用以下形式给出

$ prog [-options] [args...]

我建议使用这样的库来处理这个问题。它支持开箱即用的可选参数,例如您的示例。

我建议您尝试使用;,这是一个“基于回调的C#程序选项解析器”

OptionSet目前支持:

Boolean options of the form: -flag, --flag, and /flag. Boolean parameters can have a `+' or `-' appended to explicitly enable or disable the flag (in the same fashion as mcs -debug+). For boolean callbacks, the provided value is non-null for enabled, and null for disabled.
    Value options with a required value (append `=' to the option name) or an optional value (append `:' to the option name). The option value can either be in the current option (--opt=value) or in the following parameter (--opt value). The actual value is provided as the parameter to the callback delegate, unless it's (1) optional and (2) missing, in which case null is passed.
    Bundled parameters which must start with a single `-' and consists of a sequence of (optional) boolean options followed by an (optional) value-using option followed by the options's vlaue. In this manner,
-abc would be a shorthand for -a -b -c, and -cvffile would be shorthand for -c -v -f=file (in the same manner as tar(1)).
    Option processing is disabled when -- is encountered.
以下是来自以下方面的示例:

bool show\u help=false;
列表名称=新列表();
int repeat=1;
var p=新的OptionSet(){
{“n | name=”,“要问候的人的{name}。”,
v=>names.Add(v)},
{“r | repeat=”,
重复问候的次数{次}。\n“+
“这必须是一个整数。”,
(int v)=>repeat=v},
{“v”,“增加调试消息的详细程度”,
v=>{if(v!=null)++verbosity;}},
{“h |帮助”,“显示此消息并退出”,
v=>show_help=v!=null},
};
额外列出;
试一试{
额外=p.解析(args);
}
捕获(OptionException e){
控制台。写(“问候:”);
Console.WriteLine(e.Message);
Console.WriteLine(“有关更多信息,请尝试使用‘问候——帮助’”);
返回;
}

我使用。它很简单,并且(对我来说)可以完成这项工作。

如果您想拥有shell脚本在命令行参数中所具有的全部复杂性,您可能希望获得一个库,该库能够为您解析命令行参数。语言中没有内置解析器;您需要从第三方工具中选择一个。我同意这一点。我过去用过它。它非常强大且易于使用(它们提供的示例代码就是您开始使用的全部内容)。自从我第一次使用它以来,他们甚至添加了
NuGet
包,这是另一个优点。
bool show_help = false;
List<string> names = new List<string> ();
int repeat = 1;

var p = new OptionSet () {
    { "n|name=", "the {NAME} of someone to greet.",
       v => names.Add (v) },
    { "r|repeat=", 
       "the number of {TIMES} to repeat the greeting.\n" + 
          "this must be an integer.",
        (int v) => repeat = v },
    { "v", "increase debug message verbosity",
       v => { if (v != null) ++verbosity; } },
    { "h|help",  "show this message and exit", 
       v => show_help = v != null },
};

List<string> extra;
try {
    extra = p.Parse (args);
}
catch (OptionException e) {
    Console.Write ("greet: ");
    Console.WriteLine (e.Message);
    Console.WriteLine ("Try `greet --help' for more information.");
    return;
}