commandlineparser / commandlineparser/commandline

InvalidCastException when parsing nullable enum

未關閉
#719 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
C#
星號
4.8k
分支
478
PR 合併指標
30 天內沒有已合併 PR

描述

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

貢獻指南

這個儲存庫沒有索引到貢獻指南

研究方向

首先,使用 nullable ECommand? 屬性和 `-c DoStuff` 引數,透過 parser.ParseArguments() 重現該故障。追蹤 nullable enum 的轉換路徑;當命令能夠在沒有 InvalidCastException 的情況下完成剖析並指派預期的 enum 值時,即表示完成。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
csharp
領域
cli
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。