【问题标题】:Java commons-cli, options with list of possible valuesJava commons-cli,带有可能值列表的选项
【发布时间】:2010-12-21 02:58:34
【问题描述】:

如何让一个选项只接受一些指定的值,如下例所示:

$ 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

【问题讨论】:

    标签: java command-line command-line-interface apache-commons apache-commons-cli


    【解决方案1】:

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

    【讨论】:

    • 当有人插入错误值时,使用此类不会导致解析器抛出异常。 CommandLineParser.parse 不要调用 getChoiceValue,因为它不是覆盖。
    • 要部分绕过该问题,您可以在 ChoiceOption 中覆盖以下内容: @Override public List getValuesList() { List ret = new ArrayList(); ret.add(getChoiceValue());返回 ret;解析后你可以调用getOptionValue,当值无效时会引发异常
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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,但这应该是你所需要的。我也没有编译这个,但我认为它应该可以工作。

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

      【讨论】:

        猜你喜欢
        • 2013-06-15
        • 1970-01-01
        • 2016-12-18
        • 2011-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-10
        • 2017-10-09
        相关资源
        最近更新 更多