commandlineparser / commandlineparser/commandline

FormatCommandLine function doesn't format Enum objects correctly

Aperta
#710 1 commento 2 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
C#
Stelle
4.8k
Fork
478
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

When passing in an object that has Enum parameters into the unparsing function FormatCommandLine, if the specified Enum has a value of 0, it does not successfully unparse into a string. I am currently using 2.8.0 of the CommandLineParser

For example:
```
public enum SomeEnumType
{
AValue,
AnotherValue
}
```

And you have an object:
```
[Verb("some-verb")]
public class SomeObject
{
[Option("some-attribute", Required = true)]
public SomeEnumType SomeAttribute { get; set; }
}
```

And you attempt to unparse it as such:
```
public class Program
{
static void Main(string[] args)
{
var stringArgs = Parser.Default.FormatCommandLine(new SomeObject
{
SomeAttribute = SomeEnumType.AValue
});
Console.WriteLine(stringArgs); // output: some-verb
var stringArgs2 = Parser.Default.FormatCommandLine(new SomeObject
{
SomeAttribute = SomeEnumType.AnotherValue
});
Console.WriteLine(stringArgs2);// output: some-verb --some-attribute AnotherValue
}
}
```

The workaround is to either add an Unspecified at the very beginning, or set AValue = 1 and Another Value = 2:
```
public enum SomeEnumType
{
AValue = 1,
AnotherValue = 2
}
```
or
```
public enum SomeEnumType
{
Unspecified,
AValue,
AnotherValue
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.