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

Options get doubled in non validating arg group when used in MixIn #2341

Open
sfeigl opened this issue Oct 14, 2024 · 2 comments
Open

Options get doubled in non validating arg group when used in MixIn #2341

sfeigl opened this issue Oct 14, 2024 · 2 comments
Labels
theme: arg-group An issue or change related to argument groups type: bug 🐛
Milestone

Comments

@sfeigl
Copy link

sfeigl commented Oct 14, 2024

When using a non-validating ArgGroup in a MixIn, the options of the ArgGroup are doubled in the "Usage" line. When placing the ArgGroup directly in the command class, the options are not doubled. When using a validating ArgGroup the options are also not doubled - as expected. As far as I tested to problem only occurs when using non-validating ArgGroups in a MixIn

I am using the ArgGroup to put a heading above a set of options that are added by a loadable plugin. The intention is to make clear which option belongs to which plugin.

Example:

plugin1:
--option
--anotherOption
plugin2:
--yetAnotherOption

The this code to reproduce the problem:

Minimal reproducing example

Actual output of picocli:

Force help
Usage: Test [--double] [--double]
Heading
--double This option is doubled

What I expect:

Force help
Usage: Test [--double]
Heading
--double This option is doubled

@remkop
Copy link
Owner

remkop commented Oct 14, 2024

Thank you for raising this. Sounds like you have found a bug. I will look into it when I have some time.

@remkop remkop added this to the 4.7.7 milestone Nov 3, 2024
@remkop remkop added the theme: arg-group An issue or change related to argument groups label Nov 3, 2024
@remkop
Copy link
Owner

remkop commented Jan 21, 2025

This is a bug caused by a change in the implementation for equals for ArgSpec.
I have to dig a bit into the history of this change...

            protected boolean equalsImpl(ArgSpec other) {
                return this.commandSpec == other.commandSpec <--- 

Test demonstrating the issue:

import org.junit.jupiter.api.Test;
import picocli.CommandLine;

import java.util.concurrent.Callable;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2341 {
    @CommandLine.Command
    static class TestCommand implements Callable<Integer> {
//        @CommandLine.Mixin MyMixIn myMixIn;
        public Integer call() throws Exception {
            return 0;
        }
    }

    static class MyMixIn {
        @CommandLine.ArgGroup(validate = false, heading = "Heading\n")
        private OptionGroup group;
    }

    public class OptionGroup {
        @CommandLine.Option(names="--double", description = "This option is doubled")
        boolean option;
    }

    @Test
    public void testIssue2341() {
        TestCommand testCommand = new TestCommand();
        CommandLine commandLine = new CommandLine(testCommand);
        commandLine.addMixin("myMixin", new MyMixIn());
        String usageMessage = commandLine.getUsageMessage(CommandLine.Help.Ansi.OFF);

        String expected = String.format("" +
                "Usage: <main class> [--double]%n" +
                "Heading%n" +
                "      --double   This option is doubled%n");
        assertEquals(expected, usageMessage);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
theme: arg-group An issue or change related to argument groups type: bug 🐛
Projects
None yet
Development

No branches or pull requests

4 participants
@sfeigl @remkop and others