使用 CommandLine lib 解析选项时出现错误的默认值异常

Bad default value exception when parsing options with CommandLine lib

我已经使用 CommandLine 库来解析它通过字符串类型、int 类型、bool 类型但它不能通过字节类型,我有一个异常 "Bad default value."

    [Option("m", null, DefaultValue = 0, HelpText = "help")]
    public byte mm { get; set; }

为什么会出现此异常以及如何解决?

文字 0 是每个编译器默认的 Int32。您需要指定一个 byte 来匹配 属性.

的类型

不幸的是 there is no byte literal symbol 在 c# 中,因此您需要显式转换它:

[Option("m", null, DefaultValue = (byte)0, HelpText = "help")]
public byte mm { get; set; }