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

Command methods should pass null for unmatched primitive wrapper options #538

Closed
remkop opened this issue Nov 2, 2018 · 0 comments
Closed
Milestone

Comments

@remkop
Copy link
Owner

remkop commented Nov 2, 2018

Currently, when command methods have an option whose type is a primitive wrapper, like below:

@Command
public void cmd(@Option(names = "-x") Integer x) {}

If this command is invoked without a -x option, so the option is not matched, the value passed to the method is Integer.valueOf(0). This is correct when the option type is a primitive int, but not for an object Integer primitive wrapper.

Test to reproduce:

static class CommandMethodWithDefaults {
    @Command
    public String cmd(@Option(names = "-a", defaultValue = "2") Integer a,
                      @Option(names = "-b"                    ) Integer b,
                      @Option(names = "-c", defaultValue = "abc") String c,
                      @Option(names = "-d"                      ) String d,
                      @Option(names = "-e", defaultValue = "a=b") Map<String, String> e,
                      @Option(names = "-f"                      ) Map<String, String> f) {
        return String.format("a=%s, b=%s, c=%s, d=%s, e=%s, f=%s", a, b, c, d, e, f);
    }
}

@Test
public void testCommandMethodObjectDefaults() {
    Object s1 = CommandLine.invoke("cmd", CommandMethodWithDefaults.class);
    assertEquals("nothing matched", "a=2, b=null, c=abc, d=null, e={a=b}, f=null", s1); // fails

    Object s2 = CommandLine.invoke("cmd", CommandMethodWithDefaults.class, 
            "-a1", "-b2", "-cX", "-dY", "-eX=Y", "-fA=B");
    assertEquals("all matched", "a=1, b=2, c=X, d=Y, e={X=Y}, f={A=B}", s2);
}
@remkop remkop added this to the 3.8 milestone Nov 2, 2018
@remkop remkop modified the milestones: 3.8, 3.7.1 Nov 10, 2018
@remkop remkop closed this as completed in b761217 Nov 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant