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/06/13 19:03:31 UTC

svn commit: r667595 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/CommandLine.java test/org/apache/commons/cli/CommandLineTest.java

Author: ebourg
Date: Fri Jun 13 10:03:31 2008
New Revision: 667595

URL: http://svn.apache.org/viewvc?rev=667595&view=rev
Log:
Added the method getOptionProperties() in CommandLine to retrieve easily a set of properties specified by an option (-Dparam=value)

Added:
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java   (with props)
Modified:
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java?rev=667595&r1=667594&r2=667595&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java (original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/CommandLine.java Fri Jun 13 10:03:31 2008
@@ -22,6 +22,7 @@
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Properties;
 
 /** 
  * <p>Represents list of arguments parsed against
@@ -228,6 +229,45 @@
         return getOptionValue(String.valueOf(opt), defaultValue);
     }
 
+    /**
+     * Retrieve the map of values associated to the option. This is convenient
+     * for options specifying Java properties like <tt>-Dparam1=value1
+     * -Dparam2=value2</tt>. The first argument of the option is the key, and
+     * the 2nd argument is the value. If the option has only one argument
+     * (<tt>-Dfoo</tt>) it is considered as a boolean flag and the value is
+     * <tt>"true"</tt>.
+     *
+     * @param opt name of the option
+     * @return The Properties mapped by the option, never <tt>null</tt>
+     *         even if the option doesn't exists
+     */
+    public Properties getOptionProperties(String opt)
+    {
+        Properties props = new Properties();
+
+        for (Iterator it = options.iterator(); it.hasNext();)
+        {
+            Option option = (Option) it.next();
+
+            if (opt.equals(option.getOpt()) || opt.equals(option.getLongOpt()))
+            {
+                List values = option.getValuesList();
+                if (values.size() >= 2)
+                {
+                    // use the first 2 arguments as the key/value pair
+                    props.put(values.get(0), values.get(1));
+                }
+                else if (values.size() == 1)
+                {
+                    // no explicit value, handle it as a boolean
+                    props.put(values.get(0), "true");
+                }                
+            }
+        }
+
+        return props;
+    }
+
     /** 
      * Retrieve any left-over non-recognized options and arguments
      *

Added: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java?rev=667595&view=auto
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java (added)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java Fri Jun 13 10:03:31 2008
@@ -0,0 +1,51 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.cli;
+
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Emmanuel Bourg
+ * @version $Revision$, $Date$
+ */
+public class CommandLineTest extends TestCase
+{
+    public void testGetOptionProperties() throws Exception
+    {
+        String[] args = new String[] { "-Dparam1=value1", "-Dparam2=value2", "-Dparam3", "-Dparam4=value4", "-D", "--property", "foo=bar" };
+
+        Options options = new Options();
+        options.addOption(OptionBuilder.withValueSeparator().hasOptionalArgs(2).create('D'));
+        options.addOption(OptionBuilder.withValueSeparator().hasArgs(2).withLongOpt("property").create());
+
+        Parser parser = new GnuParser();
+        CommandLine cl = parser.parse(options, args);
+
+        Properties props = cl.getOptionProperties("D");
+        assertNotNull("null properties", props);
+        assertEquals("number of properties in " + props, 4, props.size());
+        assertEquals("property 1", "value1", props.getProperty("param1"));
+        assertEquals("property 2", "value2", props.getProperty("param2"));
+        assertEquals("property 3", "true", props.getProperty("param3"));
+        assertEquals("property 4", "value4", props.getProperty("param4"));
+
+        assertEquals("property with long format", "bar", cl.getOptionProperties("property").getProperty("foo"));
+    }
+}

Propchange: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/CommandLineTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL