001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.cli2.bug;
018
019import junit.framework.TestCase;
020
021import org.apache.commons.cli2.Argument;
022import org.apache.commons.cli2.CommandLine;
023import org.apache.commons.cli2.Group;
024import org.apache.commons.cli2.Option;
025import org.apache.commons.cli2.OptionException;
026import org.apache.commons.cli2.builder.ArgumentBuilder;
027import org.apache.commons.cli2.builder.DefaultOptionBuilder;
028import org.apache.commons.cli2.builder.GroupBuilder;
029import org.apache.commons.cli2.commandline.Parser;
030import org.apache.commons.cli2.validation.NumberValidator;
031
032/**
033 * An argument whose value is a negative number is mistaken as an option.
034 *
035 * @author Oliver Heger
036 * @version $Id: BugCLI150Test.java 681191 2008-07-30 20:16:51Z oheger $
037 */
038public class BugCLI150Test extends TestCase
039{
040    public void testNegativeNumber() throws OptionException
041    {
042        final DefaultOptionBuilder oBuilder = new DefaultOptionBuilder();
043        final ArgumentBuilder aBuilder = new ArgumentBuilder();
044        final GroupBuilder gBuilder = new GroupBuilder();
045
046        final Argument numArg = aBuilder.withValidator(
047                NumberValidator.getNumberInstance()).withMinimum(1)
048                .withMaximum(1).create();
049        final Option numOpt = oBuilder.withLongName("num").withArgument(numArg)
050                .create();
051        final Group options = gBuilder.withOption(numOpt).create();
052
053        final Parser parser = new Parser();
054        parser.setGroup(options);
055
056        CommandLine cl = parser.parse(new String[] {
057                "--num", "-42"
058        });
059        Number num = (Number) cl.getValue(numOpt);
060        assertEquals("Wrong option value", -42, num.intValue());
061    }
062}