View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.cli2.bug;
18  
19  import junit.framework.TestCase;
20  import org.apache.commons.cli2.CommandLine;
21  import org.apache.commons.cli2.Group;
22  import org.apache.commons.cli2.builder.ArgumentBuilder;
23  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
24  import org.apache.commons.cli2.builder.GroupBuilder;
25  import org.apache.commons.cli2.commandline.Parser;
26  import org.apache.commons.cli2.option.DefaultOption;
27  
28  import java.util.List;
29  
30  /**
31   * ArgumentBuilder.withMaximum causes parse errors: Unexpeced <value> while processing options
32   *
33   * @author David Biesack
34   * @author brianegge
35   */
36  public class BugCLI145Test extends TestCase {
37      public void testWithMaximum() {
38          final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
39          final ArgumentBuilder abuilder = new ArgumentBuilder();
40          final GroupBuilder gbuilder = new GroupBuilder();
41          DefaultOption aOption = obuilder//
42                  .withShortName("a")
43                  .withLongName("a")
44                  .withArgument(abuilder
45                          .withName("a")
46                          .withDefault("10")
47                          .create())
48                  .create();
49          DefaultOption bOption = obuilder
50                  .withShortName("b")
51                  .withLongName("b")
52                  .withArgument(abuilder
53                          .withName("b")
54                          .withMinimum(2)
55                          .withMaximum(4)
56                          .withDefault("100")
57                          .withDefault("1000")
58                          .withDefault("10000")
59                          .withDefault("1000000")
60                          .create())
61                  .create();
62          Group options = gbuilder
63                  .withName("options")
64                  .withOption(aOption)
65                  .withOption(bOption)
66                  .create();
67          Parser parser = new Parser();
68          parser.setHelpTrigger("--help");
69          parser.setGroup(options);
70          CommandLine cl = parser.parseAndHelp("-a 0 -b 1 2 3 4".split(" "));
71          assertNotNull(cl);
72          int a = Integer.parseInt(cl.getValue(aOption).toString());
73          List b = cl.getValues(bOption);
74          assertEquals(0, a);
75          assertEquals(4, b.size());
76      }
77  
78      public void testWithMaximumUsingDefaultValues() {
79          final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
80          final ArgumentBuilder abuilder = new ArgumentBuilder();
81          final GroupBuilder gbuilder = new GroupBuilder();
82          DefaultOption aOption = obuilder//
83                  .withShortName("a")
84                  .withLongName("a")
85                  .withArgument(abuilder
86                          .withName("a")
87                          .withDefault("10")
88                          .create())
89                  .create();
90          DefaultOption bOption = obuilder
91                  .withShortName("b")
92                  .withLongName("b")
93                  .withArgument(abuilder
94                          .withName("b")
95                          .withMinimum(2)
96                          .withMaximum(4)
97                          .withDefault("100")
98                          .withDefault("1000")
99                          .withDefault("10000")
100                         .create())
101                 .create();
102         Group options = gbuilder
103                 .withName("options")
104                 .withOption(aOption)
105                 .withOption(bOption)
106                 .create();
107         Parser parser = new Parser();
108         parser.setHelpTrigger("--help");
109         parser.setGroup(options);
110         CommandLine cl = parser.parseAndHelp("-a -b".split(" "));
111         assertNotNull(cl);
112         int a = Integer.parseInt(cl.getValue(aOption).toString());
113         List b = cl.getValues(bOption);
114         assertEquals(10, a);
115         assertEquals(3, b.size());
116         assertEquals("10000", b.get(2));
117     }
118 }