Author: bayard Date: Mon Jun 9 00:27:02 2008 New Revision: 664644 URL: http://svn.apache.org/viewvc?rev=664644&view=rev Log: Applying Brian Egge's patch to CLI-145, fixing things so the withMinimum/withMaximum works correctly Added: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI145Test.java (with props) Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java?rev=664644&r1=664643&r2=664644&view=diff ============================================================================== --- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java (original) +++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/OptionException.java Mon Jun 9 00:27:02 2008 @@ -43,6 +43,9 @@ /** The message explaining the Exception */ private final String message; + /** The id of the message */ + private final String messageKey; + /** * Creates a new OptionException. * @@ -73,6 +76,7 @@ final String messageKey, final String value) { this.option = option; + this.messageKey = messageKey; if (messageKey != null) { final StringBuffer buffer = new StringBuffer(); @@ -104,4 +108,8 @@ public String getMessage() { return message; } + + public String getMessageKey() { + return messageKey; + } } Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java?rev=664644&r1=664643&r2=664644&view=diff ============================================================================== --- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java (original) +++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/option/ArgumentImpl.java Mon Jun 9 00:27:02 2008 @@ -140,7 +140,8 @@ final ListIterator arguments, final Option option) throws OptionException { - int argumentCount = commandLine.getValues(option, Collections.EMPTY_LIST).size(); + // count of arguments processed for this option. + int argumentCount = 0; while (arguments.hasNext() && (argumentCount < maximum)) { final String allValuesQuoted = (String) arguments.next(); Added: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI145Test.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI145Test.java?rev=664644&view=auto ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI145Test.java (added) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI145Test.java Mon Jun 9 00:27:02 2008 @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.cli2.bug; + +import junit.framework.TestCase; +import org.apache.commons.cli2.CommandLine; +import org.apache.commons.cli2.Group; +import org.apache.commons.cli2.builder.ArgumentBuilder; +import org.apache.commons.cli2.builder.DefaultOptionBuilder; +import org.apache.commons.cli2.builder.GroupBuilder; +import org.apache.commons.cli2.commandline.Parser; +import org.apache.commons.cli2.option.DefaultOption; + +import java.util.List; + +/** + * ArgumentBuilder.withMaximum causes parse errors: Unexpeced while processing options + * + * @author David Biesack + * @author brianegge + */ +public class BugCLI145Test extends TestCase { + public void testWithMaximum() { + final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); + final ArgumentBuilder abuilder = new ArgumentBuilder(); + final GroupBuilder gbuilder = new GroupBuilder(); + DefaultOption aOption = obuilder// + .withShortName("a") + .withLongName("a") + .withArgument(abuilder + .withName("a") + .withDefault("10") + .create()) + .create(); + DefaultOption bOption = obuilder + .withShortName("b") + .withLongName("b") + .withArgument(abuilder + .withName("b") + .withMinimum(2) + .withMaximum(4) + .withDefault("100") + .withDefault("1000") + .withDefault("10000") + .withDefault("1000000") + .create()) + .create(); + Group options = gbuilder + .withName("options") + .withOption(aOption) + .withOption(bOption) + .create(); + Parser parser = new Parser(); + parser.setHelpTrigger("--help"); + parser.setGroup(options); + CommandLine cl = parser.parseAndHelp("-a 0 -b 1 2 3 4".split(" ")); + assertNotNull(cl); + int a = Integer.parseInt(cl.getValue(aOption).toString()); + List b = cl.getValues(bOption); + assertEquals(0, a); + assertEquals(4, b.size()); + } + + public void testWithMaximumUsingDefaultValues() { + final DefaultOptionBuilder obuilder = new DefaultOptionBuilder(); + final ArgumentBuilder abuilder = new ArgumentBuilder(); + final GroupBuilder gbuilder = new GroupBuilder(); + DefaultOption aOption = obuilder// + .withShortName("a") + .withLongName("a") + .withArgument(abuilder + .withName("a") + .withDefault("10") + .create()) + .create(); + DefaultOption bOption = obuilder + .withShortName("b") + .withLongName("b") + .withArgument(abuilder + .withName("b") + .withMinimum(2) + .withMaximum(4) + .withDefault("100") + .withDefault("1000") + .withDefault("10000") + .create()) + .create(); + Group options = gbuilder + .withName("options") + .withOption(aOption) + .withOption(bOption) + .create(); + Parser parser = new Parser(); + parser.setHelpTrigger("--help"); + parser.setGroup(options); + CommandLine cl = parser.parseAndHelp("-a -b".split(" ")); + assertNotNull(cl); + int a = Integer.parseInt(cl.getValue(aOption).toString()); + List b = cl.getValues(bOption); + assertEquals(10, a); + assertEquals(3, b.size()); + assertEquals("10000", b.get(2)); + } +} Propchange: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI145Test.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java?rev=664644&r1=664643&r2=664644&view=diff ============================================================================== --- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java (original) +++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java Mon Jun 9 00:27:02 2008 @@ -23,6 +23,7 @@ import org.apache.commons.cli2.Argument; import org.apache.commons.cli2.Group; import org.apache.commons.cli2.OptionException; +import org.apache.commons.cli2.resource.ResourceConstants; import org.apache.commons.cli2.builder.ArgumentBuilder; import org.apache.commons.cli2.builder.DefaultOptionBuilder; import org.apache.commons.cli2.builder.GroupBuilder; @@ -31,7 +32,7 @@ /** * The first is a loop in Parser.parse() if I set a non-declared option. This - * code goes into a loop in Parser.java method parse this “while” loop runs + * code goes into a loop in Parser.java method parse this 'while' loop runs * endless * * @author Steve Alberty @@ -73,7 +74,7 @@ parser.parse(new String[] { "testfile.txt", "testfile.txt", "testfile.txt", "testfile.txt" }); fail("OptionException"); } catch (OptionException e) { - assertEquals("Unexpected testfile.txt while processing ", e.getMessage()); + assertEquals(ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, e.getMessageKey()); } } }