Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use toString() to match enum constants #830

Merged
merged 1 commit into from
Nov 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -11987,19 +11987,17 @@ private ITypeConverter<?> getTypeConverter(final Class<?> type, ArgSpec argSpec,
return new ITypeConverter<Object>() {
@SuppressWarnings("unchecked")
public Object convert(String value) throws Exception {
String sensitivity = "case-sensitive";
if (commandSpec.parser().caseInsensitiveEnumValuesAllowed()) {
String upper = value.toUpperCase();
try { return Enum.valueOf((Class<Enum>) type, value); }
catch (IllegalArgumentException ex) {
boolean insensitive = commandSpec.parser().caseInsensitiveEnumValuesAllowed();
for (Object enumConstant : type.getEnumConstants()) {
if (upper.equals(String.valueOf(enumConstant).toUpperCase())) { return enumConstant; }
String thisName = enumConstant.toString();
if( value.equals(thisName) || insensitive && value.equalsIgnoreCase(thisName) ) { return enumConstant; }
Comment on lines +11994 to +11995
Copy link
Owner

@remkop remkop Oct 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a bugfix to also compare enumConstant.name() case-insensitively:

String str = enumConstant.toString();
String name = enumConstant.name();
if (value.equals(str) || value.equals(name) || insensitive && (value.equalsIgnoreCase(str) || value.equalsIgnoreCase(name))) {
    return enumConstant;
}

Also, please keep the whitespace conventions the same as the original. :-) So, if (xxx) instead of if( xxx ).

}
sensitivity = "case-insensitive";
}
try { return Enum.valueOf((Class<Enum>) type, value); }
catch (Exception ex) {
String sensitivity = insensitive ? "case-insensitive" : "case-sensitive";
Enum<?>[] constants = ((Class<Enum<?>>) type).getEnumConstants();
String[] names = new String[constants.length];
for (int i = 0; i < names.length; i++) { names[i] = constants[i].name(); }
for (int i = 0; i < names.length; i++) { names[i] = constants[i].toString(); }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When no match is found, we want to report the enum constant names, not their toString() values: see #592

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, using constants[i].toString() is better: I realize now that my fix for #592 was wrong. The actual cause for the error reported in #592 was that the parser only looks at the toString() value of the constants if caseInsensitiveEnumValuesAllowed is true. If we fix that, it makes sense to print constants[i].toString() when no match is found.

throw new TypeConversionException(
String.format("expected one of %s (%s) but was '%s'", Arrays.asList(names), sensitivity, value)); }
}
Expand Down