命令行解析器库 - 布尔参数

Command Line Parser Library - Boolean Parameter

我尝试将布尔参数传递给控制台应用程序并使用 Command Line Parser Library 处理该值。

[Option('c', "closeWindow", Required = true, HelpText = "Close the window.")]
public bool CloseWindow { get; set; }

我尝试将参数传递为

-c false
-c False
-c "false"
-...

没有区别,每次尝试我都会得到 "true" 作为值。

任何人都可以告诉我如何传递参数以获得布尔值 false 吗?

为了避免可能的询问,有一个字符串选项被正确传递:

[Option('s', "system", Required = true, HelpText = "Any help text")]
public string System { get; set; }

您不需要添加 TrueFalse。使用 -c 将计算为 True。不使用它将评估为 False。在文档的某处有一个 -v 的示例,用于详细输出。但我现在找不到它。我想 Required=true 不是布尔选项所必需的。

bool? 随心所欲

与 :

[Option('c', "closeWindow", Required = true, HelpText = "Close the window.")]
public bool? CloseWindow { get; set; }

结果将是:

-c false // -> false
-c true  // -> true
-c       // -> error
         // -> error if Required = true, null otherwise

这里有一个可以考虑的解决方法:

更改选项的名称,使 false 始终为默认值。如果您希望“close window”成为默认值,则选项的名称将变为 -w "keepWindowOpen".