You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2008/07/22 09:30:19 UTC

svn commit: r678662 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/MissingOptionException.java java/org/apache/commons/cli/Parser.java test/org/apache/commons/cli/ParseRequiredTest.java

Author: ebourg
Date: Tue Jul 22 00:30:18 2008
New Revision: 678662

URL: http://svn.apache.org/viewvc?rev=678662&view=rev
Log:
Changed MissingOptionException to include the list of missing options and build itself the exception message

Modified:
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/MissingOptionException.java
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParseRequiredTest.java

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/MissingOptionException.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/MissingOptionException.java?rev=678662&r1=678661&r2=678662&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/MissingOptionException.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/MissingOptionException.java Tue Jul 22 00:30:18 2008
@@ -17,6 +17,9 @@
 
 package org.apache.commons.cli;
 
+import java.util.List;
+import java.util.Iterator;
+
 /**
  * Thrown when a required option has not been provided.
  *
@@ -25,6 +28,9 @@
  */
 public class MissingOptionException extends ParseException
 {
+    /** The list of missing options */
+    private List missingOptions;
+
     /**
      * Construct a new <code>MissingSelectedException</code>
      * with the specified detail message.
@@ -35,4 +41,52 @@
     {
         super(message);
     }
+
+    /**
+     * Constructs a new <code>MissingSelectedException</code> with the
+     * specified list of missing options.
+     *
+     * @param missingOptions the list of missing options
+     * @since 1.2
+     */
+    public MissingOptionException(List missingOptions)
+    {
+        this(createMessage(missingOptions));
+        this.missingOptions = missingOptions;
+    }
+
+    /**
+     * Return the list of options (as strings) missing in the command line parsed.
+     *
+     * @since 1.2
+     */
+    public List getMissingOptions()
+    {
+        return missingOptions;
+    }
+
+    /**
+     * Build the exception message from the specified list of options.
+     *
+     * @param missingOptions
+     * @since 1.2
+     */
+    private static String createMessage(List missingOptions)
+    {
+        StringBuffer buff = new StringBuffer("Missing required option");
+        buff.append(missingOptions.size() == 1 ? "" : "s");
+        buff.append(": ");
+
+        Iterator it = missingOptions.iterator();
+        while (it.hasNext())
+        {
+            buff.append(it.next());
+            if (it.hasNext())
+            {
+                buff.append(", ");
+            }
+        }
+
+        return buff.toString();
+    }
 }

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java?rev=678662&r1=678661&r2=678662&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/Parser.java Tue Jul 22 00:30:18 2008
@@ -295,33 +295,18 @@
     }
 
     /**
-     * <p>Throws a {@link MissingOptionException} if all of the
-     * required options are no present.</p>
+     * Throws a {@link MissingOptionException} if all of the required options
+     * are not present.
      *
      * @throws MissingOptionException if any of the required Options
      * are not present.
      */
-    protected void checkRequiredOptions()
-        throws MissingOptionException
+    protected void checkRequiredOptions() throws MissingOptionException
     {
-        // if there are required options that have not been
-        // processsed
-        if (getRequiredOptions().size() > 0)
-        {
-            Iterator iter = getRequiredOptions().iterator();
-            StringBuffer buff = new StringBuffer("Missing required option");
-            buff.append(getRequiredOptions().size() == 1 ? "" : "s");
-            buff.append(": ");
-
-
-            // loop through the required options
-            while (iter.hasNext())
-            {
-                buff.append(iter.next());
-                buff.append(", ");
-            }
-
-            throw new MissingOptionException(buff.substring(0, buff.length() - 2));
+        // if there are required options that have not been processsed
+        if (!getRequiredOptions().isEmpty())
+        {
+            throw new MissingOptionException(getRequiredOptions());
         }
     }
 

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParseRequiredTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParseRequiredTest.java?rev=678662&r1=678661&r2=678662&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParseRequiredTest.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/ParseRequiredTest.java Tue Jul 22 00:30:18 2008
@@ -78,6 +78,7 @@
         catch (MissingOptionException e)
         {
             assertEquals( "Incorrect exception message", "Missing required option: b", e.getMessage() );
+            assertTrue(e.getMissingOptions().contains("b"));
         }
         catch (ParseException e)
         {
@@ -103,6 +104,8 @@
         catch (MissingOptionException e)
         {
             assertEquals( "Incorrect exception message", "Missing required options: b, c", e.getMessage() );
+            assertTrue(e.getMissingOptions().contains("b"));
+            assertTrue(e.getMissingOptions().contains("c"));
         }
         catch (ParseException e)
         {