commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r780163 - in /commons/proper/cli/trunk: src/java/org/apache/commons/cli/OptionGroup.java src/java/org/apache/commons/cli/Parser.java src/test/org/apache/commons/cli/ParserTestCase.java xdocs/changes.xml
Date Sat, 30 May 2009 02:31:22 GMT
Author: ebourg
Date: Sat May 30 02:31:22 2009
New Revision: 780163

URL: http://svn.apache.org/viewvc?rev=780163&view=rev
Log:
OptionGroup now selects properly an option with no short name (CLI-182)
OptionGroups no longer throw an AlreadySelectedException when reused for several parsings
(CLI-183)

Modified:
    commons/proper/cli/trunk/src/java/org/apache/commons/cli/OptionGroup.java
    commons/proper/cli/trunk/src/java/org/apache/commons/cli/Parser.java
    commons/proper/cli/trunk/src/test/org/apache/commons/cli/ParserTestCase.java
    commons/proper/cli/trunk/xdocs/changes.xml

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli/OptionGroup.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli/OptionGroup.java?rev=780163&r1=780162&r2=780163&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli/OptionGroup.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli/OptionGroup.java Sat May 30 02:31:22
2009
@@ -85,12 +85,19 @@
      */
     public void setSelected(Option option) throws AlreadySelectedException
     {
+        if (option == null)
+        {
+            // reset the option previously selected
+            selected = null;
+            return;
+        }
+        
         // if no option has already been selected or the 
         // same option is being reselected then set the
         // selected member variable
-        if (selected == null || selected.equals(option.getOpt()))
+        if (selected == null || selected.equals(option.getKey()))
         {
-            selected = option.getOpt();
+            selected = option.getKey();
         }
         else
         {

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli/Parser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli/Parser.java?rev=780163&r1=780162&r2=780163&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli/Parser.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli/Parser.java Sat May 30 02:31:22
2009
@@ -145,6 +145,13 @@
             Option opt = (Option) it.next();
             opt.clearValues();
         }
+        
+        // clear the data from the groups
+        for (Iterator it = options.getOptionGroups().iterator(); it.hasNext();)
+        {
+            OptionGroup group = (OptionGroup) it.next();
+            group.setSelected(null);
+        }        
 
         // initialise members
         setOptions(options);

Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli/ParserTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli/ParserTestCase.java?rev=780163&r1=780162&r2=780163&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli/ParserTestCase.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli/ParserTestCase.java Sat May 30
02:31:22 2009
@@ -144,7 +144,7 @@
         assertTrue( "Confirm MissingArgumentException caught", caught );
     }
 
-    public void testDoubleDash() throws Exception
+    public void testDoubleDash1() throws Exception
     {
         String[] args = new String[] { "--copt",
                                        "--",
@@ -574,7 +574,63 @@
             fail("expected to catch MissingOptionException");
         }
     }
+    
+    public void testMissingRequiredGroup() throws Exception
+    {
+        OptionGroup group = new OptionGroup();
+        group.addOption(OptionBuilder.create("a"));
+        group.addOption(OptionBuilder.create("b"));
+        group.setRequired(true);
+
+        Options options = new Options();
+        options.addOptionGroup(group);
+        options.addOption(OptionBuilder.isRequired().create("c"));
+
+        try
+        {
+            parser.parse(options, new String[] { "-c" });
+            fail("MissingOptionException not thrown");
+        }
+        catch (MissingOptionException e)
+        {
+            assertEquals(1, e.getMissingOptions().size());
+            assertTrue(e.getMissingOptions().get(0) instanceof OptionGroup);
+        }
+        catch (ParseException e)
+        {
+            fail("Expected to catch MissingOptionException");
+        }
+    }
+    
+    public void testOptionGroup() throws Exception
+    {
+        OptionGroup group = new OptionGroup();
+        group.addOption(OptionBuilder.create("a"));
+        group.addOption(OptionBuilder.create("b"));
+        
+        Options options = new Options();
+        options.addOptionGroup(group);
+        
+        parser.parse(options, new String[] { "-b" });
+        
+        assertEquals("selected option", "b", group.getSelected());
+    }
 
+    public void testOptionGroupLong() throws Exception
+    {
+        OptionGroup group = new OptionGroup();
+        group.addOption(OptionBuilder.withLongOpt("foo").create());
+        group.addOption(OptionBuilder.withLongOpt("bar").create());
+        
+        Options options = new Options();
+        options.addOptionGroup(group);
+        
+        CommandLine cl = parser.parse(options, new String[] { "--bar" });
+        
+        assertTrue(cl.hasOption("bar"));
+        assertEquals("selected option", "bar", group.getSelected());
+    }
+    
     public void testReuseOptionsTwice() throws Exception
     {
         Options opts = new Options();

Modified: commons/proper/cli/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/xdocs/changes.xml?rev=780163&r1=780162&r2=780163&view=diff
==============================================================================
--- commons/proper/cli/trunk/xdocs/changes.xml (original)
+++ commons/proper/cli/trunk/xdocs/changes.xml Sat May 30 02:31:22 2009
@@ -23,8 +23,14 @@
   <body>
     
     <release version="1.3" date="in SVN">
+      <action type="fix" dev="ebourg" issue="CLI-183">
+        OptionGroups no longer throw an AlreadySelectedException when reused for several
parsings.
+      </action>
+      <action type="fix" dev="ebourg" issue="CLI-182">
+        OptionGroup now selects properly an option with no short name.
+      </action>
       <action type="add" dev="ebourg" issue="CLI-160">
-        PosixParser now supports partial long options (--ver instead of --version)
+        PosixParser now supports partial long options (--ver instead of --version).
       </action>
     </release>
 



Mime
View raw message