Apache命令行解析器错误? [英] Apache command line parser bug?

查看:65
本文介绍了Apache命令行解析器错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从教程的示例代码中获得了以下代码,并对其进行了一些调整。

I got the code below from a sample code from tutorials point and tweaked it a little bit.

App.java

public static void main(String[] args) throws ParseException {
        CommandTest t = new CommandTest();
        t.start(args);
}

CommandTest.java

public class CommandTest {

    void start(String[] args) throws ParseException {

          //***Definition Stage***
          // create Options object
          Options options = new Options();

          // add option "-a"
          options.addOption(
                    Option.builder("a")
                        .longOpt("add")
                        .desc("add numbers")
                        .hasArg(false)
                        .valueSeparator('=')
                        .required(false)
                        .build()
                  );

          // add option "-m"
          options.addOption("m", false, "");
          options.addOption(
                    Option.builder("m")
                        .longOpt("multiply")
                        .desc("multiply numbers")
                        .hasArg(false)
                        .valueSeparator('=')
                        .required(false)
                        .build()
                  );

          //***Parsing Stage***
          //Create a parser
          CommandLineParser parser = new DefaultParser();

          //parse the options passed as command line arguments
          CommandLine cmd = parser.parse( options, args);

          //***Interrogation Stage***
          //hasOptions checks if option is present or not
          if(cmd.hasOption("a")) { 
             System.out.println("Sum of the numbers: " + getSum(args));
          } else if(cmd.hasOption("m")) {
             System.out.println("Multiplication of the numbers: " + getMultiplication(args));
          }
       }

       public static int getSum(String[] args) {
          int sum = 0;
          for(int i = 1; i < args.length ; i++) {
             sum += Integer.parseInt(args[i]);
          } 
          return sum;
       }

       public static int getMultiplication(String[] args) {
          int multiplication = 1;
          for(int i = 1; i < args.length ; i++) {
             multiplication *= Integer.parseInt(args[i]);
          } 
          return multiplication;
       }
}

现在,我的问题是,当我尝试使用 -multi 参数执行上面的代码,它将仍然被接受吗?我已经设置了仅接收 -m -multiply 的选项。但是,它仍会接受 -multi

Now, my question is that, when i try to execute the above code with a parameter of -multi it will still be accepted? I've already set the options to receive only either -m or -multiply. However, it will still accept -multi

我正在使用commons-cli-1.3.1(即

I am using commons-cli-1.3.1 (im trying to debug a legacy code by the way)

注意:上面的源代码只是一个SAMPLE源代码,不需要应用实际的编码准则(好坏),我只是想知道为什么行为会原样发生。

Note: Above source is a SAMPLE source only, no need to apply actual coding guidelines (good or bad) i just want to know why the behavior happens as it is.

推荐答案

这是找到不匹配选项时的行为( org。 apache.commons.cli.Options:233 ):

This is the behaviour when a non-matching option gets found (org.apache.commons.cli.Options:233):

public List<String> getMatchingOptions(String opt) {
  opt = Util.stripLeadingHyphens(opt);
  List<String> matchingOpts = new ArrayList();
  if (this.longOpts.keySet().contains(opt)) {
    return Collections.singletonList(opt);
  } else {
    Iterator var3 = this.longOpts.keySet().iterator();

    while(var3.hasNext()) {
      String longOpt = (String)var3.next();
      /******************************************************/
      /* longOpt = "multiply"                               */
      /* opt = "multi"                                      */
      /******************************************************/
      if (longOpt.startsWith(opt)) {
        matchingOpts.add(longOpt);
      }
      /******************************************************/
    }

    return matchingOpts;
  }
}

如突出显示的区域所示,如果a short选项不匹配,库会搜索与输入选项部分匹配的第一个long选项。它使用 startsWith ,并且由于 multiply .startsWith( multi) true 它默认为选项-乘以

As you can see in the highlighted block, if a short option isn't matched the library searches for the first long option that partially matches the entered option. It uses startsWith, and since "multiply".startsWith("multi") is true it defaults to option --multiply.

这篇关于Apache命令行解析器错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆