Skip to content

Commit 50b149a

Browse files
committed
Simplify and test MissingOptionException message construction
1 parent ffce848 commit 50b149a

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

src/main/java/org/apache/commons/cli/MissingOptionException.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717

1818
package org.apache.commons.cli;
1919

20-
import java.util.Iterator;
2120
import java.util.List;
2221

2322
/**
2423
* Thrown when a required option has not been provided.
2524
*/
2625
public class MissingOptionException extends ParseException {
26+
2727
/** This exception {@code serialVersionUID}. */
2828
private static final long serialVersionUID = 8161889051578563249L;
2929

@@ -34,18 +34,9 @@ public class MissingOptionException extends ParseException {
3434
*/
3535
private static String createMessage(final List<?> missingOptions) {
3636
final StringBuilder buf = new StringBuilder("Missing required option");
37-
buf.append(missingOptions.size() == 1 ? "" : "s");
38-
buf.append(": ");
39-
40-
final Iterator<?> it = missingOptions.iterator();
41-
while (it.hasNext()) {
42-
buf.append(it.next());
43-
if (it.hasNext()) {
44-
buf.append(", ");
45-
}
46-
}
47-
48-
return buf.toString();
37+
buf.append(missingOptions.size() == 1 ? "" : "s").append(": ");
38+
final String string = missingOptions.toString();
39+
return buf.append(string.substring(1, string.length() - 1)).toString();
4940
}
5041

5142
/** The list of missing options and groups */

src/test/java/org/apache/commons/cli/MissingOptionExceptionTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ Licensed to the Apache Software Foundation (ASF) under one or more
3030
*/
3131
public class MissingOptionExceptionTest {
3232

33+
@Test
34+
void testGetMessage() {
35+
final List<String> originalList = new ArrayList<>();
36+
originalList.add("optA");
37+
originalList.add("optB");
38+
final MissingOptionException exception = new MissingOptionException(originalList);
39+
assertEquals("Missing required options: optA, optB", exception.getMessage());
40+
assertEquals("Missing required options: ", new MissingOptionException(new ArrayList<>()).getMessage());
41+
}
42+
3343
@Test
3444
void testGetMissingOptions() {
3545
final List<String> originalList = new ArrayList<>();

0 commit comments

Comments
 (0)