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

why specified convert not effect ? #545

Open
Wuv1Up opened this issue Oct 18, 2022 · 1 comment
Open

why specified convert not effect ? #545

Wuv1Up opened this issue Oct 18, 2022 · 1 comment

Comments

@Wuv1Up
Copy link

Wuv1Up commented Oct 18, 2022

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)
@mkarg
Copy link
Collaborator

mkarg commented Apr 28, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants