commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r655735 - in /commons/proper/cli/branches/cli-1.x/src: java/org/apache/commons/cli/HelpFormatter.java test/org/apache/commons/cli/HelpFormatterTest.java
Date Tue, 13 May 2008 04:55:00 GMT
Author: bayard
Date: Mon May 12 21:54:59 2008
New Revision: 655735

URL: http://svn.apache.org/viewvc?rev=655735&view=rev
Log:
Making the OptionComparator modifiable so people can change the order in which arguments are
printed by the HelpFormatter - as requested in CLI-155

Modified:
    commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java
    commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java

Modified: commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java?rev=655735&r1=655734&r2=655735&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/java/org/apache/commons/cli/HelpFormatter.java
Mon May 12 21:54:59 2008
@@ -126,6 +126,13 @@
     public String defaultArgName = DEFAULT_ARG_NAME;
 
     /**
+     * Comparator used to sort the options when they output in help text
+     * 
+     * Defaults to case-insensitive alphabetical sorting by option key
+     */
+    protected Comparator optionComparator = new OptionComparator();
+
+    /**
      * Sets the 'width'.
      *
      * @param width the new value of 'width'
@@ -285,6 +292,33 @@
         return this.defaultArgName;
     }
 
+    /**
+     * Comparator used to sort the options when they output in help text
+     * 
+     * Defaults to case-insensitive alphabetical sorting by option key
+     */
+    public Comparator getOptionComparator() 
+    {
+        return this.optionComparator;
+    }
+
+    /**
+     * 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
+     */
+    public void setOptionComparator(Comparator comparator) 
+    {
+        if ( comparator == null ) 
+        {
+            this.optionComparator = new OptionComparator();
+        } 
+        else 
+        {
+            this.optionComparator = comparator;
+        }
+    }
+
 
     // ------------------------------------------------------------------ Public
 
@@ -487,7 +521,7 @@
         Option option;
 
         List optList = new ArrayList(options.getOptions());
-        Collections.sort(optList, new OptionComparator());
+        Collections.sort(optList, getOptionComparator() );
         // iterate over the options
         for (Iterator i = optList.iterator(); i.hasNext();)
         {
@@ -541,7 +575,7 @@
      * @param group the group to append
      * @see #appendOption(StringBuffer,Option,boolean)
      */
-    private static void appendOptionGroup(final StringBuffer buff, 
+    private void appendOptionGroup(final StringBuffer buff, 
                                           final OptionGroup group)
     {
         if (!group.isRequired())
@@ -550,7 +584,7 @@
         }
 
         List optList = new ArrayList(group.getOptions());
-        Collections.sort(optList, new OptionComparator());
+        Collections.sort(optList, getOptionComparator() );
         // for each option in the OptionGroup
         for (Iterator i = optList.iterator(); i.hasNext();)
         {
@@ -706,7 +740,7 @@
         Option option;
         List optList = options.helpOptions();
 
-        Collections.sort(optList, new OptionComparator());
+        Collections.sort(optList, getOptionComparator() );
 
         for (Iterator i = optList.iterator(); i.hasNext();)
         {

Modified: commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java?rev=655735&r1=655734&r2=655735&view=diff
==============================================================================
--- commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java
(original)
+++ commons/proper/cli/branches/cli-1.x/src/test/org/apache/commons/cli/HelpFormatterTest.java
Mon May 12 21:54:59 2008
@@ -19,6 +19,8 @@
 import java.io.ByteArrayOutputStream;
 import java.io.PrintWriter;
 
+import java.util.Comparator;
+
 import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
@@ -209,4 +211,30 @@
         assertEquals("usage: app [-a] [-b] [-c]" + EOL, bytesOut.toString());
     }
 
+    // uses the test for CLI-131 to implement CLI-155
+    public void testPrintSortedUsage() {
+        Option optionA = new Option("a", "first");
+        Option optionB = new Option("b", "second");
+        Option optionC = new Option("c", "third");
+        Options opts = new Options();
+        opts.addOption(optionA);
+        opts.addOption(optionB);
+        opts.addOption(optionC);
+        HelpFormatter helpFormatter = new HelpFormatter();
+        helpFormatter.setOptionComparator(
+            new Comparator() { 
+                public int compare(Object o1, Object o2) {
+                    // reverses the fuctionality of the default comparator
+                    Option opt1 = (Option)o1;
+                    Option opt2 = (Option)o2;
+                    return opt2.getKey().compareToIgnoreCase(opt1.getKey());
+                }
+            } );
+        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+        PrintWriter printWriter = new PrintWriter(bytesOut);
+        helpFormatter.printUsage(printWriter, 80, "app", opts);
+        printWriter.close();
+        assertEquals("usage: app [-c] [-b] [-a]" + EOL, bytesOut.toString());
+    }
+
 }



Mime
View raw message