You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "ilia (Jira)" <ji...@apache.org> on 2021/06/05 21:26:00 UTC

[jira] [Comment Edited] (CLI-309) MissingArgumentException when DefaultParser processes an option value which starts with hyphen/dash.

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

ilia edited comment on CLI-309 at 6/5/21, 9:25 PM:
---------------------------------------------------

I did some additional investigation and found that the issue is caused by the fix for CLI-265. Since we already have short option "*p",* the parser incorrectly assumes that value "*-password1"* is actually concatenated short options because it's starts with *"p".* This causes *MissingArgumentException* because *-sec-attr* expects a value.

_org.apache.commons.cli.DefaultParser_
{code:java}
private boolean isShortOption(final String token)
    {
...
        // check for several concatenated short options
        return !optName.isEmpty() && options.hasShortOption(String.valueOf(optName.charAt(0)));
    } {code}
 

We can check that all characters are valid short options -S1S2S3 [1] but we also have the case -S1S2V which cannot be covered. 

[1]
{code:java}
// check for several concatenated short options
        if (optName.length() > 1) {
            boolean isConcOptions = false;
            for (int i = 0; i < optName.length(); i++) {
                isConcOptions = options.hasShortOption(String.valueOf(optName.charAt(i)));
            }
            return isConcOptions;
        }{code}


was (Author: ivassile):
I did some additional investigation and found that the issue is caused by the fix for CLI-265. Since we already have short option "*p",* the parser incorrectly assumes that value "*-password1"* is actually concatenated short options because it's starts with ** "*p".* This causes *MissingArgumentException* because *-sec-attr* expects a value.

_org.apache.commons.cli.DefaultParser_
{code:java}
private boolean isShortOption(final String token)
    {
...
        // check for several concatenated short options
        return !optName.isEmpty() && options.hasShortOption(String.valueOf(optName.charAt(0)));
    } {code}
 

We can check that all characters are valid short options -S1S2S3 [1] but we also have the case -S1S2V which cannot be covered. 

[1]
{code:java}
// check for several concatenated short options
        if (optName.length() > 1) {
            boolean isConcOptions = false;
            for (int i = 0; i < optName.length(); i++) {
                isConcOptions = options.hasShortOption(String.valueOf(optName.charAt(i)));
            }
            return isConcOptions;
        }{code}

> MissingArgumentException when DefaultParser processes an option value which starts with hyphen/dash.
> ----------------------------------------------------------------------------------------------------
>
>                 Key: CLI-309
>                 URL: https://issues.apache.org/jira/browse/CLI-309
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: Parser
>    Affects Versions: 1.4
>            Reporter: ilia
>            Priority: Major
>
>  
> When processing option value which starts with hyphen/dash like *-sec-attr -password1* , exception [1] occurs. If the current token start with dash, it will be processed as an option and therefore this will not be set as a value to the previous option, which will cause [1]. The only way to set such value is to use equals sign like *-sec-attr=-password1*. In this case the whole token is processed as option=value pair and the correct value will be set.
>  
> [1] 
> {code:java}
> org.apache.commons.cli.MissingArgumentException: Missing argument for option: xorg.apache.commons.cli.MissingArgumentException: Missing argument for option: x at org.apache.commons.cli.DefaultParser.checkRequiredArgs(DefaultParser.java:259) at org.apache.commons.cli.DefaultParser.handleOption(DefaultParser.java:669) at org.apache.commons.cli.DefaultParser.handleConcatenatedOptions(DefaultParser.java:773) at org.apache.commons.cli.DefaultParser.handleShortAndLongOption(DefaultParser.java:590) at org.apache.commons.cli.DefaultParser.handleToken(DefaultParser.java:291) at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:168) at org.apache.commons.cli.DefaultParser.parse(DefaultParser.java:129){code}



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