java - Apache Commons CLI 1.3.1: Option coming after another option with multiple arguments is consumed as ARGUMENT -


i'm using apache commons cli 1.3.1 process options , of options can take 1 unlimited number arguments. trivia example 2 options this

usage: myprogram -optionx <arg1> <arg2> <arg3> < ... > [-optiony] -optionx <arg1> <arg2> <arg3> < ... >   optionx takes 1 unlimited                                         number of arguments. -optiony                                optiony optional 

what found second option optiony recognized argument of optionx instead of being recognized option itself. means if type myprogram -optionx arg1 arg2 -optiony in command line, 3 arguments (arg1, arg2, , -optiony) associated optionx.

here code can use reproduce problem.

import org.apache.commons.cli.*;  public class testcli {  public static void main(string[] args) {      option optobj1 = option.builder("optionx")                             .argname("arg1> <arg2> <arg3> < ... ")                             .desc("optionx takes 1 unlimited number of arguments.")                             .required()                             .hasargs()                             .build();      option optobj2 = new option("optiony", "optiony optional");      options optslist = new options();     optslist.addoption(optobj1);     optslist.addoption(optobj2);      commandlineparser commandlineparser = new defaultparser();      try {          commandline cmd = commandlineparser.parse(optslist, new string[]{"-optionx", "arg1", "arg2", "-optiony"});          system.out.println("--------------------------");         system.out.println("argument list of optionx: ");         (string argname : cmd.getoptionvalues(optobj1.getopt())) {             system.out.println("arg: " + argname);         }          system.out.println("--------------------------");         system.out.println("value of optiony: " + cmd.hasoption(optobj2.getopt()));      } catch (parseexception e) {         system.out.println("unexcepted option: " + e.getmessage());     }  } } 

this output you'll see.

-------------------------- argument list of optionx:  arg: arg1 arg: arg2 arg: -optiony -------------------------- value of optiony: false 

do miss here?

any suggestion appreciated.

the problem putting long name in short name of option.

when use option optobj1 = option.builder("optionx").... or new option("optiony", "optiony optional"), you're setting short name of option, supposed 1 character long.

that work until have multi arguments option. in case, parser cannot find "o" (the first letter of option) in short option list , don't have long name set, parser determines -optiony argument -optionx.

to solve problem, set long option name of option , should work alright.

example

option.builder("x").longopt("optionx")....  option optobj2 = new option("y", "optiony", hasargs, "optiony optional"); 

Comments

  1. It is also possible to make money online with Magento and a good use of this software is the creation of a Magento eCommerce website, which is usually completed within six weeks, giving you the opportunity to start making money online. Get more interesting details about magento check this site.

    ReplyDelete

Post a Comment

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -