commandlineparser / commandlineparser/commandline
InvalidCastException when parsing nullable enum
- Langage dominant
- C#
- Étoiles
- 4.8k
- Forks
- 478
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
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
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Commencez par reproduire l’échec via parser.ParseArguments() avec la propriété nullable ECommand? et l’argument `-c DoStuff`. Suivez le chemin de conversion de nullable enum ; le travail est terminé lorsque la commande est analysée sans InvalidCastException et affecte la valeur enum attendue.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- csharp
- Domaine
- cli
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100