commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r679511 - in /commons/proper/cli/trunk/src: java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java test/org/apache/commons/cli2/bug/BugCLI158Test.java
Date Thu, 24 Jul 2008 19:38:04 GMT
Author: oheger
Date: Thu Jul 24 12:38:04 2008
New Revision: 679511

URL: http://svn.apache.org/viewvc?rev=679511&view=rev
Log:
CLI-158: Default arguments are now applied even if arguments are passed. The default values
for missing arguments will be set.
Thanks to tobias dot bocanegra at day dot com for the patch.

Added:
    commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java   (with
props)
Modified:
    commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java

Modified: commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java?rev=679511&r1=679510&r2=679511&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
(original)
+++ commons/proper/cli/trunk/src/java/org/apache/commons/cli2/commandline/WriteableCommandLineImpl.java
Thu Jul 24 12:38:04 2008
@@ -109,26 +109,33 @@
     }
 
     public List getValues(final Option option,
-                          final List defaultValues) {
-        // First grab the command line values
+                          List defaultValues) {
+        // initialize the return list
         List valueList = (List) values.get(option);
 
-        // Secondly try the defaults supplied to the method
-        if ((valueList == null) || valueList.isEmpty()) {
-            valueList = defaultValues;
-        }
-
-        // Thirdly try the option's default values
-        if ((valueList == null) || valueList.isEmpty()) {
-            valueList = (List) this.defaultValues.get(option);
+        // grab the correct default values
+        if (defaultValues == null || defaultValues.isEmpty()) {
+            defaultValues = (List) this.defaultValues.get(option);
         }
 
-        // Finally use an empty list
-        if (valueList == null) {
-            valueList = Collections.EMPTY_LIST;
+        // augment the list with the default values
+        if (defaultValues != null && !defaultValues.isEmpty()) {
+            if (valueList == null || valueList.isEmpty()) {
+                valueList = defaultValues;
+            } else {
+                // if there are more default values as specified, add them to
+                // the list.
+                if (defaultValues.size() > valueList.size()) {
+                    // copy the list first
+                    valueList = new ArrayList(valueList);
+                    for (int i=valueList.size(); i<defaultValues.size(); i++) {
+                        valueList.add(defaultValues.get(i));
+                    }
+                }
+            }
         }
-
-        return valueList;
+        
+        return valueList == null ? Collections.EMPTY_LIST : valueList;
     }
 
     public List getUndefaultedValues(Option option) {

Added: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java?rev=679511&view=auto
==============================================================================
--- commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java (added)
+++ commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java Thu Jul
24 12:38:04 2008
@@ -0,0 +1,114 @@
+/**
+ * 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.cli2.bug;
+
+import java.util.Arrays;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.cli2.CommandLine;
+import org.apache.commons.cli2.Group;
+import org.apache.commons.cli2.builder.ArgumentBuilder;
+import org.apache.commons.cli2.builder.DefaultOptionBuilder;
+import org.apache.commons.cli2.builder.GroupBuilder;
+import org.apache.commons.cli2.commandline.Parser;
+import org.apache.commons.cli2.option.DefaultOption;
+
+/**
+ * http://issues.apache.org/jira/browse/CLI-158
+ */
+public class BugCLI158Test extends TestCase {
+
+    private Parser createDefaultValueParser(String[] defaults) {
+        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+        final ArgumentBuilder abuilder = new ArgumentBuilder();
+        final GroupBuilder gbuilder = new GroupBuilder();
+
+        DefaultOption bOption = obuilder.withShortName("b")
+                .withLongName("b")
+                .withArgument(abuilder.withName("b")
+                        .withMinimum(0)
+                        .withMaximum(defaults.length)
+                        .withDefaults(Arrays.asList(defaults))
+                        .create())
+                .create();
+
+        Group options = gbuilder
+                .withName("options")
+                .withOption(bOption)
+                .create();
+
+        Parser parser = new Parser();
+        parser.setHelpTrigger("--help");
+        parser.setGroup(options);
+        return parser;
+    }
+
+    public void testSingleOptionSingleArgument() throws Exception {
+        Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
+        String enteredValue1 = "1";
+        String[] args = new String[]{"-b", enteredValue1};
+        CommandLine cl = parser.parse(args);
+        CommandLine cmd = cl;
+        assertNotNull(cmd);
+        List b = cmd.getValues("-b");
+        assertEquals("[" + enteredValue1 + ", 1000]", b + "");
+    }
+
+    public void testSingleOptionNoArgument() throws Exception {
+        Parser parser = createDefaultValueParser(new String[]{"100", "1000"});
+        String[] args = new String[]{"-b"};
+        CommandLine cl = parser.parse(args);
+        CommandLine cmd = cl;
+        assertNotNull(cmd);
+        List b = cmd.getValues("-b");
+        assertEquals("[100, 1000]", b + "");
+    }
+
+    public void testSingleOptionMaximumNumberOfArgument() throws Exception {
+        String[] args = new String[]{"-b", "1", "2"};
+        final ArgumentBuilder abuilder = new ArgumentBuilder();
+        final DefaultOptionBuilder obuilder = new DefaultOptionBuilder();
+        final GroupBuilder gbuilder = new GroupBuilder();
+
+        DefaultOption bOption = obuilder.withShortName("b")
+                .withLongName("b")
+                .withArgument(abuilder.withName("b")
+                        .withMinimum(2)
+                        .withMaximum(4)
+                        .withDefault("100")
+                        .withDefault("1000")
+                        .withDefault("10000")
+                        .create())
+                .create();
+
+        Group options = gbuilder
+                .withName("options")
+                .withOption(bOption)
+                .create();
+
+        Parser parser = new Parser();
+        parser.setHelpTrigger("--help");
+        parser.setGroup(options);
+        CommandLine cl = parser.parse(args);
+        CommandLine cmd = cl;
+        assertNotNull(cmd);
+        List b = cmd.getValues("-b");
+        assertEquals("[1, 2, 10000]", b + "");
+    }
+}
\ No newline at end of file

Propchange: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/proper/cli/trunk/src/test/org/apache/commons/cli2/bug/BugCLI158Test.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message