commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Oxenburgh (JIRA)" <j...@apache.org>
Subject [jira] Commented: (CLI-158) deafult arguments only works if no arguments are submitted
Date Wed, 04 Jun 2008 19:46:47 GMT

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

Andrew Oxenburgh commented on CLI-158:
--------------------------------------

I couldn't find any written use cases, so I tried to intuit it from
its actual behaviour, and I agree you have a point, it could probably
be either way.

However, if the minimum number of arguments is anything other than
zero there is no point in setting default arguments because there is
no way they can be used. If you specify a minimum of 2 arguments with
3 defaults, but only submit 1 argument, it will throw an
OptionException at parse time. If you submit 2 arguments the defaults
are ignored. [Please see the code below]

On another point, there does however seem to be a lack of use case
tests. These tend to be very useful as sample code. I have written
some sample code and submitted it on
http://wiki.apache.org/commons/CLI2, but maybe we should put them in
the code base.

Anyone else have any opinions on this?

Regards





==========================
    public void testSingleOptionMaximumNumberOfArgument() throws Exception {
        String[] args = new String[]{"-b", "1", "2"};
        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
        final GroupBuilder gbuilder = new GroupBuilder();

        DefaultOption bOption = obuilder.withShortName("b")
                .withLongName("b")
                .withArgument(abuilder.withName("b")
                .withMinimum(2)
                .withMaximum(4)
                .withDefault("100")
                .withDefault("1000")
                .withDefault("10000")
                .create())
                .create();

        Group options = gbuilder
                .withName("options")
                .withOption(bOption)
                .create();

        Parser parser = new Parser();
        parser.setHelpTrigger("--help");
        parser.setGroup(options);
        CommandLine cl = parser.parse(args); // <<<< OptionException
thrown here.
        CommandLine cmd = cl;
        assertNotNull(cmd);
        List b = cmd.getValues("-b");
        assertEquals("[1, 2]", b + "");
    }

==========================









> deafult arguments only works if no arguments are submitted
> ----------------------------------------------------------
>
>                 Key: CLI-158
>                 URL: https://issues.apache.org/jira/browse/CLI-158
>             Project: Commons CLI
>          Issue Type: Bug
>          Components: CLI-2.x
>    Affects Versions: 2.0
>            Reporter: Andrew Oxenburgh
>             Fix For: 2.0
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> When using multple arguments and defaults, the behaviour is counter-intuitive and will
only pick up a default if no args are passed in.
> For instance in the code below I have set up so 0, 1, or 2 args may bve accepted, with
defaults 100 and 1000.
> I expect it to behave as follows.
> 1. for 2 args, 1 and 2 the values should be 1 and 2. This works as expected.
> 2. for 0 args passed in the values should be 100 and 1000, picking up both of the defaults.
This works as expected
> 3. for 1 arg passed in the values should be 1 and 1000, so the second argument picks
up the second default value. The valuse become just 1, which is not as expected..
> Currently, in the second case will only return 1 and ignore the defaults.
>     public void testSingleOptionSingleArgument() throws Exception {
>         String defaulValue1 = "100";
>         String defaultValue2 = "1000";
>         final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
>         final ArgumentBuilder abuilder = new ArgumentBuilder();
>         final GroupBuilder gbuilder = new GroupBuilder();
>         DefaultOption bOption = obuilder.withShortName("b")
>                 .withLongName("b")
>                 .withArgument(abuilder.withName("b")
>                         .withMinimum(0)
>                         .withMaximum(2)
>                         .withDefault(defaulValue1)
>                         .withDefault(defaultValue2)
>                         .create())
>                 .create();
>         Group options = gbuilder
>                 .withName("options")
>                 .withOption(bOption)
>                 .create();
>         Parser parser = new Parser();
>         parser.setHelpTrigger("--help");
>         parser.setGroup(options);
>         String enteredValue1 = "1";
>         String[] args = new String[]{"-b", enteredValue1};
>         CommandLine cl = parser.parse(args);
>         CommandLine cmd = cl;
>         assertNotNull(cmd);
>         List b = cmd.getValues("-b");
>         assertEquals("[" + enteredValue1 + "]", b + "");
>     }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message