Skip to content

Commit 6f972cf

Browse files
committed
Applying Brian Egge's enhancement from CLI-131
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/branches/cli-1.0.x@544762 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3880640 commit 6f972cf

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,10 @@ public void printUsage(PrintWriter pw, int width, String app,
442442
// temp variable
443443
Option option;
444444

445+
List optList = new ArrayList(options.getOptions());
446+
Collections.sort(optList, new OptionComparator());
445447
// iterate over the options
446-
for (Iterator i = options.getOptions().iterator(); i.hasNext();)
448+
for (Iterator i = optList.iterator(); i.hasNext();)
447449
{
448450
// get the next Option
449451
option = (Option) i.next();
@@ -503,8 +505,10 @@ private static void appendOptionGroup(final StringBuffer buff,
503505
buff.append("[");
504506
}
505507

508+
List optList = new ArrayList(group.getOptions());
509+
Collections.sort(optList, new OptionComparator());
506510
// for each option in the OptionGroup
507-
for (Iterator i = group.getOptions().iterator(); i.hasNext();)
511+
for (Iterator i = optList.iterator(); i.hasNext();)
508512
{
509513
// whether the option is required or not is handled at group level
510514
appendOption(buff, (Option) i.next(), true);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ public void test27635() {
466466
StringWriter out = new StringWriter();
467467
formatter.printHelp(new PrintWriter(out),80,"commandline","header",mOptions,2,2,"footer",true);
468468
assertEquals(
469-
"usage: commandline [--config <arg>] [-r <arg>] [-a <arg>] [-h] [-t] [-n] [-l"+EOL+
470-
" <arg>] [-s <arg>] [-v]"+EOL+
469+
"usage: commandline [-a <arg>] [--config <arg>] [-h] [-l <arg>] [-n] [-r <arg>]" + EOL +
470+
" [-s <arg>] [-t] [-v]" + EOL +
471471
"header"+EOL+
472472
" -a,--age <arg> Age (in days) of cache item before being recomputed"+EOL+
473473
" --config <arg> Use the specified configuration file"+EOL+
@@ -527,7 +527,7 @@ public void test19383() {
527527
StringWriter out = new StringWriter();
528528
formatter.printHelp(new PrintWriter(out),80, "foobar", "", options, 2, 2, "", true);
529529
assertEquals(
530-
"usage: foobar [-a] [-c] [--bbb]"+SEP+
530+
"usage: foobar [-a] [--bbb] [-c]"+SEP+
531531
" -a,--aaa aaaaaaa"+SEP+
532532
" --bbb bbbbbbb"+SEP+
533533
" -c ccccccc"+SEP

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@
2727
*
2828
* @author Slawek Zachcial
2929
* @author John Keyes ( john at integralsource.com )
30+
* @author brianegge
3031
**/
3132
public class TestHelpFormatter extends TestCase
3233
{
34+
35+
private static final String EOL = System.getProperty("line.separator");
36+
3337
public static void main( String[] args )
3438
{
3539
String[] testName = { TestHelpFormatter.class.getName() };
@@ -174,4 +178,23 @@ public void testAutomaticUsage()
174178
assertEquals("simple auto usage", expected, out.toString().trim());
175179
out.reset();
176180
}
181+
182+
// This test ensures the options are properly sorted
183+
// See https://issues.apache.org/jira/browse/CLI-131
184+
public void testPrintUsage() {
185+
Option optionA = new Option("a", "first");
186+
Option optionB = new Option("b", "second");
187+
Option optionC = new Option("c", "third");
188+
Options opts = new Options();
189+
opts.addOption(optionA);
190+
opts.addOption(optionB);
191+
opts.addOption(optionC);
192+
HelpFormatter helpFormatter = new HelpFormatter();
193+
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
194+
PrintWriter printWriter = new PrintWriter(bytesOut);
195+
helpFormatter.printUsage(printWriter, 80, "app", opts);
196+
printWriter.close();
197+
assertEquals("usage: app [-a] [-b] [-c]" + EOL, bytesOut.toString());
198+
}
199+
177200
}

0 commit comments

Comments
 (0)