jcommander
jcommander copied to clipboard
why specified convert not effect ?
I create a JobPriorityConverter class, it's inherit from EnumConverter.
public class JobPriorityConverter extends EnumConverter<JobPriority> {
public JobPriorityConverter(final String str, final Class<JobPriority> clazz) {
super(str, clazz);
}
@Override
public JobPriority convert(String value) {
JobPriority jobPriority = super.convert(value);
if (jobPriority == JobPriority.VERY_HIGH || jobPriority == JobPriority.UNKNOWN_JOB_PRIORITY) {
return JobPriority.HIGH;
} else {
return jobPriority;
}
}
}
The JobPriority
is generated by proto, it look like below
public enum JobPriority implements com.google.protobuf.ProtocolMessageEnum {
UNKNOWN_JOB_PRIORITY(0),
VERY_HIGH(1),
HIGH(2),
NORMAL(3),
UNRECOGNIZED(-1);
}
Now, I have a parameter.
@Parameter(names = {"--priority"}, converter = JobPriorityConverter.class)
protected JobPriority priority = JobPriority.NORMAL;
But I run the program, for example: java -classpath xxx ... className --priority VERY_HIGH, then it should be HIGH, actually it's still VERY_HIGH.
I pass the invalid priority, it output.
Exception in thread "main" com.beust.jcommander.ParameterException: Invalid value for -pr parameter. Allowed values:[UNKNOWN_JOB_PRIORITY, VERY_HIGH, HIGH, NORMAL, UNRECOGNIZED]
at com.beust.jcommander.converters.EnumConverter.convert(EnumConverter.java:38)
at com.beust.jcommander.converters.EnumConverter.convert(EnumConverter.java:13)
at com.beust.jcommander.JCommander.convertValue(JCommander.java:1472)
at com.beust.jcommander.ParameterDescription.addValue(ParameterDescription.java:238)
at com.beust.jcommander.JCommander.processFixedArity(JCommander.java:895)
at com.beust.jcommander.JCommander.processFixedArity(JCommander.java:870)
at com.beust.jcommander.JCommander.parseValues(JCommander.java:721)
at com.beust.jcommander.JCommander.parse(JCommander.java:340)
at com.beust.jcommander.JCommander.parse(JCommander.java:319)
If your posting is intended to be a question, then please post it on https://groups.google.com/g/jcommander.
If your posting is intended as a bug report, then please post a unit test here to proof your claim.