@@ -125,6 +125,13 @@ public class HelpFormatter {
125125 */
126126 public String defaultArgName = DEFAULT_ARG_NAME ;
127127
128+ /**
129+ * Comparator used to sort the options when they output in help text
130+ *
131+ * Defaults to case-insensitive alphabetical sorting by option key
132+ */
133+ protected Comparator optionComparator = new OptionComparator ();
134+
128135 /**
129136 * Sets the 'width'.
130137 *
@@ -285,6 +292,33 @@ public String getArgName()
285292 return this .defaultArgName ;
286293 }
287294
295+ /**
296+ * Comparator used to sort the options when they output in help text
297+ *
298+ * Defaults to case-insensitive alphabetical sorting by option key
299+ */
300+ public Comparator getOptionComparator ()
301+ {
302+ return this .optionComparator ;
303+ }
304+
305+ /**
306+ * Set the comparator used to sort the options when they output in help text
307+ *
308+ * Passing in a null parameter will set the ordering to the default mode
309+ */
310+ public void setOptionComparator (Comparator comparator )
311+ {
312+ if ( comparator == null )
313+ {
314+ this .optionComparator = new OptionComparator ();
315+ }
316+ else
317+ {
318+ this .optionComparator = comparator ;
319+ }
320+ }
321+
288322
289323 // ------------------------------------------------------------------ Public
290324
@@ -487,7 +521,7 @@ public void printUsage(PrintWriter pw, int width, String app,
487521 Option option ;
488522
489523 List optList = new ArrayList (options .getOptions ());
490- Collections .sort (optList , new OptionComparator () );
524+ Collections .sort (optList , getOptionComparator () );
491525 // iterate over the options
492526 for (Iterator i = optList .iterator (); i .hasNext ();)
493527 {
@@ -541,7 +575,7 @@ public void printUsage(PrintWriter pw, int width, String app,
541575 * @param group the group to append
542576 * @see #appendOption(StringBuffer,Option,boolean)
543577 */
544- private static void appendOptionGroup (final StringBuffer buff ,
578+ private void appendOptionGroup (final StringBuffer buff ,
545579 final OptionGroup group )
546580 {
547581 if (!group .isRequired ())
@@ -550,7 +584,7 @@ private static void appendOptionGroup(final StringBuffer buff,
550584 }
551585
552586 List optList = new ArrayList (group .getOptions ());
553- Collections .sort (optList , new OptionComparator () );
587+ Collections .sort (optList , getOptionComparator () );
554588 // for each option in the OptionGroup
555589 for (Iterator i = optList .iterator (); i .hasNext ();)
556590 {
@@ -706,7 +740,7 @@ protected StringBuffer renderOptions(StringBuffer sb, int width,
706740 Option option ;
707741 List optList = options .helpOptions ();
708742
709- Collections .sort (optList , new OptionComparator () );
743+ Collections .sort (optList , getOptionComparator () );
710744
711745 for (Iterator i = optList .iterator (); i .hasNext ();)
712746 {
0 commit comments