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

boolean field option is true if option is present but boolean method parameter option is false if option is present #1772

Closed
Onedy opened this issue Aug 9, 2022 · 4 comments
Labels
status: duplicate 👨🏻‍🤝‍👨🏻 A duplicate of another issue theme: parser An issue or change related to the parser type: bug 🐛
Milestone

Comments

@Onedy
Copy link

Onedy commented Aug 9, 2022

Minimal example:

@CommandLine.Command(name = "test")
public class TestCommand implements Command {
    @Option(names = "-r")
    public boolean option1;

    @CommandLine.Command(name = "subcommand")
    void start(@Option(names = "-s") boolean option2) {
        System.out.println("-r is "+option1 +" and -s is "+option2);
    }
}

Command: test -r subcommand -s
Output: -r is true and -s is false

@Onedy
Copy link
Author

Onedy commented Aug 9, 2022

It seems also that no matter what value I specify for -s option (after adding arity="1"), the value is always false.

@CommandLine.Command(name = "test")
public class TestCommand implements Command {
    @Option(names = "-r")
    public boolean option1;

    @CommandLine.Command(name = "subcommand")
    void start(@Option(names = "-s", arity = "1") boolean option2) {
        System.out.println("-r is "+option1 +" and -s is "+option2);
    }
}

Command: test -r subcommand -s true
Output: -r is true and -s is false

@remkop
Copy link
Owner

remkop commented Aug 9, 2022

I tested both variants you mentioned but I am unable to reproduce the issue. I get:

-r is true and -s is true

Can you try with -Dpicocli.trace=DEBUG and paste the output here?

@remkop remkop added the theme: parser An issue or change related to the parser label Aug 9, 2022
@Onedy
Copy link
Author

Onedy commented Aug 11, 2022

I don't have anymore the previous minimal example but I've created a minimal project where you can reproduce the issue with a test:
PicocliTest.zip
Just unzip and run with ./gradlew clean assemble :test --tests "test.MyTest" --info
And look at:

MyTest > testIssue1772() FAILED
    org.opentest4j.AssertionFailedError: 
    expected: "------> -r is true and -s is true <------"
     but was: "------> -r is true and -s is false <------"

@remkop remkop added this to the 4.7 milestone Aug 18, 2022
remkop added a commit that referenced this issue Aug 21, 2022
@remkop
Copy link
Owner

remkop commented Aug 21, 2022

@Onedy Thank you for the test project to reproduce the issue.
I created a derived test that tests the same without using Spring:

package test;

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

import static org.junit.jupiter.api.Assertions.*;
import static picocli.CommandLine.ScopeType.INHERIT;

/**
 * https://github.com/remkop/picocli/issues/1772
 */
public class Issue1772 {
    public CommandLine getTestCommandLine(ParentTestCommand parentCommand, TestCommand testCommand, IFactory factory) {
        CommandLine commandLine = new CommandLine(parentCommand, factory);
        commandLine.addSubcommand(testCommand);
        return commandLine;
    }
    @Command(name = "parentTestCommand", mixinStandardHelpOptions = true, scope = INHERIT)
    static class ParentTestCommand {
    }

    @Command(name = "test")
    static class TestCommand {
        @Option(names = "-r")
        public boolean option1;

        public static String testString;

        @Command(name = "subcommand")
        void start(@Option(names = "-s") boolean option2) {
            testString = "------> -r is " + option1 + " and -s is " + option2 + " <------";
        }
    }

    @Test
    public void testIssue1772() {
        CommandLine commandLine = getTestCommandLine(new ParentTestCommand(), new TestCommand(), CommandLine.defaultFactory());
        commandLine.execute("test", "-r", "subcommand", "-s");
        assertEquals("------> -r is true and -s is true <------", TestCommand.testString);
    }
}

The above test fails with picocli 4.6.3, as demonstrated when I add it to the test project you provided:

Issue1772 > testIssue1772() FAILED
    org.opentest4j.AssertionFailedError: expected: <------> -r is true and -s is true <------> but was: <------> -r is true and -s is false <------>
        at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
        at org.junit.jupiter.api.AssertionUtils.failNotEqual(AssertionUtils.java:62)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:177)
        at org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1141)
        at test.Issue1772.testIssue1772(Issue1772.java:40)

But when I run the same test with the current version of picocli in the main branch (which has the fix for #1741), then the same test passes.

So, I believe this ticket can also be closed: since it won't be a problem after the fix for #1741 is released in the next version of picocli.

@remkop remkop closed this as completed Aug 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate 👨🏻‍🤝‍👨🏻 A duplicate of another issue theme: parser An issue or change related to the parser type: bug 🐛
Projects
None yet
Development

No branches or pull requests

2 participants