commandlineparser / commandlineparser/commandline

FormatCommandLine function doesn't format Enum objects correctly

Open
#710 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.8k
Forks
478
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.