Java common -cli,具有可能值列表的选项

时间:2022-11-20 00:15:19

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 个解决方案

#1


5  

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

由于common -cli不直接支持这一点,因此最简单的解决方案可能是在获取选项时检查选项的值。

#2


6  

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.

显然,有比长if/else更好的方法,可能还有enum,但这应该是您所需要的全部。我也没有编译过这个,但是我认为它应该有用。

This example also does not make the "-a" switch mandatory, since that wasn't specified in the question.

这个示例也不强制使用“-a”开关,因为问题中没有指定这一点。

#3


6  

The other way can be to extend the Option class. At work we have made that:

另一种方法是扩展Option类。在工作中,我们做到了:

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

#1


5  

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

由于common -cli不直接支持这一点,因此最简单的解决方案可能是在获取选项时检查选项的值。

#2


6  

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.

显然,有比长if/else更好的方法,可能还有enum,但这应该是您所需要的全部。我也没有编译过这个,但是我认为它应该有用。

This example also does not make the "-a" switch mandatory, since that wasn't specified in the question.

这个示例也不强制使用“-a”开关,因为问题中没有指定这一点。

#3


6  

The other way can be to extend the Option class. At work we have made that:

另一种方法是扩展Option类。在工作中,我们做到了:

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