Skip to content

Commit e2ba946

Browse files
committed
Applying Brian Egge's patch to CLI-145, fixing things so the withMinimum/withMaximum works correctly
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@664644 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2b4fb79 commit e2ba946

4 files changed

Lines changed: 131 additions & 3 deletions

File tree

src/java/org/apache/commons/cli2/OptionException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public class OptionException
4343
/** The message explaining the Exception */
4444
private final String message;
4545

46+
/** The id of the message */
47+
private final String messageKey;
48+
4649
/**
4750
* Creates a new OptionException.
4851
*
@@ -73,6 +76,7 @@ public OptionException(final Option option,
7376
final String messageKey,
7477
final String value) {
7578
this.option = option;
79+
this.messageKey = messageKey;
7680

7781
if (messageKey != null) {
7882
final StringBuffer buffer = new StringBuffer();
@@ -104,4 +108,8 @@ public Option getOption() {
104108
public String getMessage() {
105109
return message;
106110
}
111+
112+
public String getMessageKey() {
113+
return messageKey;
114+
}
107115
}

src/java/org/apache/commons/cli2/option/ArgumentImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ public void processValues(final WriteableCommandLine commandLine,
140140
final ListIterator arguments,
141141
final Option option)
142142
throws OptionException {
143-
int argumentCount = commandLine.getValues(option, Collections.EMPTY_LIST).size();
143+
// count of arguments processed for this option.
144+
int argumentCount = 0;
144145

145146
while (arguments.hasNext() && (argumentCount < maximum)) {
146147
final String allValuesQuoted = (String) arguments.next();
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.cli2.bug;
18+
19+
import junit.framework.TestCase;
20+
import org.apache.commons.cli2.CommandLine;
21+
import org.apache.commons.cli2.Group;
22+
import org.apache.commons.cli2.builder.ArgumentBuilder;
23+
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
24+
import org.apache.commons.cli2.builder.GroupBuilder;
25+
import org.apache.commons.cli2.commandline.Parser;
26+
import org.apache.commons.cli2.option.DefaultOption;
27+
28+
import java.util.List;
29+
30+
/**
31+
* ArgumentBuilder.withMaximum causes parse errors: Unexpeced <value> while processing options
32+
*
33+
* @author David Biesack
34+
* @author brianegge
35+
*/
36+
public class BugCLI145Test extends TestCase {
37+
public void testWithMaximum() {
38+
final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
39+
final ArgumentBuilder abuilder = new ArgumentBuilder();
40+
final GroupBuilder gbuilder = new GroupBuilder();
41+
DefaultOption aOption = obuilder//
42+
.withShortName("a")
43+
.withLongName("a")
44+
.withArgument(abuilder
45+
.withName("a")
46+
.withDefault("10")
47+
.create())
48+
.create();
49+
DefaultOption bOption = obuilder
50+
.withShortName("b")
51+
.withLongName("b")
52+
.withArgument(abuilder
53+
.withName("b")
54+
.withMinimum(2)
55+
.withMaximum(4)
56+
.withDefault("100")
57+
.withDefault("1000")
58+
.withDefault("10000")
59+
.withDefault("1000000")
60+
.create())
61+
.create();
62+
Group options = gbuilder
63+
.withName("options")
64+
.withOption(aOption)
65+
.withOption(bOption)
66+
.create();
67+
Parser parser = new Parser();
68+
parser.setHelpTrigger("--help");
69+
parser.setGroup(options);
70+
CommandLine cl = parser.parseAndHelp("-a 0 -b 1 2 3 4".split(" "));
71+
assertNotNull(cl);
72+
int a = Integer.parseInt(cl.getValue(aOption).toString());
73+
List b = cl.getValues(bOption);
74+
assertEquals(0, a);
75+
assertEquals(4, b.size());
76+
}
77+
78+
public void testWithMaximumUsingDefaultValues() {
79+
final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
80+
final ArgumentBuilder abuilder = new ArgumentBuilder();
81+
final GroupBuilder gbuilder = new GroupBuilder();
82+
DefaultOption aOption = obuilder//
83+
.withShortName("a")
84+
.withLongName("a")
85+
.withArgument(abuilder
86+
.withName("a")
87+
.withDefault("10")
88+
.create())
89+
.create();
90+
DefaultOption bOption = obuilder
91+
.withShortName("b")
92+
.withLongName("b")
93+
.withArgument(abuilder
94+
.withName("b")
95+
.withMinimum(2)
96+
.withMaximum(4)
97+
.withDefault("100")
98+
.withDefault("1000")
99+
.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+
}

src/test/org/apache/commons/cli2/bug/BugLoopingOptionLookAlikeTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.cli2.Argument;
2424
import org.apache.commons.cli2.Group;
2525
import org.apache.commons.cli2.OptionException;
26+
import org.apache.commons.cli2.resource.ResourceConstants;
2627
import org.apache.commons.cli2.builder.ArgumentBuilder;
2728
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
2829
import org.apache.commons.cli2.builder.GroupBuilder;
@@ -31,7 +32,7 @@
3132

3233
/**
3334
* The first is a loop in Parser.parse() if I set a non-declared option. This
34-
* code goes into a loop in Parser.java method parse this while loop runs
35+
* code goes into a loop in Parser.java method parse this 'while' loop runs
3536
* endless
3637
*
3738
* @author Steve Alberty
@@ -73,7 +74,7 @@ public void testLoopingOptionLookAlike2() {
7374
parser.parse(new String[] { "testfile.txt", "testfile.txt", "testfile.txt", "testfile.txt" });
7475
fail("OptionException");
7576
} catch (OptionException e) {
76-
assertEquals("Unexpected testfile.txt while processing ", e.getMessage());
77+
assertEquals(ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, e.getMessageKey());
7778
}
7879
}
7980
}

0 commit comments

Comments
 (0)