commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r955099 - in /commons/proper/cli/trunk/src: java/org/apache/commons/cli/DefaultParser.java test/org/apache/commons/cli/ParserTestCase.java test/org/apache/commons/cli/ValueTest.java
Date Wed, 16 Jun 2010 00:14:27 GMT
Author: ebourg
Date: Wed Jun 16 00:14:26 2010
New Revision: 955099

URL: http://svn.apache.org/viewvc?rev=955099&view=rev
Log:
Added the missing parse methods accepting a map with the default options to DefaultParser
Moved the related tests from ValueTest to ParserTestCase

Modified:
    commons/proper/cli/trunk/src/java/org/apache/commons/cli/DefaultParser.java
    commons/proper/cli/trunk/src/test/org/apache/commons/cli/ParserTestCase.java
    commons/proper/cli/trunk/src/test/org/apache/commons/cli/ValueTest.java

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli/DefaultParser.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli/DefaultParser.java?rev=955099&r1=955098&r2=955099&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli/DefaultParser.java (original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli/DefaultParser.java Wed Jun 16
00:14:26 2010
@@ -18,8 +18,10 @@
 package org.apache.commons.cli;
 
 import java.util.ArrayList;
-import java.util.List;
+import java.util.Enumeration;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Properties;
 
 /**
  * Default parser.
@@ -54,11 +56,47 @@ public class DefaultParser implements Co
     
     public CommandLine parse(Options options, String[] arguments) throws ParseException
     {
-        return parse(options, arguments, false);
+        return parse(options, arguments, null);
+    }
+
+    /**
+     * Parse the arguments according to the specified options and properties.
+     *
+     * @param options    the specified Options
+     * @param arguments  the command line arguments
+     * @param properties command line option name-value pairs
+     * @return the list of atomic option and value tokens
+     *
+     * @throws ParseException if there are any problems encountered
+     * while parsing the command line tokens.
+     */
+    public CommandLine parse(Options options, String[] arguments, Properties properties)
throws ParseException
+    {
+        return parse(options, arguments, properties, false);
     }
 
     public CommandLine parse(Options options, String[] arguments, boolean stopAtNonOption)
throws ParseException
     {
+        return parse(options, arguments, null, stopAtNonOption);
+    }
+
+    /**
+     * Parse the arguments according to the specified options and properties.
+     *
+     * @param options         the specified Options
+     * @param arguments       the command line arguments
+     * @param properties      command line option name-value pairs
+     * @param stopAtNonOption if <tt>true</tt> an unrecognized argument stops
+     *     the parsing and the remaining arguments are added to the 
+     *     {@link CommandLine}s args list. If <tt>false</tt> an unrecognized
+     *     argument triggers a ParseException.
+     *
+     * @return the list of atomic option and value tokens
+     * @throws ParseException if there are any problems encountered
+     * while parsing the command line tokens.
+     */
+    public CommandLine parse(Options options, String[] arguments, Properties properties,
boolean stopAtNonOption) throws ParseException
+    {
         this.options = options;
         this.stopAtNonOption = stopAtNonOption;
         skipParsing = false;
@@ -85,12 +123,58 @@ public class DefaultParser implements Co
         // check the arguments of the last option
         checkRequiredArgs();
         
+        // add the default options
+        handleProperties(properties);
+        
         checkRequiredOptions();
         
         return cmd;
     }
 
     /**
+     * Sets the values of Options using the values in <code>properties</code>.
+     *
+     * @param properties The value properties to be processed.
+     */
+    private void handleProperties(Properties properties)
+    {
+        if (properties == null)
+        {
+            return;
+        }
+        
+        for (Enumeration e = properties.propertyNames(); e.hasMoreElements();)
+        {
+            String option = e.nextElement().toString();
+            
+            if (!cmd.hasOption(option))
+            {
+                Option opt = options.getOption(option);
+                
+                // get the value from the properties
+                String value = properties.getProperty(option);
+                
+                if (opt.hasArg())
+                {
+                    if (opt.getValues() == null || opt.getValues().length == 0)
+                    {
+                        opt.addValueForProcessing(value);
+                    }
+                }
+                else if (!("yes".equalsIgnoreCase(value)
+                        || "true".equalsIgnoreCase(value)
+                        || "1".equalsIgnoreCase(value)))
+                {
+                    // if the value is not yes, true or 1 then don't add the option to the
CommandLine
+                    continue;
+                }
+                
+                cmd.addOption(opt);
+            }
+        }
+    }
+
+    /**
      * Throws a {@link MissingOptionException} if all of the required options
      * are not present.
      *

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=955099&r1=955098&r2=955099&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 Wed Jun 16
00:14:26 2010
@@ -869,4 +869,121 @@ public abstract class ParserTestCase ext
         assertTrue("Confirm -f is set", cl.hasOption("f"));
         assertEquals("number of arg for -f", 1, cl.getOptionValues("f").length);
     }
+
+    private CommandLine parse(CommandLineParser parser, Options opts, String[] args, Properties
properties) throws ParseException {
+        if (parser instanceof Parser) {
+            return ((Parser) parser).parse(opts, args, properties);
+        } else if (parser instanceof DefaultParser) {
+            return ((DefaultParser) parser).parse(opts, args, properties);
+        } else {
+            throw new UnsupportedOperationException("Default options not supported by this
parser");
+        }
+    }
+
+    public void testPropertyOptionSingularValue() throws Exception
+    {
+        Options opts = new Options();
+        opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt("hide").create());  
     
+        
+        Properties properties = new Properties();
+        properties.setProperty( "hide", "seek" );
+
+        CommandLine cmd = parse(parser, opts, null, properties);
+        assertTrue( cmd.hasOption("hide") );
+        assertEquals( "seek", cmd.getOptionValue("hide") );
+        assertTrue( !cmd.hasOption("fake") );
+    }
+
+    public void testPropertyOptionFlags() throws Exception
+    {
+        Options opts = new Options();
+        opts.addOption("a", false, "toggle -a");
+        opts.addOption("c", "c", false, "toggle -c");
+        opts.addOption(OptionBuilder.hasOptionalArg().create('e'));
+        
+        Properties properties = new Properties();
+        properties.setProperty("a", "true");
+        properties.setProperty("c", "yes");
+        properties.setProperty("e", "1");
+        
+        CommandLine cmd = parse(parser, opts, null, properties);
+        assertTrue(cmd.hasOption("a"));
+        assertTrue(cmd.hasOption("c"));
+        assertTrue(cmd.hasOption("e"));
+        
+        
+        properties = new Properties();
+        properties.setProperty("a", "false");
+        properties.setProperty("c", "no");
+        properties.setProperty("e", "0");
+        
+        cmd = parse(parser, opts, null, properties);
+        assertTrue(!cmd.hasOption("a"));
+        assertTrue(!cmd.hasOption("c"));
+        assertTrue(cmd.hasOption("e")); // this option accepts an argument
+        
+        
+        properties = new Properties();
+        properties.setProperty("a", "TRUE");
+        properties.setProperty("c", "nO");
+        properties.setProperty("e", "TrUe");
+        
+        cmd = parse(parser, opts, null, properties);
+        assertTrue(cmd.hasOption("a"));
+        assertTrue(!cmd.hasOption("c"));
+        assertTrue(cmd.hasOption("e"));
+        
+        
+        properties = new Properties();
+        properties.setProperty("a", "just a string");
+        properties.setProperty("e", "");
+        
+        cmd = parse(parser, opts, null, properties);
+        assertTrue(!cmd.hasOption("a"));
+        assertTrue(!cmd.hasOption("c"));
+        assertTrue(cmd.hasOption("e"));
+        
+        
+        properties = new Properties();
+        properties.setProperty("a", "0");
+        properties.setProperty("c", "1");
+        
+        cmd = parse(parser, opts, null, properties);
+        assertTrue(!cmd.hasOption("a"));
+        assertTrue(cmd.hasOption("c"));
+    } 
+
+    public void testPropertyOptionMultipleValues() throws Exception
+    {
+        Options opts = new Options();
+        opts.addOption(OptionBuilder.hasArgs().withValueSeparator(',').create('k'));
+        
+        Properties properties = new Properties();
+        properties.setProperty( "k", "one,two" );
+
+        String[] values = new String[] { "one", "two" };
+
+        CommandLine cmd = parse(parser, opts, null, properties);
+        assertTrue( cmd.hasOption("k") );
+        assertTrue( Arrays.equals( values, cmd.getOptionValues('k') ) );
+    }
+
+    public void testPropertyOverrideValues() throws Exception
+    {
+        Options opts = new Options();
+        opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
+        opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
+        
+        String[] args = new String[] { "-j", "found", "-i", "ink" };
+
+        Properties properties = new Properties();
+        properties.setProperty( "j", "seek" );
+
+        CommandLine cmd = parse(parser, opts, args, properties);
+        assertTrue( cmd.hasOption("j") );
+        assertEquals( "found", cmd.getOptionValue("j") );
+        assertTrue( cmd.hasOption("i") );
+        assertEquals( "ink", cmd.getOptionValue("i") );
+        assertTrue( !cmd.hasOption("fake") );
+    }
 }

Modified: commons/proper/cli/trunk/src/test/org/apache/commons/cli/ValueTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli/ValueTest.java?rev=955099&r1=955098&r2=955099&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli/ValueTest.java (original)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli/ValueTest.java Wed Jun 16 00:14:26
2010
@@ -17,9 +17,6 @@
 
 package org.apache.commons.cli;
 
-import java.util.Arrays;
-import java.util.Properties;
-
 import junit.framework.TestCase;
 
 public class ValueTest extends TestCase
@@ -40,7 +37,6 @@ public class ValueTest extends TestCase
         opts.addOption(OptionBuilder.hasOptionalArgs(2).withLongOpt("hide").create());
         opts.addOption(OptionBuilder.hasOptionalArgs(2).create('i'));
         opts.addOption(OptionBuilder.hasOptionalArgs().create('j'));
-        opts.addOption(OptionBuilder.hasArgs().withValueSeparator(',').create('k'));
 
         String[] args = new String[] { "-a",
             "-b", "foo",
@@ -175,102 +171,4 @@ public class ValueTest extends TestCase
         assertEquals( cmd.getArgs().length, 1 );
         assertEquals( "head", cmd.getArgs()[0] );
     }
-
-    public void testPropertyOptionSingularValue() throws Exception
-    {
-        Properties properties = new Properties();
-        properties.setProperty( "hide", "seek" );
-
-        Parser parser = new PosixParser();
-        
-        CommandLine cmd = parser.parse(opts, null, properties);
-        assertTrue( cmd.hasOption("hide") );
-        assertEquals( "seek", cmd.getOptionValue("hide") );
-        assertTrue( !cmd.hasOption("fake") );
-    }
-
-    public void testPropertyOptionFlags() throws Exception
-    {
-        Properties properties = new Properties();
-        properties.setProperty( "a", "true" );
-        properties.setProperty( "c", "yes" );
-        properties.setProperty( "e", "1" );
-
-        Parser parser = new PosixParser();
-
-        CommandLine cmd = parser.parse(opts, null, properties);
-        assertTrue( cmd.hasOption("a") );
-        assertTrue( cmd.hasOption("c") );
-        assertTrue( cmd.hasOption("e") );
-
-
-        properties = new Properties();
-        properties.setProperty( "a", "false" );
-        properties.setProperty( "c", "no" );
-        properties.setProperty( "e", "0" );
-
-        cmd = parser.parse(opts, null, properties);
-        assertTrue( !cmd.hasOption("a") );
-        assertTrue( !cmd.hasOption("c") );
-        assertTrue( cmd.hasOption("e") ); // this option accepts as argument
-
-
-        properties = new Properties();
-        properties.setProperty( "a", "TRUE" );
-        properties.setProperty( "c", "nO" );
-        properties.setProperty( "e", "TrUe" );
-
-        cmd = parser.parse(opts, null, properties);
-        assertTrue( cmd.hasOption("a") );
-        assertTrue( !cmd.hasOption("c") );
-        assertTrue( cmd.hasOption("e") );
-
-        
-        properties = new Properties();
-        properties.setProperty( "a", "just a string" );
-        properties.setProperty( "e", "" );
-
-        cmd = parser.parse(opts, null, properties);
-        assertTrue( !cmd.hasOption("a") );
-        assertTrue( !cmd.hasOption("c") );
-        assertTrue( cmd.hasOption("e") );
-    } 
-
-    public void testPropertyOptionMultipleValues() throws Exception
-    {
-        Properties properties = new Properties();
-        properties.setProperty( "k", "one,two" );
-
-        Parser parser = new PosixParser();
-        
-        String[] values = new String[] {
-            "one", "two"
-        };
-
-        CommandLine cmd = parser.parse(opts, null, properties);
-        assertTrue( cmd.hasOption("k") );
-        assertTrue( Arrays.equals( values, cmd.getOptionValues('k') ) );
-    }
-
-    public void testPropertyOverrideValues() throws Exception
-    {
-        String[] args = new String[] { 
-            "-j",
-            "found",
-            "-i",
-            "ink"
-        };
-
-        Properties properties = new Properties();
-        properties.setProperty( "j", "seek" );
-
-        Parser parser = new PosixParser();
-        CommandLine cmd = parser.parse(opts, args, properties);
-        assertTrue( cmd.hasOption("j") );
-        assertEquals( "found", cmd.getOptionValue("j") );
-        assertTrue( cmd.hasOption("i") );
-        assertEquals( "ink", cmd.getOptionValue("i") );
-        assertTrue( !cmd.hasOption("fake") );
-    }
-
 }



Mime
View raw message