commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ebo...@apache.org
Subject svn commit: r1091575 - in /commons/proper/cli/trunk/src: main/java/org/apache/commons/cli/HelpFormatter.java main/java/org/apache/commons/cli/Options.java test/java/org/apache/commons/cli/HelpFormatterTest.java
Date Tue, 12 Apr 2011 21:17:52 GMT
Author: ebourg
Date: Tue Apr 12 21:17:52 2011
New Revision: 1091575

URL: http://svn.apache.org/viewvc?rev=1091575&view=rev
Log:
HelpFormatter now accepts a null comparator to preserve the declaration order of the options
(CLI-212)

Modified:
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
    commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/Options.java
    commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java

Modified: commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java?rev=1091575&r1=1091574&r2=1091575&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java (original)
+++ commons/proper/cli/trunk/src/main/java/org/apache/commons/cli/HelpFormatter.java Tue Apr
12 21:17:52 2011
@@ -341,20 +341,13 @@ public class HelpFormatter
 
     /**
      * Set the comparator used to sort the options when they output in help text.
-     * Passing in a null parameter will set the ordering to the default mode.
+     * Passing in a null comparator will keep the options in the order they were declared.
      * 
      * @since 1.2
      */
     public void setOptionComparator(Comparator<Option> comparator)
     {
-        if (comparator == null)
-        {
-            this.optionComparator = new OptionComparator();
-        }
-        else
-        {
-            this.optionComparator = comparator;
-        }
+        this.optionComparator = comparator;
     }
 
     /**
@@ -546,17 +539,17 @@ public class HelpFormatter
         // create a list for processed option groups
         final Collection<OptionGroup> processedGroups = new ArrayList<OptionGroup>();
 
-        // temp variable
-        Option option;
-
         List<Option> optList = new ArrayList<Option>(options.getOptions());
-        Collections.sort(optList, getOptionComparator());
+        if (getOptionComparator() != null)
+        {
+            Collections.sort(optList, getOptionComparator());
+        }
         // iterate over the options
         for (Iterator i = optList.iterator(); i.hasNext();)
         {
             // get the next Option
-            option = (Option) i.next();
-
+            Option option = (Option) i.next();
+            
             // check if the option is part of an OptionGroup
             OptionGroup group = options.getOptionGroup(option);
 
@@ -611,7 +604,10 @@ public class HelpFormatter
         }
 
         List<Option> optList = new ArrayList<Option>(group.getOptions());
-        Collections.sort(optList, getOptionComparator());
+        if (getOptionComparator() != null)
+        {
+            Collections.sort(optList, getOptionComparator());
+        }
         // for each option in the OptionGroup
         for (Iterator i = optList.iterator(); i.hasNext();)
         {
@@ -757,16 +753,18 @@ public class HelpFormatter
         // the longest opt string this list will be then used to 
         // sort options ascending
         int max = 0;
-        StringBuffer optBuf;
         List<StringBuffer> prefixList = new ArrayList<StringBuffer>();
         
         List<Option> optList = options.helpOptions();
         
-        Collections.sort(optList, getOptionComparator());
+        if (getOptionComparator() != null)
+        {
+            Collections.sort(optList, getOptionComparator());
+        }
         
         for (Option option : optList)
         {
-            optBuf = new StringBuffer();
+            StringBuffer optBuf = new StringBuffer();
             
             if (option.getOpt() == null)
             {
@@ -806,7 +804,7 @@ public class HelpFormatter
         for (Iterator i = optList.iterator(); i.hasNext();)
         {
             Option option = (Option) i.next();
-            optBuf = new StringBuffer(prefixList.get(x++).toString());
+            StringBuffer optBuf = new StringBuffer(prefixList.get(x++).toString());
 
             if (optBuf.length() < max)
             {

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=1091575&r1=1091574&r2=1091575&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 Apr 12
21:17:52 2011
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -49,10 +50,10 @@ public class Options implements Serializ
     private static final long serialVersionUID = 1L;
 
     /** a map of the options with the character key */
-    private Map<String, Option> shortOpts = new HashMap<String, Option>();
+    private Map<String, Option> shortOpts = new LinkedHashMap<String, Option>();
 
     /** a map of the options with the long key */
-    private Map<String, Option> longOpts = new HashMap<String, Option>();
+    private Map<String, Option> longOpts = new LinkedHashMap<String, Option>();
 
     /** a map of the required options */
     private List<Object> requiredOpts = new ArrayList<Object>();

Modified: commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java?rev=1091575&r1=1091574&r2=1091575&view=diff
==============================================================================
--- commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java (original)
+++ commons/proper/cli/trunk/src/test/java/org/apache/commons/cli/HelpFormatterTest.java Tue
Apr 12 21:17:52 2011
@@ -269,13 +269,11 @@ public class HelpFormatterTest extends T
         opts.addOption(new Option("c", "third"));
 
         HelpFormatter helpFormatter = new HelpFormatter();
-        helpFormatter.setOptionComparator(new Comparator()
+        helpFormatter.setOptionComparator(new Comparator<Option>()
         {
-            public int compare(Object o1, Object o2)
+            public int compare(Option opt1, Option opt2)
             {
                 // reverses the fuctionality of the default comparator
-                Option opt1 = (Option) o1;
-                Option opt2 = (Option) o2;
                 return opt2.getKey().compareToIgnoreCase(opt1.getKey());
             }
         });
@@ -289,9 +287,9 @@ public class HelpFormatterTest extends T
     public void testPrintSortedUsageWithNullComparator()
     {
         Options opts = new Options();
-        opts.addOption(new Option("a", "first"));
+        opts.addOption(new Option("c", "first"));
         opts.addOption(new Option("b", "second"));
-        opts.addOption(new Option("c", "third"));
+        opts.addOption(new Option("a", "third"));
 
         HelpFormatter helpFormatter = new HelpFormatter();
         helpFormatter.setOptionComparator(null);
@@ -299,7 +297,7 @@ public class HelpFormatterTest extends T
         StringWriter out = new StringWriter();
         helpFormatter.printUsage(new PrintWriter(out), 80, "app", opts);
 
-        assertEquals("usage: app [-a] [-b] [-c]" + EOL, out.toString());
+        assertEquals("usage: app [-c] [-b] [-a]" + EOL, out.toString());
     }
 
     public void testPrintOptionGroupUsage()



Mime
View raw message