commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Christian Schulte (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (CLI-255) DefaultParser, option with long name and single dash, unlimited arguments
Date Mon, 18 Apr 2016 04:41:25 GMT

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

Christian Schulte commented on CLI-255:
---------------------------------------

I am running into a similar issue. Options after an option with {{Option.UNLIMITED_VALUES}}
are not handled correctly. The cause for this seems to be in method {{isShortOption(String
token)}} of class {{DefaultParser}}.

{code}
    private boolean isShortOption(String token)
    {
        // short options (-S, -SV, -S=V, -SV1=V2, -S1S2)
        return token.startsWith("-") && token.length() >= 2 && options.hasShortOption(token.substring(1,
2));
    }
{code}

The {{token.substring(1, 2)}} is incorrect. It should read {{token.substring(1)}}. A short
option may contain more than just one character. Like here:

{code}
Option.builder( "cp" ).longOpt( "classpath" ).hasArgs().optionalArg( false ).
            valueSeparator( File.pathSeparatorChar ).
            desc( "Classpath elements separated by " + File.pathSeparator ).
            argName( "classpath elements" ).build();
{code}


> DefaultParser, option with long name and single dash, unlimited arguments
> -------------------------------------------------------------------------
>
>                 Key: CLI-255
>                 URL: https://issues.apache.org/jira/browse/CLI-255
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: Parser
>    Affects Versions: 1.3, 1.3.1
>            Reporter: Alexander Prishchepov
>            Priority: Minor
>
> If I have options with long name and single dash, DefaultParser does not recognize them
after a list of unlimited arguments.
> Here is the test case:
> {code:java}
>     public void testUnlimitedArgs() throws Exception
>     {
>         String[] args = new String[] {"-unlimitedOne", "one", "two", "-unlimitedTwo",
"alpha"};
>         Options options = new Options();
>         options.addOption(Option.builder("unlimitedOne").hasArgs().build());
>         options.addOption(Option.builder("unlimitedTwo").hasArgs().build());
>         CommandLine cl = parser.parse(options, args);
>         assertTrue("Confirm -unlimitedOne is set", cl.hasOption("unlimitedOne"));
>         assertEquals("number of arg for -unlimitedOne", 2, cl.getOptionValues("unlimitedOne").length);
>         assertTrue("Confirm -unlimitedTwo is set", cl.hasOption("unlimitedTwo"));
>         assertEquals("number of arg for -unlimitedTwo", 1, cl.getOptionValues("unlimitedTwo").length);
>     }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message