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

Reusing CommandLine object doesn't reset values in argument groups options. #1768

Closed
valepakh opened this issue Aug 5, 2022 · 5 comments
Closed
Labels
theme: arg-group An issue or change related to argument groups theme: parser An issue or change related to the parser
Milestone

Comments

@valepakh
Copy link

valepakh commented Aug 5, 2022

The following test fails:

    @Test
    public void testReuseBothCommandLineAndUserObjectWithArgGroup() {
        Command userObject = new Command();
        CommandLine cmdLine = new CommandLine(userObject);
        cmdLine.execute("--group", "group", "--option", "option");
        cmdLine.execute();
        assertNull(userObject.option, "Expected option to be reset to null");
        assertNull(userObject.group.option, "Expected group option to be reset to null");
    }

    @Command(name = "command")
    static class Command implements Runnable {
        @Option(names = "--option")
        private String option;
        
        @ArgGroup
        private Group group;

        private static class Group {
            @Option(names = "--group")
            private String option;
        }

        @Override
        public void run() {
        }
    }
@remkop remkop added theme: arg-group An issue or change related to argument groups theme: parser An issue or change related to the parser labels Aug 18, 2022
@remkop remkop added this to the 4.7 milestone Aug 18, 2022
remkop added a commit that referenced this issue Oct 31, 2022
@remkop
Copy link
Owner

remkop commented Oct 31, 2022

Apologies for the late reply.
For the option in the ArgGroup, you can force the value to be reset to null by specifying defaultValue = Option.NULL_VALUE in the annotation:

    @Command(name = "command")
    static class MyCommand implements Runnable {
        @Option(names = "--option"/*, defaultValue = Option.NULL_VALUE*/) // not needed for non-group options
        private String option;

        @ArgGroup
        private Group group = new Group();

        private static class Group {
            @Option(names = "--group", defaultValue = Option.NULL_VALUE)
            private String option;
        }

        public void run() {
        }
    }

@valepakh
Copy link
Author

valepakh commented Nov 1, 2022

I might be missing something but the test still fails even with the defaultValue = Option.NULL_VALUE set.

@remkop
Copy link
Owner

remkop commented Nov 1, 2022

I added your test to ArgGroupTest.java.
I did have to switch the order of parameters in the assertNull statements because this test uses JUnit 4.
This test passes. Can you take a look?

@valepakh
Copy link
Author

valepakh commented Nov 1, 2022

It looks like it was fixed in #1531, thanks!

@valepakh
Copy link
Author

valepakh commented Nov 1, 2022

And it actually passes without defaultValue = Option.NULL_VALUE

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 theme: parser An issue or change related to the parser
Projects
None yet
Development

No branches or pull requests

2 participants