commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r955501 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/ test/java/org/apache/commons/cli/
Date Thu, 17 Jun 2010 07:57:39 GMT
Author: ebourg
Date: Thu Jun 17 07:57:38 2010
New Revision: 955501

URL: http://svn.apache.org/viewvc?rev=955501&view=rev
Log:
Fixed the bugs with the default options and the option groups (CLI-203 and CLI-204)

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionGroupTest.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java?rev=955501&r1=955500&r2=955501&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/DefaultParser.java Thu Jun
17 07:57:38 2010
@@ -147,10 +147,18 @@ public class DefaultParser implements Co
         {
             String option = e.nextElement().toString();
             
-            if (!cmd.hasOption(option))
+            Option opt = options.getOption(option);
+            if (opt == null)
+            {
+                throw new UnrecognizedOptionException("Default option wasn't defined", option);
+            }
+            
+            // if the option is part of a group, check if another option of the group has
been selected
+            OptionGroup group = options.getOptionGroup(opt);
+            boolean selected = group != null && group.getSelected() != null;
+            
+            if (!cmd.hasOption(option) && !selected)
             {
-                Option opt = options.getOption(option);
-                
                 // get the value from the properties
                 String value = properties.getProperty(option);
                 

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java?rev=955501&r1=955500&r2=955501&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Parser.java Thu Jun 17 07:57:38
2010
@@ -259,11 +259,19 @@ public abstract class Parser implements 
         for (Enumeration e = properties.propertyNames(); e.hasMoreElements();)
         {
             String option = e.nextElement().toString();
-
-            if (!cmd.hasOption(option))
+            
+            Option opt = options.getOption(option);
+            if (opt == null)
+            {
+                throw new UnrecognizedOptionException("Default option wasn't defined", option);
+            }
+            
+            // if the option is part of a group, check if another option of the group has
been selected
+            OptionGroup group = options.getOptionGroup(opt);
+            boolean selected = group != null && group.getSelected() != null;
+            
+            if (!cmd.hasOption(option) && !selected)
             {
-                Option opt = getOptions().getOption(option);
-
                 // get the value from the properties instance
                 String value = properties.getProperty(option);
 

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionGroupTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionGroupTest.java?rev=955501&r1=955500&r2=955501&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionGroupTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/OptionGroupTest.java Thu
Jun 17 07:57:38 2010
@@ -17,6 +17,8 @@
 
 package org.apache.commons.cli;
 
+import java.util.Properties;
+
 import junit.framework.TestCase;
 
 /**
@@ -26,7 +28,7 @@ import junit.framework.TestCase;
 public class OptionGroupTest extends TestCase
 {
     private Options _options = null;
-    private CommandLineParser parser = new PosixParser();
+    private Parser parser = new PosixParser();
 
     public void setUp()
     {
@@ -186,6 +188,18 @@ public class OptionGroupTest extends Tes
         assertTrue( "Confirm NO extra args", cl.getArgList().size() == 0);
     }
 
+    public void testTwoOptionsFromGroupWithProperties() throws Exception
+    {
+        String[] args = new String[] { "-f" };
+        
+        Properties properties = new Properties();
+        properties.put("d", "true");
+        
+        CommandLine cl = parser.parse( _options, args, properties);
+        assertTrue(cl.hasOption("f"));
+        assertTrue(!cl.hasOption("d"));
+    }
+
     public void testValidLongOnlyOptions() throws Exception
     {
         CommandLine cl1 = parser.parse(_options, new String[]{"--export"});

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java?rev=955501&r1=955500&r2=955501&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/ParserTestCase.java Thu
Jun 17 07:57:38 2010
@@ -998,4 +998,47 @@ public abstract class ParserTestCase ext
         CommandLine cmd = parse(parser, opts, null, properties);
         assertTrue(cmd.hasOption("f"));
     }
+
+    public void testPropertyOptionUnexpected() throws Exception
+    {
+        Options opts = new Options();
+        
+        Properties properties = new Properties();
+        properties.setProperty("f", "true");
+        
+        try {
+            parse(parser, opts, null, properties);
+            fail("UnrecognizedOptionException expected");
+        } catch (UnrecognizedOptionException e) {
+            // expected
+        }
+    }
+
+    public void testPropertyOptionGroup() throws Exception
+    {
+        Options opts = new Options();
+        
+        OptionGroup group1 = new OptionGroup();
+        group1.addOption(new Option("a", null));
+        group1.addOption(new Option("b", null));
+        opts.addOptionGroup(group1);
+        
+        OptionGroup group2 = new OptionGroup();
+        group2.addOption(new Option("x", null));
+        group2.addOption(new Option("y", null));
+        opts.addOptionGroup(group2);
+        
+        String[] args = new String[] { "-a" };
+        
+        Properties properties = new Properties();
+        properties.put("b", "true");
+        properties.put("x", "true");
+        
+        CommandLine cmd = parse(parser, opts, args, properties);
+        
+        assertTrue(cmd.hasOption("a"));
+        assertFalse(cmd.hasOption("b"));
+        assertTrue(cmd.hasOption("x"));
+        assertFalse(cmd.hasOption("y"));
+    }
 }



Mime
View raw message