commandlineparser / commandlineparser/commandline

InvalidCastException when parsing nullable enum

Aberta
#719 1 comentário 0 reações 0 responsáveis Ver no GitHub
Linguagem predominante
C#
Estrelas
4.8k
Forks
478
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

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

Guia de contribuição

Nenhum guia de contribuição indexado para este repositório

Direção de pesquisa

Start by reproducing the failure through parser.ParseArguments() with the nullable ECommand? property and the `-c DoStuff` argument. Trace the nullable-enum conversion path; done means the command parses without InvalidCastException and assigns the expected enum value.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
csharp
Domínio
cli
Tipo de issue
Bug
Dificuldade
3/5
Tempo estimado
1-2 dias
Status de atividade
Estagnada
Clareza
Razoavelmente clara
Facilidade para iniciantes
35/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.