javacommand-line-interfaceapache-commons-cli

Passing multiple similar options with Commons CLI


I'm using commons Cli, and was wondering if there was a way to create a number of related options without defining them in the constructor. For example, to input a number of files using --if1, --if2, --if3 e.g without defining:

options.addOption("if1".. 
options.addOption("if2"..
options.addOption("if3"..

I know that I can work around it by having a single if flag with comma-delimited values for files. However, I am working to a spec that would like the above formatting so it'd be great to exhaust all options before taking that route.

Any comments would be greatly appreciated,

Thanks, Sam


Solution

  • Multivalues option is used usually for such cases, i.e.

    --if value1 --if value2 --if value3
    

    But you might use loop for their creation

    for (int i=1; i<=3; i++) {
       options.addOption("if" + i);
    }