Defining positional parameters using 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 can I check pos1 and pos2?

+6
source share
1 answer

How to quickly read the documentation, I did not know that the CommandLine class would provide access to other positional parameters.

After parsing the parameters 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); } 
+3
source

All Articles