Search code examples
javaapache-commons-cli

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?


Solution

  • 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