Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining positional parameters with apache commons cli

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?

like image 392
terrywb Avatar asked Feb 29 '16 21:02

terrywb


1 Answers

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);
}
like image 59
terrywb Avatar answered Oct 21 '22 13:10

terrywb