commandlineparser / commandlineparser/commandline

InvalidCastException when parsing nullable enum

Đang mở
#719 1 bình luận 0 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
C#
Star
4.8k
Fork
478
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

Hi there,

my code looks like this:

```
// Code in Main
using (var parser = Parser.Default)
{
var parserResult = parser.ParseArguments(args);
}

// CommandlineOptions Class
public class CommandlineOptions
{
[Option('c', "command", Required = true, HelpText = "The Command to use")]
public ECommand? Command { get; set; }
}

// Enum ECommand
public enum ECommand
{
DoStuff = 1;
DoSomethingElse = 2;
}
```

Notice that my Command-Option is a nullable enum.

When i run my programm with the following command
`myProgramm.exe -c DoStuff`
I get an `InvalidCastException`, saying that it can't convert `System.String` in `ECommand`.
This exception is thrown by` parser.ParseArguments()`.

Am I missing something or is this a bug?
How can i fix that?

I know that the exception doesn't occure if the enum isn't nullable. But I need the nullable enum at that point!

Hope you can help me.
Greetings Tom

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.