From fc8aa032df34377050ae947c8a0ddc85512f5428 Mon Sep 17 00:00:00 2001 From: Ruud Senden <8635138+rsenden@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:12:31 +0100 Subject: [PATCH] Update option rendering (fixes #1886) --- .../docgen/manpage/ManPageGenerator.java | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/picocli-codegen/src/main/java/picocli/codegen/docgen/manpage/ManPageGenerator.java b/picocli-codegen/src/main/java/picocli/codegen/docgen/manpage/ManPageGenerator.java index a705d5ede..45c0807c3 100644 --- a/picocli-codegen/src/main/java/picocli/codegen/docgen/manpage/ManPageGenerator.java +++ b/picocli-codegen/src/main/java/picocli/codegen/docgen/manpage/ManPageGenerator.java @@ -501,30 +501,27 @@ static void genOptions(PrintWriter pw, CommandSpec spec) { List groups = optionListGroups(spec); for (ArgGroupSpec group : groups) { options.removeAll(group.allOptionsNested()); } - - if (options.isEmpty() && !spec.usageMessage().showEndOfOptionsDelimiterInUsageHelp()) { - pw.printf("// tag::picocli-generated-man-section-options[]%n"); - pw.printf("// end::picocli-generated-man-section-options[]%n"); - pw.println(); - return; - } - pw.printf("// tag::picocli-generated-man-section-options[]%n"); - pw.printf("== Options%n"); - + Comparator optionSort = spec.usageMessage().sortOptions() ? new SortByShortestOptionNameAlphabetically() : createOrderComparatorIfNecessary(spec.options()); - if (optionSort != null) { - Collections.sort(options, optionSort); // default: sort options ABC - } - for (OptionSpec option : options) { - writeOption(pw, optionRenderer, paramLabelRenderer, option); - } + + pw.printf("// tag::picocli-generated-man-section-options[]%n"); + if (!options.isEmpty()) { + pw.printf("== Options%n"); - if (spec.usageMessage().showEndOfOptionsDelimiterInUsageHelp()) { - CommandLine cmd = new CommandLine(spec).setColorScheme(COLOR_SCHEME); - CommandLine.Help help = cmd.getHelp(); - writeEndOfOptions(pw, optionRenderer, paramLabelRenderer, help.END_OF_OPTIONS_OPTION); + if (optionSort != null) { + Collections.sort(options, optionSort); // default: sort options ABC + } + for (OptionSpec option : options) { + writeOption(pw, optionRenderer, paramLabelRenderer, option); + } + + if (spec.usageMessage().showEndOfOptionsDelimiterInUsageHelp()) { + CommandLine cmd = new CommandLine(spec).setColorScheme(COLOR_SCHEME); + CommandLine.Help help = cmd.getHelp(); + writeEndOfOptions(pw, optionRenderer, paramLabelRenderer, help.END_OF_OPTIONS_OPTION); + } } // now create a custom option section for each arg group that has a heading @@ -552,6 +549,7 @@ static void genOptions(PrintWriter pw, CommandSpec spec) { pw.println(); } + /** Returns the list of {@code ArgGroupSpec}s with a non-{@code null} heading. */ private static List optionListGroups(CommandSpec commandSpec) { List result = new ArrayList();