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; 020import org.apache.commons.cli2.CommandLine; 021import org.apache.commons.cli2.Group; 022import org.apache.commons.cli2.builder.ArgumentBuilder; 023import org.apache.commons.cli2.builder.DefaultOptionBuilder; 024import org.apache.commons.cli2.builder.GroupBuilder; 025import org.apache.commons.cli2.commandline.Parser; 026import org.apache.commons.cli2.option.DefaultOption; 027 028import java.util.List; 029 030/** 031 * ArgumentBuilder.withMaximum causes parse errors: Unexpeced <value> while processing options 032 * 033 * @author David Biesack 034 * @author brianegge 035 */ 036public class BugCLI145Test extends TestCase { 037 public void testWithMaximum() { 038 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); 039 final ArgumentBuilder abuilder = new ArgumentBuilder(); 040 final GroupBuilder gbuilder = new GroupBuilder(); 041 DefaultOption aOption = obuilder// 042 .withShortName("a") 043 .withLongName("a") 044 .withArgument(abuilder 045 .withName("a") 046 .withDefault("10") 047 .create()) 048 .create(); 049 DefaultOption bOption = obuilder 050 .withShortName("b") 051 .withLongName("b") 052 .withArgument(abuilder 053 .withName("b") 054 .withMinimum(2) 055 .withMaximum(4) 056 .withDefault("100") 057 .withDefault("1000") 058 .withDefault("10000") 059 .withDefault("1000000") 060 .create()) 061 .create(); 062 Group options = gbuilder 063 .withName("options") 064 .withOption(aOption) 065 .withOption(bOption) 066 .create(); 067 Parser parser = new Parser(); 068 parser.setHelpTrigger("--help"); 069 parser.setGroup(options); 070 CommandLine cl = parser.parseAndHelp("-a 0 -b 1 2 3 4".split(" ")); 071 assertNotNull(cl); 072 int a = Integer.parseInt(cl.getValue(aOption).toString()); 073 List b = cl.getValues(bOption); 074 assertEquals(0, a); 075 assertEquals(4, b.size()); 076 } 077 078 public void testWithMaximumUsingDefaultValues() { 079 final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); 080 final ArgumentBuilder abuilder = new ArgumentBuilder(); 081 final GroupBuilder gbuilder = new GroupBuilder(); 082 DefaultOption aOption = obuilder// 083 .withShortName("a") 084 .withLongName("a") 085 .withArgument(abuilder 086 .withName("a") 087 .withDefault("10") 088 .create()) 089 .create(); 090 DefaultOption bOption = obuilder 091 .withShortName("b") 092 .withLongName("b") 093 .withArgument(abuilder 094 .withName("b") 095 .withMinimum(2) 096 .withMaximum(4) 097 .withDefault("100") 098 .withDefault("1000") 099 .withDefault("10000") 100 .create()) 101 .create(); 102 Group options = gbuilder 103 .withName("options") 104 .withOption(aOption) 105 .withOption(bOption) 106 .create(); 107 Parser parser = new Parser(); 108 parser.setHelpTrigger("--help"); 109 parser.setGroup(options); 110 CommandLine cl = parser.parseAndHelp("-a -b".split(" ")); 111 assertNotNull(cl); 112 int a = Integer.parseInt(cl.getValue(aOption).toString()); 113 List b = cl.getValues(bOption); 114 assertEquals(10, a); 115 assertEquals(3, b.size()); 116 assertEquals("10000", b.get(2)); 117 } 118}