Skip to content

Commit

Permalink
[#1408] (WIP pending #1574) synopsis should respect order (if specified)
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop committed Feb 14, 2022
1 parent ee3f60b commit 2ba9f62
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15340,8 +15340,11 @@ public String fullSynopsis() {
*/
public String synopsis(int synopsisHeadingLength) {
if (!empty(commandSpec.usageMessage().customSynopsis())) { return customSynopsis(); }
Comparator<OptionSpec> sortStrategy = true // #1574 TODO commandSpec.usageMessage().sortSynopsis()
? createShortOptionArityAndNameComparator() // alphabetic sort
: createOrderComparatorIfNecessary(commandSpec.options()); // explicit sort
return commandSpec.usageMessage().abbreviateSynopsis() ? abbreviatedSynopsis()
: detailedSynopsis(synopsisHeadingLength, createShortOptionArityAndNameComparator(), true);
: detailedSynopsis(synopsisHeadingLength, sortStrategy, true);
}

/** Generates a generic synopsis like {@code <command name> [OPTIONS] [PARAM1 [PARAM2]...]}, omitting parts
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/picocli/OrderedSynopsisTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package picocli;

import org.junit.Ignore;
import org.junit.Test;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import static org.junit.Assert.*;

// https://github.com/remkop/picocli/issues/1408
public class OrderedSynopsisTest {

@Ignore("#1408")
@Test
public void testIssue1408() {
@Command(name = "myCommand",
mixinStandardHelpOptions = true,
description = "A command with explicitly ordered options.",
sortOptions = false)
class Example {
@Option(names = { "-d", "--option-d" }, order = -10, description = "Should be first")
private void setD(String value) {
this.d = value;
}
private String d;

@Option(names = { "-c", "--option-c" }, order = -9, description = "Should be second")
private void setC(String value) {
this.c = value;
}
private String c;

@Option(names = { "-b", "--option-b" }, order = -8, description = "Should be third")
private String b;

@Option(names = { "-a", "--option-a" }, order = -7, description = "Should be fourth")
private String a;
}
String actual = new CommandLine(new Example()).getUsageMessage(CommandLine.Help.Ansi.OFF);
String expected = String.format("" +
"Usage: myCommand [-hV] [-d=<d>] [-c=<c>] [-b=<b>] [-a=<a>]%n" +
"A command with explicitly ordered options.%n" +
" -d, --option-d=<d> Should be first%n" +
" -c, --option-c=<c> Should be second%n" +
" -b, --option-b=<b> Should be third%n" +
" -a, --option-a=<a> Should be fourth%n" +
" -h, --help Show this help message and exit.%n" +
" -V, --version Print version information and exit.%n");
assertEquals(expected, actual);
}
}

0 comments on commit 2ba9f62

Please sign in to comment.