[ 
https://issues.apache.org/jira/browse/CLI-288?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16569135#comment-16569135
 ] 

NIRAJ RATHI commented on CLI-288:
---------------------------------

The problem is the way JVM is converting input comand line argument to String 
array.

 

 
{code:java}
public static void main(String[] args) throws Exception {
 for ( String arg: args )
 {
 System.out.println(arg);
 }
}
{code}
 

 

For command line cmd="less -opt2"

String array to main is [--cmd='less, -opt2'] instead of 3 Strings only 2 are 
here.

 

For command line cmd "less -opt2"

String array to main is [--cmd, 'less, -opt2'] here we have 3 Strings.

Option parsing is based on String array prepared by JVM.

 

In case of second one DefaultParser treat -opt2 as unknown option.

 

> Inconsistent parsing of unlimited option values
> -----------------------------------------------
>
>                 Key: CLI-288
>                 URL: https://issues.apache.org/jira/browse/CLI-288
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: Parser
>    Affects Versions: 1.4
>            Reporter: Adam Cardenas
>            Priority: Major
>
> with the following configuration...
> {code:java}
> DefaultParser parser = new DefaultParser();
>  Options opts = new Options();
> Option pager = Option.builder(null)
>  .longOpt("cmd")
>  .hasArg()
>  .numberOfArgs(Option.UNLIMITED_VALUES)
>  .build();
> opts.addOption(pager);
> {code}
>  
> This works as long as the value of *cmd* is a single value.
> {code}
> $ myApp --cmd='less'
> {code}
> The following does not work, as soon as we add options to less
> {code}
> $ myApp --cmd='less -XFR'
> Exception in thread "main" com.cevaris.ag4j.cli.AppArgsException: 
> org.apache.commons.cli.UnrecognizedOptionException: Unrecognized option: -XFR
>         at com.cevaris.ag4j.cli.ApacheAppArgs.parse(ApacheAppArgs.java:33)
>         at com.cevaris.ag4j.Main.main(Main.java:61)
> Caused by: org.apache.commons.cli.UnrecognizedOptionException: Unrecognized 
> option: -XFR
>         at 
> org.apache.commons.cli.DefaultParser.handleUnknownToken(DefaultParser.java:360)
>         at 
> org.apache.commons.cli.DefaultParser.handleConcatenatedOptions(DefaultParser.java:702)
>         at 
> org.apache.commons.cli.DefaultParser.handleShortAndLongOption(DefaultParser.java:533)
>         at 
> org.apache.commons.cli.DefaultParser.handleToken(DefaultParser.java:243)
>         at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:120)
>         at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:76)
>         at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:60)
>         at com.cevaris.ag4j.cli.ApacheAppArgs.parse(ApacheAppArgs.java:31)'
> {code}
> The following works, as long as we use a space to separate option & value, 
> rather than using an *=*
> {code}
> $ myApp --cmd 'less -XFR'  # works
> $ myApp --cmd='less -XFR'  # fails
> {code}
> Attempted to use *.valueSeparator()* with no luck.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to