You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Harsha Vardhan (Jira)" <ji...@apache.org> on 2020/01/28 14:29:00 UTC

[jira] [Comment Edited] (CLI-244) Non-existing option is not reported as a failure when it follows an option that accepts multiple values

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

Harsha Vardhan edited comment on CLI-244 at 1/28/20 2:28 PM:
-------------------------------------------------------------

The DefaultParser implementation can solve this issue by simply changing the order of if-else-if conditions in the handleToken(String) method.

The existing implementation (as of v1.4) is as follows:

 
{code:java}
...
else if (currentOption != null && currentOption.acceptsArg() && isArgument(token))
{
    currentOption.addValueForProcessing(Util.stripLeadingAndTrailingQuotes(token));
}
else if (token.startsWith("--"))
{
    handleLongOption(token);
}
...
{code}
But changing the order to the following seems to work:

 

 
{code:java}
...
else if (token.startsWith("--"))
{
    handleLongOption(token);
}
else if (currentOption != null && currentOption.acceptsArg() && isArgument(token))
{
    currentOption.addValueForProcessing(Util.stripLeadingAndTrailingQuotes(token));
}
...
{code}
 

 


was (Author: harshatech2012):
The DefaultParser implementation can solve this issue by simply changing the order of if-else-if conditions in the handleToken(String) method.

The existing implementation is as follows:

 
{code:java}
...
else if (currentOption != null && currentOption.acceptsArg() && isArgument(token))
{
    currentOption.addValueForProcessing(Util.stripLeadingAndTrailingQuotes(token));
}
else if (token.startsWith("--"))
{
    handleLongOption(token);
}
...
{code}
But changing the order to the following seems to work:

 

 
{code:java}
...
else if (token.startsWith("--"))
{
    handleLongOption(token);
}
else if (currentOption != null && currentOption.acceptsArg() && isArgument(token))
{
    currentOption.addValueForProcessing(Util.stripLeadingAndTrailingQuotes(token));
}
...
{code}
 

 

> Non-existing option is not reported as a failure when it follows an option that accepts multiple values
> -------------------------------------------------------------------------------------------------------
>
>                 Key: CLI-244
>                 URL: https://issues.apache.org/jira/browse/CLI-244
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-1.x
>    Affects Versions: 1.2
>         Environment: Java 7
> Linux/Windows
>            Reporter: Ivan C
>            Priority: Critical
>
> If I define the following options:
> {code:java}
>         Option dest = OptionBuilder
>                 .withArgName("-d")
>                 .withDescription("Destination")
>                 .hasArg()
>                 .create("-d");
>         dest.setRequired(true);
>         Option filenames = OptionBuilder
>                 .withArgName("-f")
>                 .withDescription("Filenames; comma separated")
>                 .hasArgs()
>                 .withValueSeparator(',')
>                 .create("-f");
> {code}
> when I parse the following arguments
> {code}
> -d c:\development\test -f abc.txt -qa hello
> {code}
> rather than getting a ParseException, the code thinks abc.txt, -qa, hello are the values for the -f option.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)