Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

command line parsing using apache commons cli

M tryng to use apache commons cli , My use case is variable number of arguments with some options.

Say

 -p str1 str2;

It can be

 -p str1 str2 str3 .. strn

Another is

 -m str1
 -h

with

 cmdline.getOptionValues("p");

It fetches only last string.How can I fetch all the values of an particular option?

Edit:

if(cmdline.hasOption("p")){
 String[] argsList = cmdline.getOptionValues(p);
  String strLine = Arrays.toString(argsList);
  argsList = strLine.split(",");
 }

M i doing it right? will string consist of exactly data I want or smthng unexpected white spaces r anythng else?

like image 320
user3505394 Avatar asked May 07 '26 19:05

user3505394


1 Answers

Use hasArgs() with a value separator set to a comma, so the option becomes

-p str1,str2,str3,...,strn

This is how multi-valued options are handled in CLI

like image 89
Jim Garrison Avatar answered May 09 '26 10:05

Jim Garrison