Skip to content

Commit df8f8e1

Browse files
committed
Making the OptionComparator modifiable so people can change the order in which arguments are printed by the HelpFormatter - as requested in CLI-155
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@655735 13f79535-47bb-0310-9956-ffa450edef68
1 parent fea3587 commit df8f8e1

2 files changed

Lines changed: 66 additions & 4 deletions

File tree

src/java/org/apache/commons/cli/HelpFormatter.java

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

src/test/org/apache/commons/cli/HelpFormatterTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.io.ByteArrayOutputStream;
2020
import java.io.PrintWriter;
2121

22+
import java.util.Comparator;
23+
2224
import junit.framework.TestCase;
2325
import junit.framework.TestSuite;
2426

@@ -209,4 +211,30 @@ public void testPrintUsage() {
209211
assertEquals("usage: app [-a] [-b] [-c]" + EOL, bytesOut.toString());
210212
}
211213

214+
// uses the test for CLI-131 to implement CLI-155
215+
public void testPrintSortedUsage() {
216+
Option optionA = new Option("a", "first");
217+
Option optionB = new Option("b", "second");
218+
Option optionC = new Option("c", "third");
219+
Options opts = new Options();
220+
opts.addOption(optionA);
221+
opts.addOption(optionB);
222+
opts.addOption(optionC);
223+
HelpFormatter helpFormatter = new HelpFormatter();
224+
helpFormatter.setOptionComparator(
225+
new Comparator() {
226+
public int compare(Object o1, Object o2) {
227+
// reverses the fuctionality of the default comparator
228+
Option opt1 = (Option)o1;
229+
Option opt2 = (Option)o2;
230+
return opt2.getKey().compareToIgnoreCase(opt1.getKey());
231+
}
232+
} );
233+
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
234+
PrintWriter printWriter = new PrintWriter(bytesOut);
235+
helpFormatter.printUsage(printWriter, 80, "app", opts);
236+
printWriter.close();
237+
assertEquals("usage: app [-c] [-b] [-a]" + EOL, bytesOut.toString());
238+
}
239+
212240
}

0 commit comments

Comments
 (0)