commandlineparser / commandlineparser/commandline
InvalidCastException when parsing nullable enum
- 主要言語
- 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