commandlineparser / commandlineparser/commandline

FormatCommandLine function doesn't format Enum objects correctly

Offen
#710 1 Kommentar 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C#
Sterne
4.8k
Forks
478
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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
}
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.