
Question:
How can I make an option accept only some specified values like in the following example:
$ java -jar Mumu.jar -a foo OK $ java -jar Mumu.jar -a bar OK $ java -jar Mumu.jar -a foobar foobar is not a valid value for -a
Solution:1
Since commons-cli doesn't support that directly, the simplest solution is probably to check the value of an option when you get it.
Solution:2
The other way can be to extend the Option class. At work we have made that:
public static class ChoiceOption extends Option { private final String[] choices; public ChoiceOption( final String opt, final String longOpt, final boolean hasArg, final String description, final String... choices) throws IllegalArgumentException { super(opt, longOpt, hasArg, description + ' ' + Arrays.toString(choices)); this.choices = choices; } public String getChoiceValue() throws RuntimeException { final String value = super.getValue(); if (value == null) { return value; } if (ArrayUtils.contains(choices, value)) { return value; } throw new RuntimeException( value " + describe(this) + " should be one of " + Arrays.toString(choices)); } @Override public boolean equals(final Object o) { if (this == o) { return true; } else if (o == null || getClass() != o.getClass()) { return false; } return new EqualsBuilder().appendSuper(super.equals(o)) .append(choices, ((ChoiceOption) o).choices) .isEquals(); } @Override public int hashCode() { return new ashCodeBuilder().appendSuper(super.hashCode()).append(choices).toHashCode(); } }
Solution:3
I've wanted this kind of behaviour before, and never came across a way to do this with an already provided method. That's not to say it doesn't exist. A kind of lame way, is to add the code yourself such as:
private void checkSuitableValue(CommandLine line) { if(line.hasOption("a")) { String value = line.getOptionValue("a"); if("foo".equals(value)) { println("OK"); } else if("bar".equals(value)) { println("OK"); } else { println(value + "is not a valid value for -a"); System.exit(1); } } }
Obviously there would be nicer ways to do this than the long if/else, possibly with an enum
, but that should be all you'd need. Also I've not compiled this, but I reckon it should work.
This example also does not make the "-a" switch mandatory, since that wasn't specified in the question.
Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
EmoticonEmoticon