Java commons-cli,带有可能值列表的选项

发布于 2024-08-12 22:58:53 字数 181 浏览 7 评论 0原文

如何使选项仅接受某些指定值,如下例所示:

$ 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈下烟灰 2024-08-19 22:58:54

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

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

演多会厌 2024-08-19 22:58:54

另一种方法是扩展 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();
      }
  }

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();
      }
  }
半枫 2024-08-19 22:58:54

我以前就想要这种行为,但从未遇到过使用已经提供的方法来做到这一点的方法。这并不是说它不存在。一种蹩脚的方法是自己添加代码,例如:

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

显然,有比长 if/else 更好的方法来做到这一点,可能使用 enum,但这应该是你的全部d 需要。我还没有编译这个,但我认为它应该可以工作。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文