13

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

3 Answers 3

8

Since commons-cli doesn't support that directly, the simplest solution is probably to check the value of an option when you get it.

Sign up to request clarification or add additional context in comments.

Comments

8

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();
      }
  }

2 Comments

using this class doesn't cause the parser to throw an exception when someone insert a wrong value. CommandLineParser.parse don't call the getChoiceValue because it's not an override.
to partially bypass the problem, you could override in ChoiceOption of the following: @Override public List<String> getValuesList() { List<String> ret = new ArrayList<>(); ret.add(getChoiceValue()); return ret; } after parse you can call getOptionValue and when the value is invalid it raises the exception
7

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.