Skip to content

Commit

Permalink
[#1575] Bugfix: Synopsis should not cluster boolean options if posixC…
Browse files Browse the repository at this point in the history
…lusteredShortOptionsAllowed=false

Closes #1575
  • Loading branch information
remkop committed Feb 15, 2022
1 parent ae3136d commit ead2f42
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The picocli community is pleased to announce picocli 4.7.0.

This release includes bugfixes and enhancements.

From this release, applications can programmatically set the trace level, and use tracing in custom components.

Also, this release has various fixes and enhancements related to the synopsis of the usage help message.


This is the seventy-ninth public release.
Picocli follows [semantic versioning](http://semver.org/).
Expand Down Expand Up @@ -62,6 +66,7 @@ Picocli 4.7.0 introduced a `sortSynopsis = false` attribute to let the synopsis
* [#1408] Enhancement: Synopsis should respect `order` if specified. Thanks to [Simon](https://github.com/sbernard31) for raising this.
* [#964][#1080] Enhancement: ArgGroup synopsis should respect `order` (if specified). Thanks to [Enderaoe](https://github.com/Lyther) for the pull request with unit tests.
* [#1572] Enhancement: Remove redundant braces in ArgGroup synopsis.
* [#1575] Bugfix: Synopsis should not cluster boolean options if `posixClusteredShortOptionsAllowed` is set to false.
* [#1573] DEP: Bump JLine3 version to 3.21.0 from 3.19.0.

## <a name="4.7.0-deprecated"></a> Deprecations
Expand Down
2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5956,7 +5956,7 @@ Picocli 4.7.0 introduced a `sortSynopsis = false` attribute to let the synopsis
@Command(sortSynopsis = false)
----

NOTE: Regardless of the sorting strategy, boolean short options are shown first as a single clustered group in the synopsis, followed by options that take parameters.
NOTE: Regardless of the sorting strategy, boolean short options are shown first as a single clustered group in the synopsis, followed by options that take parameters, unless the parser is <<POSIX Clustered Short Options,configured>> to disallow clustered boolean short options.

==== Required-Option Marker
Required options can be marked in the option list by the character specified with the `requiredOptionMarker` attribute. By default options are not marked because the synopsis shows users which options are required and which are optional. This feature may be useful in combination with the <<Abbreviated Synopsis,`abbreviateSynopsis`>> attribute. For example:
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/picocli/CommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15371,8 +15371,9 @@ public String synopsis(int synopsisHeadingLength) {
Comparator<OptionSpec> sortStrategy = commandSpec.usageMessage().sortSynopsis()
? createShortOptionArityAndNameComparator() // alphabetic sort
: createOrderComparatorIfNecessary(commandSpec.options()); // explicit sort
boolean clusterBooleanOptions = commandSpec.parser().posixClusteredShortOptionsAllowed();
return commandSpec.usageMessage().abbreviateSynopsis() ? abbreviatedSynopsis()
: detailedSynopsis(synopsisHeadingLength, sortStrategy, true);
: detailedSynopsis(synopsisHeadingLength, sortStrategy, clusterBooleanOptions);
}

/** Generates a generic synopsis like {@code <command name> [OPTIONS] [PARAM1 [PARAM2]...]}, omitting parts
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/picocli/HelpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,36 @@ class App {
assertEquals(expectedUsage, new CommandLine(new App()).getUsageMessage());
}

@Test
public void testSortSynopsisWithEqualIndexAndNoClustering() throws Exception {
@Command(sortOptions = false, sortSynopsis = false)
class App {
@Option(names = {"-a"}, order = 9) boolean a;
@Option(names = {"-b"}, order = 8) boolean b;
@Option(names = {"-c"}, order = 7) boolean c;
@Option(names = {"-d"}, order = 7) int d;
@Option(names = {"-e"}, order = 7) String[] e;
@Option(names = {"-f"}, order = 7) String[] f;
@Option(names = {"-g"}, order = 0) boolean g;
}
OptionSpec[] fields = options(new App(), "a", "b", "c", "d", "e", "f", "g");
Arrays.sort(fields, Help.createOrderComparator());
OptionSpec[] expected = options(new App(), "g", "c", "d", "e", "f", "b", "a");
assertArrayEquals(expected, fields);

String expectedUsage = String.format("" +
"Usage: <main class> [-g] [-c] [-d=<d>] [-e=<e>]... [-f=<f>]... [-b] [-a]%n" +
" -g%n" +
" -c%n" +
" -d=<d>%n" +
" -e=<e>%n" +
" -f=<f>%n" +
" -b%n" +
" -a%n");
assertEquals(expectedUsage, new CommandLine(new App())
.setPosixClusteredShortOptionsAllowed(false).getUsageMessage());
}

@Test
public void testSortDeclarationOrderWhenOrderAttributeOmitted() {
@Command(sortOptions = false)
Expand Down

0 comments on commit ead2f42

Please sign in to comment.