Skip to content

Commit 538fb68

Browse files
committed
Applying Andrew's fix to CLI-61 and rolling back the getMessageKey addition to OptionException that was applied from CLI-145
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/trunk@664646 13f79535-47bb-0310-9956-ffa450edef68
1 parent e2ba946 commit 538fb68

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ 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-
4946
/**
5047
* Creates a new OptionException.
5148
*
@@ -76,7 +73,6 @@ public OptionException(final Option option,
7673
final String messageKey,
7774
final String value) {
7875
this.option = option;
79-
this.messageKey = messageKey;
8076

8177
if (messageKey != null) {
8278
final StringBuffer buffer = new StringBuffer();
@@ -109,7 +105,4 @@ public String getMessage() {
109105
return message;
110106
}
111107

112-
public String getMessageKey() {
113-
return messageKey;
114-
}
115108
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public interface WriteableCommandLine extends CommandLine {
3636
*/
3737
void addValue(final Option option, final Object value);
3838

39+
/**
40+
* Retrieves the Argument values specified on the command line for the
41+
* specified Option, this doesn't return any values supplied
42+
* programmatically as defaults.
43+
*
44+
* @param option the Option associated with the values
45+
* @return a list of values or an empty List if none are found
46+
*/
47+
List getUndefaultedValues(final Option option);
48+
3949
/**
4050
* Sets the default values for an Option in the CommandLine
4151
* @param option the Option to add to

src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ public List getValues(final Option option,
129129
return valueList;
130130
}
131131

132+
public List getUndefaultedValues(Option option) {
133+
// First grab the command line values
134+
List valueList = (List) values.get(option);
135+
136+
// Finally use an empty list
137+
if (valueList == null) {
138+
valueList = Collections.EMPTY_LIST;
139+
}
140+
141+
return valueList;
142+
}
143+
132144
public Boolean getSwitch(final Option option,
133145
final Boolean defaultValue) {
134146
// First grab the command line values

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void processValues(final WriteableCommandLine commandLine,
141141
final Option option)
142142
throws OptionException {
143143
// count of arguments processed for this option.
144-
int argumentCount = 0;
144+
int argumentCount = commandLine.getUndefaultedValues(option).size();
145145

146146
while (arguments.hasNext() && (argumentCount < maximum)) {
147147
final String allValuesQuoted = (String) arguments.next();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
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;
2726
import org.apache.commons.cli2.builder.ArgumentBuilder;
2827
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
2928
import org.apache.commons.cli2.builder.GroupBuilder;
@@ -74,7 +73,7 @@ public void testLoopingOptionLookAlike2() {
7473
parser.parse(new String[] { "testfile.txt", "testfile.txt", "testfile.txt", "testfile.txt" });
7574
fail("OptionException");
7675
} catch (OptionException e) {
77-
assertEquals(ResourceConstants.ARGUMENT_UNEXPECTED_VALUE, e.getMessageKey());
76+
assertEquals("Unexpected testfile.txt while processing ", e.getMessage());
7877
}
7978
}
8079
}

0 commit comments

Comments
 (0)