使用 apache commons cli 定义位置参数

Defining positional parameters with apache commons cli

我想定义一个包含命名参数和位置参数的 Apache Commons CLI 解析器。

program [-a optA] [-b optB] [-f] pos1 pos2

如何验证 pos1 和 pos2?

快速阅读文档,我不知道 CommandLine class 会提供对其余位置参数的访问。

解析命令行传递的选项后,剩余的参数在CommandLine.getArgs()方法中可用。

public static void main(String[] args) {
      DefaultParser clParse = new DefaultParser();
      Options opts = new Options();
      opts.addOption("a", true, "Option A");
      opts.addOption("b", true, "Option B");
      opts.addOption("f", false, "Flag F");

      CommandLine cmdLine = clParse.parse(opts, args); 
      System.out.println(cmdLine.getArgs().length);
}