jcommander icon indicating copy to clipboard operation
jcommander copied to clipboard

Use Enum::toString to print but Enum#valueOf(Enum::name) to match

Open CaryCatZ opened this issue 2 years ago • 1 comments

The formatter uses Enum::toString to print. But parse uses Enum::name to match. So, you might type X but it says it's invalid but also X is an allowed value in the help. This may happen when toString method was overrode.

For example: The enumeration is:

public enum Resolution {
    R_4K, // could not start with a number
    R_1080P,
    R_720P;

    @Override
    public String toString() { // for human-reading
        return this.name().substring(2)
    }
}

The help may be like this:

...
-res
Possible Values: [4K, 1080P, 720P]
...

So, the user might type -res 4K

java -jar program.jar -res 4K

But the program gave this unthinkable report: Invalid value for -res parameter. Allowed values: [4K, 1080P, 720P]

CaryCatZ avatar Feb 18 '23 16:02 CaryCatZ

@CaryCatZ Thank you for reporting this issue. While it is arguable whether it is a good idea to provide a user-friendly name here (see https://docs.oracle.com/javase/8/docs/api/java/lang/Enum.html#toString-- which says that this is intended to be the declared name of the constant), I think this is simply a bug. So I would kindly encourage you to provide a PR containing a fix and a unit test proving its effect.

mkarg avatar Apr 27 '24 09:04 mkarg

Thanks for considering this issue and the PR will be provided soon.

CaryCatZ avatar May 31 '24 15:05 CaryCatZ