commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1749596 - in /commons/proper/cli/trunk/src: changes/changes.xml main/java/org/apache/commons/cli/OptionGroup.java main/java/org/apache/commons/cli/Options.java test/java/org/apache/commons/cli/bug/BugCLI266Test.java
Date Tue, 21 Jun 2016 20:27:06 GMT
Author: britter
Date: Tue Jun 21 20:27:06 2016
New Revision: 1749596

URL: http://svn.apache.org/viewvc?rev=1749596&view=rev
Log:
CLI-266: HelpFormatter.setOptionComparator(null) doesn't display the values in inserted order.
Thank you to Ravi Teja. This also closes #6 from GitHub.

Added:
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI266Test.java
Modified:
    commons/proper/cli/trunk/src/changes/changes.xml
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionGroup.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Options.java

Modified: commons/proper/cli/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/changes/changes.xml?rev=1749596&r1=1749595&r2=1749596&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/changes/changes.xml (original)
+++ commons/proper/cli/trunk/src/changes/changes.xml Tue Jun 21 20:27:06 2016
@@ -23,6 +23,9 @@
   <body>
 
     <release version="1.4" date="tba" description="tba">
+      <action type="fix" dev="britter" issue="CLI-266" due-to="Ravi Teja">
+        HelpFormatter.setOptionComparator(null) doesn't display the values in inserted order
+      </action>
     </release>
 
     <release version="1.3.1" date="2015-06-17" description="Bug fix release for 1.3">

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionGroup.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionGroup.java?rev=1749596&r1=1749595&r2=1749596&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionGroup.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/OptionGroup.java Tue Jun
21 20:27:06 2016
@@ -19,8 +19,8 @@ package org.apache.commons.cli;
 
 import java.io.Serializable;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.Map;
 
 /**
@@ -34,7 +34,7 @@ public class OptionGroup implements Seri
     private static final long serialVersionUID = 1L;
     
     /** hold the options */
-    private final Map<String, Option> optionMap = new HashMap<String, Option>();
+    private final Map<String, Option> optionMap = new LinkedHashMap<String, Option>();
 
     /** the name of the selected option */
     private String selected;

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Options.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Options.java?rev=1749596&r1=1749595&r2=1749596&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Options.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Options.java Tue Jun 21
20:27:06 2016
@@ -21,7 +21,6 @@ import java.io.Serializable;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -58,7 +57,7 @@ public class Options implements Serializ
     private final List<Object> requiredOpts = new ArrayList<Object>();
 
     /** a map of the option groups */
-    private final Map<String, OptionGroup> optionGroups = new HashMap<String, OptionGroup>();
+    private final Map<String, OptionGroup> optionGroups = new LinkedHashMap<String,
OptionGroup>();
 
     /**
      * Add the specified option group.

Added: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI266Test.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI266Test.java?rev=1749596&view=auto
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI266Test.java (added)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/bug/BugCLI266Test.java Tue
Jun 21 20:27:06 2016
@@ -0,0 +1,104 @@
+package org.apache.commons.cli.bug;
+
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionGroup;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+public class BugCLI266Test {
+
+    private List<String> insertedOrder  =   Arrays.asList("h", "d", "f", "x", "s",
"p", "t", "w", "o");
+    private List<String> sortOrder      =   Arrays.asList("d", "f", "h", "o", "p",
"s", "t", "w", "x");
+
+    @Test
+    public void testOptionComparatorDefaultOrder() throws ParseException {
+        HelpFormatter formatter = new HelpFormatter();
+        List<Option> options = new ArrayList<Option>(getOptions().getOptions());
+        Collections.sort(options, formatter.getOptionComparator());
+        int i = 0;
+        for(Option o: options) {
+            Assert.assertEquals(o.getOpt(), sortOrder.get(i));
+            i++;
+        }
+    }
+
+    @Test
+    public void testOptionComparatorInsertedOrder() throws ParseException {
+        Collection<Option> options = getOptions().getOptions();
+        int i = 0;
+        for(Option o: options) {
+            Assert.assertEquals(o.getOpt(), insertedOrder.get(i));
+            i++;
+        }
+    }
+
+    private Options getOptions() {
+        Options options = new Options();
+        Option help = Option.builder("h")
+                .longOpt("help")
+                .desc("Prints this help message")
+                .build();
+        options.addOption(help);
+
+        buildOptionsGroup(options);
+
+        Option t = Option.builder("t")
+                .required()
+                .hasArg()
+                .argName("file")
+                .build();
+        Option w = Option.builder("w")
+                .required()
+                .hasArg()
+                .argName("word")
+                .build();
+        Option o = Option.builder("o")
+                .hasArg()
+                .argName("directory")
+                .build();
+        options.addOption(t);
+        options.addOption(w);
+        options.addOption(o);
+        return options;
+    }
+
+    private void buildOptionsGroup(Options options) {
+        OptionGroup firstGroup = new OptionGroup();
+        OptionGroup secondGroup = new OptionGroup();
+        firstGroup.setRequired(true);
+        secondGroup.setRequired(true);
+
+        firstGroup.addOption(Option.builder("d")
+                .longOpt("db")
+                .hasArg()
+                .argName("table-name")
+                .build());
+        firstGroup.addOption(Option.builder("f")
+                .longOpt("flat-file")
+                .hasArg()
+                .argName("input.csv")
+                .build());
+        options.addOptionGroup(firstGroup);
+
+        secondGroup.addOption(Option.builder("x")
+                .hasArg()
+                .argName("arg1")
+                .build());
+        secondGroup.addOption(Option.builder("s")
+                .build());
+        secondGroup.addOption(Option.builder("p")
+                .hasArg()
+                .argName("arg1")
+                .build());
+        options.addOptionGroup(secondGroup);
+    }
+}



Mime
View raw message