Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsciiDoc generator doesn't output any options if all options are in ArgGroups #1886

Closed
rsenden opened this issue Nov 29, 2022 · 3 comments · Fixed by #1896
Closed

AsciiDoc generator doesn't output any options if all options are in ArgGroups #1886

rsenden opened this issue Nov 29, 2022 · 3 comments · Fixed by #1896
Labels
theme: codegen An issue or change related to the picocli-codegen module type: bug 🐛
Milestone

Comments

@rsenden
Copy link
Contributor

rsenden commented Nov 29, 2022

The AsciiDoc generator doesn't output any options for commands that have all their options defined in ArgGroups. Culprit is this piece of code:

        List<ArgGroupSpec> 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;
        }

If all options are contained in ArgGroups, then options will be empty, and due to the return statement in the code above, the code to output ArgGroup options is skipped:

        // now create a custom option section for each arg group that has a heading
        Collections.sort(groups, new SortByOrder<ArgGroupSpec>());
        for (ArgGroupSpec group : groups) {
            pw.println();
            String heading = makeHeading(group.heading(), "Options Group");
            pw.printf("== %s%n", COLOR_SCHEME.text(heading));
        ...

I'll look at submitting a PR, but any suggestions on how to best fix this are welcome.

@rsenden
Copy link
Contributor Author

rsenden commented Nov 29, 2022

For reference, the following implementation of ManPageGenerator::genOptions seems to work fine.

    static void genOptions(PrintWriter pw, CommandSpec spec) {
        List<OptionSpec> options = new ArrayList<OptionSpec>(spec.options()); // options are stored in order of declaration

        // remove hidden options
        for (Iterator<OptionSpec> iter = options.iterator(); iter.hasNext();) {
            if (iter.next().hidden()) { iter.remove(); }
        }

        IOptionRenderer optionRenderer = spec.commandLine().getHelp().createDefaultOptionRenderer();
        IParamLabelRenderer paramLabelRenderer = spec.commandLine().getHelp().createDefaultParamLabelRenderer();
        IParameterRenderer parameterRenderer = spec.commandLine().getHelp().createDefaultParameterRenderer();

        List<ArgGroupSpec> groups = optionListGroups(spec);
        for (ArgGroupSpec group : groups) { options.removeAll(group.allOptionsNested()); }
        
        Comparator<OptionSpec> optionSort = spec.usageMessage().sortOptions()
                ? new SortByShortestOptionNameAlphabetically()
                : createOrderComparatorIfNecessary(spec.options());
        
        pw.printf("// tag::picocli-generated-man-section-options[]%n");
        if (!options.isEmpty()) {
            pw.printf("== Options%n");

            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
        Collections.sort(groups, new SortByOrder<ArgGroupSpec>());
        for (ArgGroupSpec group : groups) {
            pw.println();
            String heading = makeHeading(group.heading(), "Options Group");
            pw.printf("== %s%n", COLOR_SCHEME.text(heading));

            for (PositionalParamSpec positional : group.allPositionalParametersNested()) {
                if (!positional.hidden()) {
                    writePositional(pw, positional, parameterRenderer, paramLabelRenderer);
                }
            }
            List<OptionSpec> groupOptions = new ArrayList<OptionSpec>(group.allOptionsNested());
            if (optionSort != null) {
                Collections.sort(groupOptions, optionSort);
            }
            for (OptionSpec option : groupOptions) {
                writeOption(pw, optionRenderer, paramLabelRenderer, option);
            }
        }
        pw.println();
        pw.printf("// end::picocli-generated-man-section-options[]%n");
        pw.println();
    }

@remkop
Copy link
Owner

remkop commented Nov 30, 2022

Thank you for letting me know!
I am a bit swamped with other (non-open source) stuff, I don't know when I will be able to look at this. Hopefully this weekend.

@remkop remkop added type: bug 🐛 theme: codegen An issue or change related to the picocli-codegen module labels Dec 16, 2022
@remkop remkop added this to the 4.7.1 milestone Dec 16, 2022
@remkop
Copy link
Owner

remkop commented Dec 16, 2022

That looks good at first glance. @rsenden thanks for the analysis and solution!

rsenden added a commit to fortify-ps/picocli that referenced this issue Dec 16, 2022
@remkop remkop linked a pull request Dec 17, 2022 that will close this issue
@remkop remkop closed this as completed in fc8aa03 Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: codegen An issue or change related to the picocli-codegen module type: bug 🐛
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants