|
| 1 | +/** |
| 2 | + * Copyright 2006 The Apache Software Foundation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.commons.cli2.bug; |
| 17 | + |
| 18 | +import org.apache.commons.cli2.Argument; |
| 19 | +import org.apache.commons.cli2.CommandLine; |
| 20 | +import org.apache.commons.cli2.Group; |
| 21 | +import org.apache.commons.cli2.Option; |
| 22 | +import org.apache.commons.cli2.OptionException; |
| 23 | +import org.apache.commons.cli2.builder.ArgumentBuilder; |
| 24 | +import org.apache.commons.cli2.builder.DefaultOptionBuilder; |
| 25 | +import org.apache.commons.cli2.builder.GroupBuilder; |
| 26 | +import org.apache.commons.cli2.commandline.Parser; |
| 27 | + |
| 28 | +import junit.framework.TestCase; |
| 29 | + |
| 30 | +/** |
| 31 | + * http://issues.apache.org/jira/browse/CLI-80 |
| 32 | + */ |
| 33 | +public class BugCLI80Test extends TestCase { |
| 34 | + |
| 35 | + public void testBug() { |
| 36 | + final String optName = "option"; |
| 37 | + |
| 38 | + Argument arg = new ArgumentBuilder().withName(optName) |
| 39 | + .withMaximum(1) |
| 40 | + .create(); |
| 41 | + |
| 42 | + Option option = new DefaultOptionBuilder().withArgument(arg) |
| 43 | + .withDescription("singular option") |
| 44 | + .withLongName(optName) |
| 45 | + .withShortName("o") |
| 46 | + .create(); |
| 47 | + |
| 48 | + Group group = new GroupBuilder().withOption(option).create(); |
| 49 | + |
| 50 | + Parser p = new Parser(); |
| 51 | + p.setGroup(group); |
| 52 | + |
| 53 | + CommandLine cl = p.parseAndHelp( new String[] { "-o", "yes" } ); |
| 54 | + assertNotNull("Couldn't parse valid commandLine", cl); |
| 55 | + |
| 56 | + assertEquals("Couldn't look up value by short name", "yes", cl.getValue("-o") ); |
| 57 | + |
| 58 | + try { |
| 59 | + cl = p.parse( new String[] { "-o", "yes", "-o", "jam" } ); |
| 60 | + fail("Parsed invalid commandLine"); |
| 61 | + } catch(OptionException e) { |
| 62 | + // ok |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments