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.CommandLine;
022import org.apache.commons.cli2.Group;
023import org.apache.commons.cli2.Option;
024import org.apache.commons.cli2.builder.ArgumentBuilder;
025import org.apache.commons.cli2.builder.DefaultOptionBuilder;
026import org.apache.commons.cli2.builder.GroupBuilder;
027import org.apache.commons.cli2.commandline.Parser;
028
029/**
030 * @author John Keyes
031 */
032public class Bug15046Test extends TestCase {
033
034    public Bug15046Test(String name) {
035        super(name);
036    }
037
038    public void testParamNamedAsOption() throws Exception {
039        final String[] CLI_ARGS = new String[] { "-z", "c" };
040
041        DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
042        ArgumentBuilder abuilder = new ArgumentBuilder();
043
044        Option option =
045            obuilder
046                .withShortName("z")
047                .withLongName("timezone")
048                .withDescription("affected option")
049                .withArgument(abuilder.withName("timezone").create())
050                .create();
051
052        GroupBuilder gbuilder = new GroupBuilder();
053        Group options =
054            gbuilder.withName("bug15046").withOption(option).create();
055
056        Parser parser = new Parser();
057        parser.setGroup(options);
058        CommandLine line = parser.parse(CLI_ARGS);
059
060        assertEquals("c", line.getValue("-z"));
061
062        Option c =
063            obuilder
064                .withShortName("c")
065                .withLongName("conflict")
066                .withDescription("conflicting option")
067                .withArgument(abuilder.withName("conflict").create())
068                .create();
069
070        options =
071            gbuilder
072                .withName("bug15046")
073                .withOption(option)
074                .withOption(c)
075                .create();
076
077        parser.setGroup(options);
078        line = parser.parse(CLI_ARGS);
079
080        assertEquals("c", line.getValue("-z"));
081    }
082}