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 org.apache.commons.cli2.Argument; 020import org.apache.commons.cli2.CommandLine; 021import org.apache.commons.cli2.Group; 022import org.apache.commons.cli2.Option; 023import org.apache.commons.cli2.OptionException; 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 029import junit.framework.TestCase; 030 031/** 032 * http://issues.apache.org/jira/browse/CLI-80 033 */ 034public class BugCLI80Test extends TestCase { 035 036 public void testBug() { 037 final String optName = "option"; 038 039 Argument arg = new ArgumentBuilder().withName(optName) 040 .withMaximum(1) 041 .create(); 042 043 Option option = new DefaultOptionBuilder().withArgument(arg) 044 .withDescription("singular option") 045 .withLongName(optName) 046 .withShortName("o") 047 .create(); 048 049 Group group = new GroupBuilder().withOption(option).create(); 050 051 Parser p = new Parser(); 052 p.setGroup(group); 053 054 CommandLine cl = p.parseAndHelp( new String[] { "-o", "yes" } ); 055 assertNotNull("Couldn't parse valid commandLine", cl); 056 057 assertEquals("Couldn't look up value by short name", "yes", cl.getValue("-o") ); 058 059 try { 060 cl = p.parse( new String[] { "-o", "yes", "-o", "jam" } ); 061 fail("Parsed invalid commandLine"); 062 } catch(OptionException e) { 063 // ok 064 } 065 } 066 067}