commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r679606 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/AlreadySelectedException.java java/org/apache/commons/cli/OptionGroup.java test/org/apache/commons/cli/OptionGroupTest.java
Date Thu, 24 Jul 2008 22:55:59 GMT
Author: ebourg
Date: Thu Jul 24 15:55:58 2008
New Revision: 679606

URL: http://svn.apache.org/viewvc?rev=679606&view=rev
Log:
Changed AlreadySelectedException to include the related option group and the option that triggered
the exception

Modified:
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java?rev=679606&r1=679605&r2=679606&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/AlreadySelectedException.java
Thu Jul 24 15:55:58 2008
@@ -26,6 +26,12 @@
  */
 public class AlreadySelectedException extends ParseException
 {
+    /** The option group selected. */
+    private OptionGroup group;
+
+    /** The option that triggered the exception. */
+    private Option option;
+
     /**
      * Construct a new <code>AlreadySelectedException</code>
      * with the specified detail message.
@@ -36,4 +42,40 @@
     {
         super(message);
     }
+
+    /**
+     * Construct a new <code>AlreadySelectedException</code>
+     * for the specified option group.
+     *
+     * @param group  the option group already selected
+     * @param option the option that triggered the exception
+     * @since 1.2
+     */
+    public AlreadySelectedException(OptionGroup group, Option option)
+    {
+        this("The option '" + option.getKey() + "' was specified but an option from this
group " +
+                "has already been selected: '" + group.getSelected() + "'");
+        this.group = group;
+        this.option = option;
+    }
+
+    /**
+     * Returns the option group where another option has been selected.
+     *
+     * @since 1.2
+     */
+    public OptionGroup getOptionGroup()
+    {
+        return group;
+    }
+
+    /**
+     * Returns the option that was added to the group and triggered the exception.
+     *
+     * @since 1.2
+     */
+    public Option getOption()
+    {
+        return option;
+    }
 }

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java?rev=679606&r1=679605&r2=679606&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/OptionGroup.java Thu
Jul 24 15:55:58 2008
@@ -14,15 +14,18 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.apache.commons.cli;
 
 import java.io.Serializable;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Map;
 
 /**
  * A group of mutually exclusive options.
+ *
  * @author John Keyes ( john at integralsource.com )
  * @version $Revision$
  */
@@ -31,7 +34,7 @@
     private static final long serialVersionUID = 1L;
     
     /** hold the options */
-    private HashMap optionMap = new HashMap();
+    private Map optionMap = new HashMap();
 
     /** the name of the selected option */
     private String selected;
@@ -40,16 +43,16 @@
     private boolean required;
 
     /**
-     * add <code>opt</code> to this group
+     * Add the specified <code>Option</code> to this group.
      *
-     * @param opt the option to add to this group
-     * @return this option group with opt added
+     * @param option the option to add to this group
+     * @return this option group with the option added
      */
-    public OptionGroup addOption(Option opt)
+    public OptionGroup addOption(Option option)
     {
         // key   - option name
         // value - the option
-        optionMap.put(opt.getKey(), opt);
+        optionMap.put(option.getKey(), option);
 
         return this;
     }
@@ -75,25 +78,22 @@
 
     /**
      * set the selected option of this group to <code>name</code>.
-     * @param opt the option that is selected
+     * @param option the option that is selected
      * @throws AlreadySelectedException if an option from this group has 
      * already been selected.
      */
-    public void setSelected(Option opt)
-                     throws AlreadySelectedException
+    public void setSelected(Option option) throws AlreadySelectedException
     {
         // if no option has already been selected or the 
         // same option is being reselected then set the
         // selected member variable
-        if ((this.selected == null) || this.selected.equals(opt.getOpt()))
+        if ((this.selected == null) || this.selected.equals(option.getOpt()))
         {
-            this.selected = opt.getOpt();
+            this.selected = option.getOpt();
         }
         else
         {
-            throw new AlreadySelectedException("an option from this group has "
-                                               + "already been selected: '"
-                                               + selected + "'");
+            throw new AlreadySelectedException(this, option);
         }
     }
 
@@ -163,4 +163,4 @@
 
         return buff.toString();
     }
-}
\ No newline at end of file
+}

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java?rev=679606&r1=679605&r2=679606&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/OptionGroupTest.java
Thu Jul 24 15:55:58 2008
@@ -139,7 +139,7 @@
         assertTrue( "Confirm TWO extra args", cl.getArgList().size() == 2);
     }
 
-    public void testTwoOptionsFromGroup()
+    public void testTwoOptionsFromGroup() throws Exception
     {
         String[] args = new String[] { "-f", "-d" };
 
@@ -148,16 +148,15 @@
             parser.parse( _options, args);
             fail( "two arguments from group not allowed" );
         }
-        catch (ParseException e)
+        catch (AlreadySelectedException e)
         {
-            if( !( e instanceof AlreadySelectedException ) )
-            {
-                fail( "incorrect exception caught:" + e.getMessage() );
-            }
+            assertNotNull("null option group", e.getOptionGroup());
+            assertEquals("selected option", "f", e.getOptionGroup().getSelected());
+            assertEquals("option", "d", e.getOption().getOpt());
         }
     }
 
-    public void testTwoLongOptionsFromGroup()
+    public void testTwoLongOptionsFromGroup() throws Exception
     {
         String[] args = new String[] { "--file", "--directory" };
 
@@ -166,12 +165,11 @@
             parser.parse(_options, args);
             fail( "two arguments from group not allowed" );
         }
-        catch (ParseException e)
+        catch (AlreadySelectedException e)
         {
-            if( !( e instanceof AlreadySelectedException ) )
-            {
-                fail( "incorrect exception caught:" + e.getMessage() );
-            }
+            assertNotNull("null option group", e.getOptionGroup());
+            assertEquals("selected option", "f", e.getOptionGroup().getSelected());
+            assertEquals("option", "d", e.getOption().getOpt());
         }
     }
 



Mime
View raw message