Defining positional parameters with apache commons cli

早过忘川 提交于 2019-12-10 12:47:34

问题


I would like to define an Apache Commons CLI parser that includes named arguments and positional arguments.

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

How do I validate pos1 and pos2?


回答1:


One a quick read of the documentation, I was not aware the the CommandLine class would provide access to the remaining positional parameters.

After parsing the Options passed on the command line, the remaining arguments are available in the CommandLine.getArgs() method.

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


来源:https://stackoverflow.com/questions/35710212/defining-positional-parameters-with-apache-commons-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!