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 org.apache.commons.cli2.Argument;
20  import org.apache.commons.cli2.CommandLine;
21  import org.apache.commons.cli2.Group;
22  import org.apache.commons.cli2.Option;
23  import org.apache.commons.cli2.OptionException;
24  import org.apache.commons.cli2.builder.ArgumentBuilder;
25  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
26  import org.apache.commons.cli2.builder.GroupBuilder;
27  import org.apache.commons.cli2.commandline.Parser;
28  
29  import junit.framework.TestCase;
30  
31  /**
32   * http://issues.apache.org/jira/browse/CLI-80
33   */
34  public class BugCLI80Test extends TestCase {
35  
36      public void testBug() {
37          final String optName = "option";
38  
39          Argument arg = new ArgumentBuilder().withName(optName)
40                                              .withMaximum(1)
41                                              .create();
42  
43          Option option = new DefaultOptionBuilder().withArgument(arg)
44                                                    .withDescription("singular option")
45                                                    .withLongName(optName)
46                                                    .withShortName("o")
47                                                    .create();
48  
49          Group group = new GroupBuilder().withOption(option).create();
50  
51          Parser p = new Parser();
52          p.setGroup(group);
53  
54          CommandLine cl = p.parseAndHelp( new String[] { "-o", "yes" } );
55          assertNotNull("Couldn't parse valid commandLine", cl);
56  
57          assertEquals("Couldn't look up value by short name", "yes", cl.getValue("-o") );
58  
59          try {
60              cl = p.parse( new String[] { "-o", "yes", "-o", "jam" } );
61              fail("Parsed invalid commandLine");
62          } catch(OptionException e) {
63            // ok
64          }
65      }
66  
67  }