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  
21  import org.apache.commons.cli2.Argument;
22  import org.apache.commons.cli2.CommandLine;
23  import org.apache.commons.cli2.Group;
24  import org.apache.commons.cli2.Option;
25  import org.apache.commons.cli2.OptionException;
26  import org.apache.commons.cli2.builder.ArgumentBuilder;
27  import org.apache.commons.cli2.builder.DefaultOptionBuilder;
28  import org.apache.commons.cli2.builder.GroupBuilder;
29  import org.apache.commons.cli2.commandline.Parser;
30  import org.apache.commons.cli2.validation.NumberValidator;
31  
32  /**
33   * An argument whose value is a negative number is mistaken as an option.
34   *
35   * @author Oliver Heger
36   * @version $Id: BugCLI150Test.java 681191 2008-07-30 20:16:51Z oheger $
37   */
38  public class BugCLI150Test extends TestCase
39  {
40      public void testNegativeNumber() throws OptionException
41      {
42          final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
43          final ArgumentBuilder aBuilder = new ArgumentBuilder();
44          final GroupBuilder gBuilder = new GroupBuilder();
45  
46          final Argument numArg = aBuilder.withValidator(
47                  NumberValidator.getNumberInstance()).withMinimum(1)
48                  .withMaximum(1).create();
49          final Option numOpt = oBuilder.withLongName("num").withArgument(numArg)
50                  .create();
51          final Group options = gBuilder.withOption(numOpt).create();
52  
53          final Parser parser = new Parser();
54          parser.setGroup(options);
55  
56          CommandLine cl = parser.parse(new String[] {
57                  "--num", "-42"
58          });
59          Number num = (Number) cl.getValue(numOpt);
60          assertEquals("Wrong option value", -42, num.intValue());
61      }
62  }