Skip to content

Commit 28a8c18

Browse files
committed
Changed MissingOptionException to include the list of missing options and build itself the exception message
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@678662 13f79535-47bb-0310-9956-ffa450edef68
1 parent fcc1e5f commit 28a8c18

3 files changed

Lines changed: 63 additions & 21 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.apache.commons.cli;
1919

20+
import java.util.List;
21+
import java.util.Iterator;
22+
2023
/**
2124
* Thrown when a required option has not been provided.
2225
*
@@ -25,6 +28,9 @@
2528
*/
2629
public class MissingOptionException extends ParseException
2730
{
31+
/** The list of missing options */
32+
private List missingOptions;
33+
2834
/**
2935
* Construct a new <code>MissingSelectedException</code>
3036
* with the specified detail message.
@@ -35,4 +41,52 @@ public MissingOptionException(String message)
3541
{
3642
super(message);
3743
}
44+
45+
/**
46+
* Constructs a new <code>MissingSelectedException</code> with the
47+
* specified list of missing options.
48+
*
49+
* @param missingOptions the list of missing options
50+
* @since 1.2
51+
*/
52+
public MissingOptionException(List missingOptions)
53+
{
54+
this(createMessage(missingOptions));
55+
this.missingOptions = missingOptions;
56+
}
57+
58+
/**
59+
* Return the list of options (as strings) missing in the command line parsed.
60+
*
61+
* @since 1.2
62+
*/
63+
public List getMissingOptions()
64+
{
65+
return missingOptions;
66+
}
67+
68+
/**
69+
* Build the exception message from the specified list of options.
70+
*
71+
* @param missingOptions
72+
* @since 1.2
73+
*/
74+
private static String createMessage(List missingOptions)
75+
{
76+
StringBuffer buff = new StringBuffer("Missing required option");
77+
buff.append(missingOptions.size() == 1 ? "" : "s");
78+
buff.append(": ");
79+
80+
Iterator it = missingOptions.iterator();
81+
while (it.hasNext())
82+
{
83+
buff.append(it.next());
84+
if (it.hasNext())
85+
{
86+
buff.append(", ");
87+
}
88+
}
89+
90+
return buff.toString();
91+
}
3892
}

src/java/org/apache/commons/cli/Parser.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,33 +295,18 @@ else if (!("yes".equalsIgnoreCase(value)
295295
}
296296

297297
/**
298-
* <p>Throws a {@link MissingOptionException} if all of the
299-
* required options are no present.</p>
298+
* Throws a {@link MissingOptionException} if all of the required options
299+
* are not present.
300300
*
301301
* @throws MissingOptionException if any of the required Options
302302
* are not present.
303303
*/
304-
protected void checkRequiredOptions()
305-
throws MissingOptionException
304+
protected void checkRequiredOptions() throws MissingOptionException
306305
{
307-
// if there are required options that have not been
308-
// processsed
309-
if (getRequiredOptions().size() > 0)
306+
// if there are required options that have not been processsed
307+
if (!getRequiredOptions().isEmpty())
310308
{
311-
Iterator iter = getRequiredOptions().iterator();
312-
StringBuffer buff = new StringBuffer("Missing required option");
313-
buff.append(getRequiredOptions().size() == 1 ? "" : "s");
314-
buff.append(": ");
315-
316-
317-
// loop through the required options
318-
while (iter.hasNext())
319-
{
320-
buff.append(iter.next());
321-
buff.append(", ");
322-
}
323-
324-
throw new MissingOptionException(buff.substring(0, buff.length() - 2));
309+
throw new MissingOptionException(getRequiredOptions());
325310
}
326311
}
327312

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void testMissingRequiredOption()
7878
catch (MissingOptionException e)
7979
{
8080
assertEquals( "Incorrect exception message", "Missing required option: b", e.getMessage() );
81+
assertTrue(e.getMissingOptions().contains("b"));
8182
}
8283
catch (ParseException e)
8384
{
@@ -103,6 +104,8 @@ public void testMissingRequiredOptions()
103104
catch (MissingOptionException e)
104105
{
105106
assertEquals( "Incorrect exception message", "Missing required options: b, c", e.getMessage() );
107+
assertTrue(e.getMissingOptions().contains("b"));
108+
assertTrue(e.getMissingOptions().contains("c"));
106109
}
107110
catch (ParseException e)
108111
{

0 commit comments

Comments
 (0)